@Test public void testGetVmsAsyncForPagination() throws IOException, InterruptedException { Vm vm1 = new Vm(); vm1.setId("vm1"); Vm vm2 = new Vm(); vm2.setId("vm2"); Vm vm3 = new Vm(); vm3.setId("vm3"); String nextPageLink = "nextPageLink"; final ResourceList<Vm> vmList = new ResourceList<>(Arrays.asList(vm1, vm2), nextPageLink, null); final ResourceList<Vm> vmListNextPage = new ResourceList<>(Arrays.asList(vm3)); ObjectMapper mapper = new ObjectMapper(); String serializedTask = mapper.writeValueAsString(vmList); String serializedTaskNextPage = mapper.writeValueAsString(vmListNextPage); setupMocksForPagination(serializedTask, serializedTaskNextPage, nextPageLink, HttpStatus.SC_OK); DeploymentApi deploymentApi = new DeploymentRestApi(restClient); final CountDownLatch latch = new CountDownLatch(1); deploymentApi.getAllDeploymentVmsAsync("foo", new FutureCallback<ResourceList<Vm>>() { @Override public void onSuccess(ResourceList<Vm> result) { assertEquals(result.getItems().size(), vmList.getItems().size() + vmListNextPage.getItems().size()); assertTrue(result.getItems().containsAll(vmList.getItems())); assertTrue(result.getItems().containsAll(vmListNextPage.getItems())); latch.countDown(); } @Override public void onFailure(Throwable t) { fail(t.toString()); latch.countDown(); } }); assertThat(latch.await(COUNTDOWNLATCH_AWAIT_TIMEOUT, TimeUnit.SECONDS), is(true)); }
@Override public void getAllDeploymentVmsAsync(String deploymentId, final FutureCallback<ResourceList<Vm>> responseCallback) throws IOException { String path = String.format("%s/%s/vms", getBasePath(), deploymentId); ResourceList<Vm> vmResourceList = new ResourceList<>(); FutureCallback<ResourceList<Vm>> callback = new FutureCallback<ResourceList<Vm>>() { @Override public void onSuccess(@Nullable ResourceList<Vm> result) { if (vmResourceList.getItems() == null) { vmResourceList.setItems(result.getItems()); } else { vmResourceList.getItems().addAll(result.getItems()); } if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) { try { getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<Vm>>() {}); } catch (IOException e) { e.printStackTrace(); } } else { responseCallback.onSuccess(vmResourceList); } } @Override public void onFailure(Throwable t) { responseCallback.onFailure(t); } }; getObjectByPathAsync(path, callback, new TypeReference<ResourceList<Vm>>() {}); }
DeploymentRestApi extends ApiBase implements DeploymentApi { @Override public void getAllDeploymentVmsAsync(String deploymentId, final FutureCallback<ResourceList<Vm>> responseCallback) throws IOException { String path = String.format("%s/%s/vms", getBasePath(), deploymentId); ResourceList<Vm> vmResourceList = new ResourceList<>(); FutureCallback<ResourceList<Vm>> callback = new FutureCallback<ResourceList<Vm>>() { @Override public void onSuccess(@Nullable ResourceList<Vm> result) { if (vmResourceList.getItems() == null) { vmResourceList.setItems(result.getItems()); } else { vmResourceList.getItems().addAll(result.getItems()); } if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) { try { getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<Vm>>() {}); } catch (IOException e) { e.printStackTrace(); } } else { responseCallback.onSuccess(vmResourceList); } } @Override public void onFailure(Throwable t) { responseCallback.onFailure(t); } }; getObjectByPathAsync(path, callback, new TypeReference<ResourceList<Vm>>() {}); } }
DeploymentRestApi extends ApiBase implements DeploymentApi { @Override public void getAllDeploymentVmsAsync(String deploymentId, final FutureCallback<ResourceList<Vm>> responseCallback) throws IOException { String path = String.format("%s/%s/vms", getBasePath(), deploymentId); ResourceList<Vm> vmResourceList = new ResourceList<>(); FutureCallback<ResourceList<Vm>> callback = new FutureCallback<ResourceList<Vm>>() { @Override public void onSuccess(@Nullable ResourceList<Vm> result) { if (vmResourceList.getItems() == null) { vmResourceList.setItems(result.getItems()); } else { vmResourceList.getItems().addAll(result.getItems()); } if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) { try { getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<Vm>>() {}); } catch (IOException e) { e.printStackTrace(); } } else { responseCallback.onSuccess(vmResourceList); } } @Override public void onFailure(Throwable t) { responseCallback.onFailure(t); } }; getObjectByPathAsync(path, callback, new TypeReference<ResourceList<Vm>>() {}); } DeploymentRestApi(RestClient restClient); }
DeploymentRestApi extends ApiBase implements DeploymentApi { @Override public void getAllDeploymentVmsAsync(String deploymentId, final FutureCallback<ResourceList<Vm>> responseCallback) throws IOException { String path = String.format("%s/%s/vms", getBasePath(), deploymentId); ResourceList<Vm> vmResourceList = new ResourceList<>(); FutureCallback<ResourceList<Vm>> callback = new FutureCallback<ResourceList<Vm>>() { @Override public void onSuccess(@Nullable ResourceList<Vm> result) { if (vmResourceList.getItems() == null) { vmResourceList.setItems(result.getItems()); } else { vmResourceList.getItems().addAll(result.getItems()); } if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) { try { getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<Vm>>() {}); } catch (IOException e) { e.printStackTrace(); } } else { responseCallback.onSuccess(vmResourceList); } } @Override public void onFailure(Throwable t) { responseCallback.onFailure(t); } }; getObjectByPathAsync(path, callback, new TypeReference<ResourceList<Vm>>() {}); } DeploymentRestApi(RestClient restClient); @Override String getBasePath(); @Override ResourceList<Deployment> listAll(); @Override void listAllAsync(final FutureCallback<ResourceList<Deployment>> responseCallback); @Override Task pauseSystem(String deploymentId); @Override void pauseSystemAsync(final String deploymentId, final FutureCallback<Task> responseCallback); @Override Task resumeSystem(String deploymentId); @Override void resumeSystemAsync(final String deploymentId, final FutureCallback<Task> responseCallback); @Override ResourceList<Vm> getAllDeploymentVms(final String deploymentId); @Override void getAllDeploymentVmsAsync(String deploymentId, final FutureCallback<ResourceList<Vm>> responseCallback); }
DeploymentRestApi extends ApiBase implements DeploymentApi { @Override public void getAllDeploymentVmsAsync(String deploymentId, final FutureCallback<ResourceList<Vm>> responseCallback) throws IOException { String path = String.format("%s/%s/vms", getBasePath(), deploymentId); ResourceList<Vm> vmResourceList = new ResourceList<>(); FutureCallback<ResourceList<Vm>> callback = new FutureCallback<ResourceList<Vm>>() { @Override public void onSuccess(@Nullable ResourceList<Vm> result) { if (vmResourceList.getItems() == null) { vmResourceList.setItems(result.getItems()); } else { vmResourceList.getItems().addAll(result.getItems()); } if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) { try { getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<Vm>>() {}); } catch (IOException e) { e.printStackTrace(); } } else { responseCallback.onSuccess(vmResourceList); } } @Override public void onFailure(Throwable t) { responseCallback.onFailure(t); } }; getObjectByPathAsync(path, callback, new TypeReference<ResourceList<Vm>>() {}); } DeploymentRestApi(RestClient restClient); @Override String getBasePath(); @Override ResourceList<Deployment> listAll(); @Override void listAllAsync(final FutureCallback<ResourceList<Deployment>> responseCallback); @Override Task pauseSystem(String deploymentId); @Override void pauseSystemAsync(final String deploymentId, final FutureCallback<Task> responseCallback); @Override Task resumeSystem(String deploymentId); @Override void resumeSystemAsync(final String deploymentId, final FutureCallback<Task> responseCallback); @Override ResourceList<Vm> getAllDeploymentVms(final String deploymentId); @Override void getAllDeploymentVmsAsync(String deploymentId, final FutureCallback<ResourceList<Vm>> responseCallback);  }