@Test public void testGetVmTasksAsyncForPagination() throws IOException, InterruptedException { Task task1 = new Task(); task1.setId("task1"); Task task2 = new Task(); task2.setId("task2"); Task task3 = new Task(); task3.setId("task3"); String nextPageLink = "nextPageLink"; ResourceList<Task> taskResourceList = new ResourceList<>(Arrays.asList(task1, task2), nextPageLink, null); ResourceList<Task> taskResourceListNextPage = new ResourceList<>(Arrays.asList(task3)); ObjectMapper mapper = new ObjectMapper(); String serialized = mapper.writeValueAsString(taskResourceList); String serializedNextPage = mapper.writeValueAsString(taskResourceListNextPage); setupMocksForPagination(serialized, serializedNextPage, nextPageLink, HttpStatus.SC_OK); VmApi vmApi = new VmRestApi(restClient); final CountDownLatch latch = new CountDownLatch(1); vmApi.getTasksForVmAsync("foo", new FutureCallback<ResourceList<Task>>() { @Override public void onSuccess(@Nullable ResourceList<Task> result) { assertEquals(result.getItems().size(), taskResourceList.getItems().size() + taskResourceListNextPage.getItems() .size()); assertTrue(result.getItems().containsAll(taskResourceList.getItems())); assertTrue(result.getItems().containsAll(taskResourceListNextPage.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 getTasksForVmAsync(final String vmId, final FutureCallback<ResourceList<Task>> responseCallback) throws IOException { String path = String.format("%s/%s/tasks", getBasePath(), vmId); ResourceList<Task> taskResourceList = new ResourceList<>(); FutureCallback<ResourceList<Task>> callback = new FutureCallback<ResourceList<Task>>() { @Override public void onSuccess(@Nullable ResourceList<Task> result) { if (taskResourceList.getItems() == null) { taskResourceList.setItems(result.getItems()); } else { taskResourceList.getItems().addAll(result.getItems()); } if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) { try { getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<Task>>() {}); } catch (IOException e) { e.printStackTrace(); } } else { responseCallback.onSuccess(taskResourceList); } } @Override public void onFailure(Throwable t) { responseCallback.onFailure(t); } }; getObjectByPathAsync(path, callback, new TypeReference<ResourceList<Task>>() {}); }
VmRestApi extends ApiBase implements VmApi { @Override public void getTasksForVmAsync(final String vmId, final FutureCallback<ResourceList<Task>> responseCallback) throws IOException { String path = String.format("%s/%s/tasks", getBasePath(), vmId); ResourceList<Task> taskResourceList = new ResourceList<>(); FutureCallback<ResourceList<Task>> callback = new FutureCallback<ResourceList<Task>>() { @Override public void onSuccess(@Nullable ResourceList<Task> result) { if (taskResourceList.getItems() == null) { taskResourceList.setItems(result.getItems()); } else { taskResourceList.getItems().addAll(result.getItems()); } if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) { try { getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<Task>>() {}); } catch (IOException e) { e.printStackTrace(); } } else { responseCallback.onSuccess(taskResourceList); } } @Override public void onFailure(Throwable t) { responseCallback.onFailure(t); } }; getObjectByPathAsync(path, callback, new TypeReference<ResourceList<Task>>() {}); } }
VmRestApi extends ApiBase implements VmApi { @Override public void getTasksForVmAsync(final String vmId, final FutureCallback<ResourceList<Task>> responseCallback) throws IOException { String path = String.format("%s/%s/tasks", getBasePath(), vmId); ResourceList<Task> taskResourceList = new ResourceList<>(); FutureCallback<ResourceList<Task>> callback = new FutureCallback<ResourceList<Task>>() { @Override public void onSuccess(@Nullable ResourceList<Task> result) { if (taskResourceList.getItems() == null) { taskResourceList.setItems(result.getItems()); } else { taskResourceList.getItems().addAll(result.getItems()); } if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) { try { getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<Task>>() {}); } catch (IOException e) { e.printStackTrace(); } } else { responseCallback.onSuccess(taskResourceList); } } @Override public void onFailure(Throwable t) { responseCallback.onFailure(t); } }; getObjectByPathAsync(path, callback, new TypeReference<ResourceList<Task>>() {}); } VmRestApi(RestClient restClient); }
VmRestApi extends ApiBase implements VmApi { @Override public void getTasksForVmAsync(final String vmId, final FutureCallback<ResourceList<Task>> responseCallback) throws IOException { String path = String.format("%s/%s/tasks", getBasePath(), vmId); ResourceList<Task> taskResourceList = new ResourceList<>(); FutureCallback<ResourceList<Task>> callback = new FutureCallback<ResourceList<Task>>() { @Override public void onSuccess(@Nullable ResourceList<Task> result) { if (taskResourceList.getItems() == null) { taskResourceList.setItems(result.getItems()); } else { taskResourceList.getItems().addAll(result.getItems()); } if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) { try { getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<Task>>() {}); } catch (IOException e) { e.printStackTrace(); } } else { responseCallback.onSuccess(taskResourceList); } } @Override public void onFailure(Throwable t) { responseCallback.onFailure(t); } }; getObjectByPathAsync(path, callback, new TypeReference<ResourceList<Task>>() {}); } VmRestApi(RestClient restClient); @Override String getBasePath(); @Override Vm getVm(String vmId); @Override void getVmAsync(final String vmId, final FutureCallback<Vm>
      responseCallback); @Override ResourceList<Task> getTasksForVm(String vmId); @Override void getTasksForVmAsync(final String vmId,
                                 final FutureCallback<ResourceList<Task>> responseCallback); @Override Task delete(String id); @Override void deleteAsync(final String id, final FutureCallback<Task> responseCallback); @Override Task addTagToVm(String vmId, Tag tag); @Override void addTagToVmAsync(final String vmId, Tag tag, final FutureCallback<Task> responseCallback); @Override Task performStartOperation(String vmId); @Override void performStartOperationAsync(final String vmId,
                                         final FutureCallback<Task> responseCallback); @Override Task performStopOperation(String vmId); @Override void performStopOperationAsync(final String vmId,
                                        final FutureCallback<Task> responseCallback); @Override Task performRestartOperation(String vmId); @Override void performRestartOperationAsync(final String vmId,
                                           final FutureCallback<Task> responseCallback); @Override Task performResumeOperation(String vmId); @Override void performResumeOperationAsync(final String vmId,
                                          final FutureCallback<Task> responseCallback); @Override Task performSuspendOperation(String vmId); @Override void performSuspendOperationAsync(final String vmId,
                                           final FutureCallback<Task> responseCallback); @Override Task attachDisk(String vmId, VmDiskOperation vmDiskOperation); @Override void attachDiskAsync(final String vmId, VmDiskOperation vmDiskOperation,
                              final FutureCallback<Task> responseCallback); @Override Task detachDisk(String vmId, VmDiskOperation vmDiskOperation); @Override void detachDiskAsync(final String vmId, VmDiskOperation vmDiskOperation,
                              final FutureCallback<Task> responseCallback); @Override Task uploadAndAttachIso(String vmId, String inputFileName); @Override Task detachIso(String vmId); @Override void detachIsoAsync(final String vmId, final FutureCallback<Task> responseCallback); @Override Task getNetworks(String vmId); @Override void getNetworksAsync(String vmId, FutureCallback<Task> responseCallback); static VmNetworks parseVmNetworksFromTask(Task task); @Override Task setMetadata(final String vmId, final VmMetadata metadata); @Override void setMetadataAsync(
      final String vmId,
      final VmMetadata metadata,
      final FutureCallback<Task> responseCallback); }
