@Test public void testGetVmsAsyncForPagination() throws IOException, InterruptedException { FlavoredCompact vm1 = new FlavoredCompact(); vm1.setId("vm1"); vm1.setKind("vm"); FlavoredCompact vm2 = new FlavoredCompact(); vm2.setId("vm2"); vm2.setKind("vm"); FlavoredCompact vm3 = new FlavoredCompact(); vm3.setId("vm3"); vm3.setKind("vm3"); String nextPageLink = "nextPageLink"; ResourceList<FlavoredCompact> vmSummaryList = new ResourceList<>(Arrays.asList(vm1, vm2), nextPageLink, null); ResourceList<FlavoredCompact> vmSummaryListNextPage = new ResourceList<>(Arrays.asList(vm3)); ObjectMapper mapper = new ObjectMapper(); String serializedTask = mapper.writeValueAsString(vmSummaryList); String serializedTaskNextPage = mapper.writeValueAsString(vmSummaryListNextPage); setupMocksForPagination(serializedTask, serializedTaskNextPage, nextPageLink, HttpStatus.SC_OK); ProjectApi projectApi = new ProjectRestApi(restClient); final CountDownLatch latch = new CountDownLatch(1); projectApi.getVmsInProjectAsync("foo", new FutureCallback<ResourceList<FlavoredCompact>>() { @Override public void onSuccess(@Nullable ResourceList<FlavoredCompact> result) { assertEquals(result.getItems().size(), vmSummaryList.getItems().size() + vmSummaryListNextPage.getItems().size()); assertTrue(result.getItems().containsAll(vmSummaryList.getItems())); assertTrue(result.getItems().containsAll(vmSummaryListNextPage.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 getVmsInProjectAsync(final String projectId, final FutureCallback<ResourceList<FlavoredCompact>> responseCallback) throws IOException { final String path = String.format("%s/%s/vms", getBasePath(), projectId); ResourceList<FlavoredCompact> flavoredCompactResourceList = new ResourceList<>(); FutureCallback<ResourceList<FlavoredCompact>> callback = new FutureCallback<ResourceList<FlavoredCompact>>() { @Override public void onSuccess(@Nullable ResourceList<FlavoredCompact> result) { if (flavoredCompactResourceList.getItems() == null) { flavoredCompactResourceList.setItems(result.getItems()); } else { flavoredCompactResourceList.getItems().addAll(result.getItems()); } if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) { try { getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<FlavoredCompact>>() {}); } catch (IOException e) { e.printStackTrace(); } } else { responseCallback.onSuccess(flavoredCompactResourceList); } } @Override public void onFailure(Throwable t) { responseCallback.onFailure(t); } }; getObjectByPathAsync(path, callback, new TypeReference<ResourceList<FlavoredCompact>>() {}); }
ProjectRestApi extends ApiBase implements ProjectApi { @Override public void getVmsInProjectAsync(final String projectId, final FutureCallback<ResourceList<FlavoredCompact>> responseCallback) throws IOException { final String path = String.format("%s/%s/vms", getBasePath(), projectId); ResourceList<FlavoredCompact> flavoredCompactResourceList = new ResourceList<>(); FutureCallback<ResourceList<FlavoredCompact>> callback = new FutureCallback<ResourceList<FlavoredCompact>>() { @Override public void onSuccess(@Nullable ResourceList<FlavoredCompact> result) { if (flavoredCompactResourceList.getItems() == null) { flavoredCompactResourceList.setItems(result.getItems()); } else { flavoredCompactResourceList.getItems().addAll(result.getItems()); } if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) { try { getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<FlavoredCompact>>() {}); } catch (IOException e) { e.printStackTrace(); } } else { responseCallback.onSuccess(flavoredCompactResourceList); } } @Override public void onFailure(Throwable t) { responseCallback.onFailure(t); } }; getObjectByPathAsync(path, callback, new TypeReference<ResourceList<FlavoredCompact>>() {}); } }
ProjectRestApi extends ApiBase implements ProjectApi { @Override public void getVmsInProjectAsync(final String projectId, final FutureCallback<ResourceList<FlavoredCompact>> responseCallback) throws IOException { final String path = String.format("%s/%s/vms", getBasePath(), projectId); ResourceList<FlavoredCompact> flavoredCompactResourceList = new ResourceList<>(); FutureCallback<ResourceList<FlavoredCompact>> callback = new FutureCallback<ResourceList<FlavoredCompact>>() { @Override public void onSuccess(@Nullable ResourceList<FlavoredCompact> result) { if (flavoredCompactResourceList.getItems() == null) { flavoredCompactResourceList.setItems(result.getItems()); } else { flavoredCompactResourceList.getItems().addAll(result.getItems()); } if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) { try { getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<FlavoredCompact>>() {}); } catch (IOException e) { e.printStackTrace(); } } else { responseCallback.onSuccess(flavoredCompactResourceList); } } @Override public void onFailure(Throwable t) { responseCallback.onFailure(t); } }; getObjectByPathAsync(path, callback, new TypeReference<ResourceList<FlavoredCompact>>() {}); } ProjectRestApi(RestClient restClient); }
ProjectRestApi extends ApiBase implements ProjectApi { @Override public void getVmsInProjectAsync(final String projectId, final FutureCallback<ResourceList<FlavoredCompact>> responseCallback) throws IOException { final String path = String.format("%s/%s/vms", getBasePath(), projectId); ResourceList<FlavoredCompact> flavoredCompactResourceList = new ResourceList<>(); FutureCallback<ResourceList<FlavoredCompact>> callback = new FutureCallback<ResourceList<FlavoredCompact>>() { @Override public void onSuccess(@Nullable ResourceList<FlavoredCompact> result) { if (flavoredCompactResourceList.getItems() == null) { flavoredCompactResourceList.setItems(result.getItems()); } else { flavoredCompactResourceList.getItems().addAll(result.getItems()); } if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) { try { getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<FlavoredCompact>>() {}); } catch (IOException e) { e.printStackTrace(); } } else { responseCallback.onSuccess(flavoredCompactResourceList); } } @Override public void onFailure(Throwable t) { responseCallback.onFailure(t); } }; getObjectByPathAsync(path, callback, new TypeReference<ResourceList<FlavoredCompact>>() {}); } ProjectRestApi(RestClient restClient); @Override String getBasePath(); @Override Project getProject(String projectId); @Override void getProjectAsync(final String projectId, final FutureCallback<Project> responseCallback); @Override ResourceList<Task> getTasksForProject(String projectId); @Override void getTasksForProjectAsync(final String projectId, final FutureCallback<ResourceList<Task>>
      responseCallback); @Override Task delete(String id); @Override void deleteAsync(final String id, final FutureCallback<Task> responseCallback); @Override Task createDisk(String projectId, DiskCreateSpec diskCreateSpec); @Override void createDiskAsync(final String projectId, final DiskCreateSpec diskCreateSpec, final FutureCallback<Task>
      responseCallback); @Override ResourceList<PersistentDisk> getDisksInProject(String projectId); @Override void getDisksInProjectAsync(final String projectId,
                                     final FutureCallback<ResourceList<PersistentDisk>> responseCallback); @Override Task createVm(String projectId, VmCreateSpec vmCreateSpec); @Override void createVmAsync(final String projectId, final VmCreateSpec vmCreateSpec, final FutureCallback<Task>
      responseCallback); @Override ResourceList<FlavoredCompact> getVmsInProject(String projectId); @Override ResourceList<Vm> getVmDetailsInProject(String projectId); @Override void getVmsInProjectAsync(final String projectId, final FutureCallback<ResourceList<FlavoredCompact>>
      responseCallback); @Override Task createCluster(String projectId, ClusterCreateSpec clusterCreateSpec); @Override void createClusterAsync(final String projectId, final ClusterCreateSpec clusterCreateSpec,
                                 final FutureCallback<Task> responseCallback); @Override ResourceList<Cluster> getClustersInProject(String projectId); @Override void getClustersInProjectAsync(final String projectId, final FutureCallback<ResourceList<Cluster>>
      responseCallback); }
