TenantsRestApi extends ApiBase implements TenantsApi { @Override public void listByNameAsync(final String name, final FutureCallback<ResourceList<Tenant>> responseCallback) throws IOException { String path = getBasePath(); if (name != null) { path += "?name=" + name; } ResourceList<Tenant> tenantResourceList = new ResourceList<>(); FutureCallback<ResourceList<Tenant>> callback = new FutureCallback<ResourceList<Tenant>>() { @Override public void onSuccess(@Nullable ResourceList<Tenant> result) { if (tenantResourceList.getItems() == null) { tenantResourceList.setItems(result.getItems()); } else { tenantResourceList.getItems().addAll(result.getItems()); } if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) { try { getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<Tenant>>() {}); } catch (IOException e) { e.printStackTrace(); } } else { responseCallback.onSuccess(tenantResourceList); } } @Override public void onFailure(Throwable t) { responseCallback.onFailure(t); } }; getObjectByPathAsync(path, callback, new TypeReference<ResourceList<Tenant>>() {}); } 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 testListAsync() throws IOException, InterruptedException { Tenant tenant1 = new Tenant(); tenant1.setId("tenant1"); tenant1.setName("tenant1"); Tenant tenant2 = new Tenant(); tenant2.setId("tenant2"); tenant2.setName("tenant2"); final ResourceList<Tenant> tenantResourceList = new ResourceList<>(Arrays.asList(tenant1, tenant2)); ObjectMapper mapper = new ObjectMapper(); String serializedTask = mapper.writeValueAsString(tenantResourceList); setupMocks(serializedTask, HttpStatus.SC_OK); TenantsApi tenantsApi = new TenantsRestApi(restClient); final CountDownLatch latch = new CountDownLatch(1); tenantsApi.listByNameAsync("foo", new FutureCallback<ResourceList<Tenant>>() { @Override public void onSuccess(@Nullable ResourceList<Tenant> result) { assertEquals(result.getItems(), tenantResourceList.getItems()); latch.countDown(); } @Override public void onFailure(Throwable t) { fail(t.toString()); latch.countDown(); } }); assertThat(latch.await(COUNTDOWNLATCH_AWAIT_TIMEOUT, TimeUnit.SECONDS), is(true)); }
@Test public void testListAsyncForPagination() throws IOException, InterruptedException { Tenant tenant1 = new Tenant(); tenant1.setId("tenant1"); tenant1.setName("tenant1"); Tenant tenant2 = new Tenant(); tenant2.setId("tenant2"); tenant2.setName("tenant2"); Tenant tenant3 = new Tenant(); tenant3.setId("tenant3"); tenant3.setName("tenant3"); String nextPageLink = "nextPageLink"; ResourceList<Tenant> tenantResourceList = new ResourceList<>(Arrays.asList(tenant1, tenant2), nextPageLink, null); ResourceList<Tenant> tenantResourceListNextPage = new ResourceList<>(Arrays.asList(tenant3)); ObjectMapper mapper = new ObjectMapper(); String serializedTask = mapper.writeValueAsString(tenantResourceList); String serializedTaskNextPage = mapper.writeValueAsString(tenantResourceListNextPage); setupMocksForPagination(serializedTask, serializedTaskNextPage, nextPageLink, HttpStatus.SC_OK); TenantsApi tenantsApi = new TenantsRestApi(restClient); final CountDownLatch latch = new CountDownLatch(1); tenantsApi.listByNameAsync("foo", new FutureCallback<ResourceList<Tenant>>() { @Override public void onSuccess(@Nullable ResourceList<Tenant> result) { assertEquals(result.getItems().size(), tenantResourceList.getItems().size() + tenantResourceListNextPage .getItems().size()); assertTrue(result.getItems().containsAll(tenantResourceList.getItems())); assertTrue(result.getItems().containsAll(tenantResourceListNextPage.getItems())); latch.countDown(); } @Override public void onFailure(Throwable t) { fail(t.toString()); latch.countDown(); } }); assertThat(latch.await(COUNTDOWNLATCH_AWAIT_TIMEOUT, TimeUnit.SECONDS), is(true)); }