VmRestApi extends ApiBase implements VmApi { @Override public void getTasksForVmAsync(final String vmId, final FutureCallback<ResourceList<Task>> responseCallback) throws IOException { String path = String.format("%s/%s/tasks", getBasePath(), vmId); ResourceList<Task> taskResourceList = new ResourceList<>(); FutureCallback<ResourceList<Task>> callback = new FutureCallback<ResourceList<Task>>() { @Override public void onSuccess(@Nullable ResourceList<Task> result) { if (taskResourceList.getItems() == null) { taskResourceList.setItems(result.getItems()); } else { taskResourceList.getItems().addAll(result.getItems()); } if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) { try { getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<Task>>() {}); } catch (IOException e) { e.printStackTrace(); } } else { responseCallback.onSuccess(taskResourceList); } } @Override public void onFailure(Throwable t) { responseCallback.onFailure(t); } }; getObjectByPathAsync(path, callback, new TypeReference<ResourceList<Task>>() {}); } VmRestApi(RestClient restClient); @Override String getBasePath(); @Override Vm getVm(String vmId); @Override void getVmAsync(final String vmId, final FutureCallback<Vm>
      responseCallback); @Override ResourceList<Task> getTasksForVm(String vmId); @Override void getTasksForVmAsync(final String vmId,
                                 final FutureCallback<ResourceList<Task>> responseCallback); @Override Task delete(String id); @Override void deleteAsync(final String id, final FutureCallback<Task> responseCallback); @Override Task addTagToVm(String vmId, Tag tag); @Override void addTagToVmAsync(final String vmId, Tag tag, final FutureCallback<Task> responseCallback); @Override Task performStartOperation(String vmId); @Override void performStartOperationAsync(final String vmId,
                                         final FutureCallback<Task> responseCallback); @Override Task performStopOperation(String vmId); @Override void performStopOperationAsync(final String vmId,
                                        final FutureCallback<Task> responseCallback); @Override Task performRestartOperation(String vmId); @Override void performRestartOperationAsync(final String vmId,
                                           final FutureCallback<Task> responseCallback); @Override Task performResumeOperation(String vmId); @Override void performResumeOperationAsync(final String vmId,
                                          final FutureCallback<Task> responseCallback); @Override Task performSuspendOperation(String vmId); @Override void performSuspendOperationAsync(final String vmId,
                                           final FutureCallback<Task> responseCallback); @Override Task attachDisk(String vmId, VmDiskOperation vmDiskOperation); @Override void attachDiskAsync(final String vmId, VmDiskOperation vmDiskOperation,
                              final FutureCallback<Task> responseCallback); @Override Task detachDisk(String vmId, VmDiskOperation vmDiskOperation); @Override void detachDiskAsync(final String vmId, VmDiskOperation vmDiskOperation,
                              final FutureCallback<Task> responseCallback); @Override Task uploadAndAttachIso(String vmId, String inputFileName); @Override Task detachIso(String vmId); @Override void detachIsoAsync(final String vmId, final FutureCallback<Task> responseCallback); @Override Task getNetworks(String vmId); @Override void getNetworksAsync(String vmId, FutureCallback<Task> responseCallback); static VmNetworks parseVmNetworksFromTask(Task task); @Override Task setMetadata(final String vmId, final VmMetadata metadata); @Override void setMetadataAsync(
      final String vmId,
      final VmMetadata metadata,
      final FutureCallback<Task> responseCallback);  }