TenantsRestApi extends ApiBase implements TenantsApi { @Override public ResourceList<Project> getProjects(String tenantId) throws IOException { String path = String.format("%s/%s/projects", getBasePath(), tenantId); ResourceList<Project> projectResourceList = new ResourceList<>(); ResourceList<Project> resourceList = getProjectResourceList(path); projectResourceList.setItems(resourceList.getItems()); while (resourceList.getNextPageLink() != null && !resourceList.getNextPageLink().isEmpty()) { resourceList = getProjectResourceList(resourceList.getNextPageLink()); projectResourceList.getItems().addAll(resourceList.getItems()); } return projectResourceList; } TenantsRestApi(RestClient restClient); @Override String getBasePath(); @Override Task create(String name); @Override void createAsync(final String name, final FutureCallback<Task> responseCallback); @Override ResourceList<Tenant> listAll(); @Override void listAllAsync(final FutureCallback<ResourceList<Tenant>> responseCallback); @Override ResourceList<Tenant> listByName(String name); @Override Tenant getTenant(String tenantId); @Override void listByNameAsync(final String name, final FutureCallback<ResourceList<Tenant>> responseCallback); @Override Task delete(String id); @Override void deleteAsync(final String id, final FutureCallback<Task> responseCallback); @Override Task createResourceTicket(String tenantId, ResourceTicketCreateSpec resourceTicketCreateSpec); @Override void createResourceTicketAsync(String tenantId, ResourceTicketCreateSpec resourceTicketCreateSpec,
                                        final FutureCallback<Task> responseCallback); @Override ResourceList<ResourceTicket> getResourceTickets(String tenantId); @Override void getResourceTicketsAsync(final String tenantId, final FutureCallback<ResourceList<ResourceTicket>>
      responseCallback); @Override Task createProject(String tenantId, ProjectCreateSpec projectCreateSpec); @Override void createProjectAsync(String tenantId, ProjectCreateSpec projectCreateSpec,
                                 final FutureCallback<Task> responseCallback); @Override ResourceList<Project> getProjects(String tenantId); @Override void getProjectsAsync(final String tenantId, final FutureCallback<ResourceList<Project>>
      responseCallback);  }
@Test public void testGetProjects() throws IOException { Project project1 = new Project(); project1.setId("project1"); Project project2 = new Project(); project2.setId("project2"); ResourceList<Project> projectResourceList = new ResourceList<>( Arrays.asList(project1, project2)); ObjectMapper mapper = new ObjectMapper(); String serializedTask = mapper.writeValueAsString(projectResourceList); setupMocks(serializedTask, HttpStatus.SC_OK); TenantsApi tenantsApi = new TenantsRestApi(restClient); ResourceList<Project> response = tenantsApi.getProjects("foo"); assertEquals(response.getItems().size(), projectResourceList.getItems().size()); assertTrue(response.getItems().containsAll(projectResourceList.getItems())); }
@Test public void testGetProjectsForPagination() throws IOException { Project project1 = new Project(); project1.setId("project1"); Project project2 = new Project(); project2.setId("project2"); Project project3 = new Project(); project3.setId("project3"); String nextPageLink = "nextPageLink"; ResourceList<Project> projectResourceList = new ResourceList<>(Arrays.asList(project1, project2), nextPageLink, null); ResourceList<Project> projectResourceListNextPage = new ResourceList<>(Arrays.asList(project3)); ObjectMapper mapper = new ObjectMapper(); String serializedTask = mapper.writeValueAsString(projectResourceList); String serializedTaskNextPage = mapper.writeValueAsString(projectResourceListNextPage); setupMocksForPagination(serializedTask, serializedTaskNextPage, nextPageLink, HttpStatus.SC_OK); TenantsApi tenantsApi = new TenantsRestApi(restClient); ResourceList<Project> response = tenantsApi.getProjects("foo"); assertEquals(response.getItems().size(), projectResourceList.getItems().size() + projectResourceListNextPage .getItems().size()); assertTrue(response.getItems().containsAll(projectResourceList.getItems())); assertTrue(response.getItems().containsAll(projectResourceListNextPage.getItems())); }