ProjectRestApi extends ApiBase implements ProjectApi { @Override public void getVmsInProjectAsync(final String projectId, final FutureCallback<ResourceList<FlavoredCompact>> responseCallback) throws IOException { final String path = String.format("%s/%s/vms", getBasePath(), projectId); ResourceList<FlavoredCompact> flavoredCompactResourceList = new ResourceList<>(); FutureCallback<ResourceList<FlavoredCompact>> callback = new FutureCallback<ResourceList<FlavoredCompact>>() { @Override public void onSuccess(@Nullable ResourceList<FlavoredCompact> result) { if (flavoredCompactResourceList.getItems() == null) { flavoredCompactResourceList.setItems(result.getItems()); } else { flavoredCompactResourceList.getItems().addAll(result.getItems()); } if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) { try { getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<FlavoredCompact>>() {}); } catch (IOException e) { e.printStackTrace(); } } else { responseCallback.onSuccess(flavoredCompactResourceList); } } @Override public void onFailure(Throwable t) { responseCallback.onFailure(t); } }; getObjectByPathAsync(path, callback, new TypeReference<ResourceList<FlavoredCompact>>() {}); } ProjectRestApi(RestClient restClient); @Override String getBasePath(); @Override Project getProject(String projectId); @Override void getProjectAsync(final String projectId, final FutureCallback<Project> responseCallback); @Override ResourceList<Task> getTasksForProject(String projectId); @Override void getTasksForProjectAsync(final String projectId, final FutureCallback<ResourceList<Task>>
      responseCallback); @Override Task delete(String id); @Override void deleteAsync(final String id, final FutureCallback<Task> responseCallback); @Override Task createDisk(String projectId, DiskCreateSpec diskCreateSpec); @Override void createDiskAsync(final String projectId, final DiskCreateSpec diskCreateSpec, final FutureCallback<Task>
      responseCallback); @Override ResourceList<PersistentDisk> getDisksInProject(String projectId); @Override void getDisksInProjectAsync(final String projectId,
                                     final FutureCallback<ResourceList<PersistentDisk>> responseCallback); @Override Task createVm(String projectId, VmCreateSpec vmCreateSpec); @Override void createVmAsync(final String projectId, final VmCreateSpec vmCreateSpec, final FutureCallback<Task>
      responseCallback); @Override ResourceList<FlavoredCompact> getVmsInProject(String projectId); @Override ResourceList<Vm> getVmDetailsInProject(String projectId); @Override void getVmsInProjectAsync(final String projectId, final FutureCallback<ResourceList<FlavoredCompact>>
      responseCallback); @Override Task createCluster(String projectId, ClusterCreateSpec clusterCreateSpec); @Override void createClusterAsync(final String projectId, final ClusterCreateSpec clusterCreateSpec,
                                 final FutureCallback<Task> responseCallback); @Override ResourceList<Cluster> getClustersInProject(String projectId); @Override void getClustersInProjectAsync(final String projectId, final FutureCallback<ResourceList<Cluster>>
      responseCallback);  }