DeploymentRestApi extends ApiBase implements DeploymentApi { @Override public ResourceList<Vm> getAllDeploymentVms(final String deploymentId) throws IOException{ String path = String.format("%s/%s/vms", getBasePath(), deploymentId); ResourceList<Vm> vmResourceList = new ResourceList<>(); ResourceList<Vm> resourceList = getVmResourceList(path); vmResourceList.setItems(resourceList.getItems()); while (resourceList.getNextPageLink() != null && !resourceList.getNextPageLink().isEmpty()) { resourceList = getVmResourceList(resourceList.getNextPageLink()); vmResourceList.getItems().addAll(resourceList.getItems()); } return vmResourceList; } 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);  }
@Test public void testGetVms() throws IOException { Vm vm1 = new Vm(); vm1.setId("vm1"); Vm vm2 = new Vm(); vm2.setId("vm2"); final ResourceList<Vm> vmList = new ResourceList<>(Arrays.asList(vm1, vm2)); ObjectMapper mapper = new ObjectMapper(); String serializedTask = mapper.writeValueAsString(vmList); setupMocks(serializedTask, HttpStatus.SC_OK); DeploymentApi deploymentApi = new DeploymentRestApi(restClient); ResourceList<Vm> response = deploymentApi.getAllDeploymentVms("foo"); assertEquals(response.getItems().size(), vmList.getItems().size()); assertTrue(response.getItems().containsAll(vmList.getItems())); }
@Test public void testGetVmsForPagination() throws IOException { Vm vm1 = new Vm(); vm1.setId("vm1"); Vm vm2 = new Vm(); vm2.setId("vm2"); Vm vm3 = new Vm(); vm3.setId("vm3"); String nextPageLink = "nextPageLink"; ResourceList<Vm> vmList = new ResourceList<>(Arrays.asList(vm1, vm2), nextPageLink, null); 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); ResourceList<Vm> response = deploymentApi.getAllDeploymentVms("foo"); assertEquals(response.getItems().size(), vmList.getItems().size() + vmListNextPage.getItems().size()); assertTrue(response.getItems().containsAll(vmList.getItems())); assertTrue(response.getItems().containsAll(vmListNextPage.getItems())); }