@Test public void testListAllAsyncForPagination() throws Exception { Flavor flavor1 = new Flavor(); flavor1.setId("flavor1"); flavor1.setKind("vm"); Flavor flavor2 = new Flavor(); flavor2.setId("flavor2"); flavor2.setKind("vm"); Flavor flavor3 = new Flavor(); flavor3.setId("flavor3"); flavor3.setKind("vm"); String nextPageLink = "nextPageLink"; final ResourceList<Flavor> flavorResourceList = new ResourceList<>(Arrays.asList(flavor1, flavor2), nextPageLink, null); final ResourceList<Flavor> flavorResourceListNextPage = new ResourceList<>(Arrays.asList(flavor3)); final ResourceList<Flavor> expectedFlavorResourceList = new ResourceList<>(Arrays.asList(flavor1, flavor2, flavor3)); ObjectMapper mapper = new ObjectMapper(); String serializedResponse = mapper.writeValueAsString(flavorResourceList); String serializedResponseNextPage = mapper.writeValueAsString(flavorResourceListNextPage); setupMocksForPagination(serializedResponse, serializedResponseNextPage, nextPageLink, HttpStatus.SC_OK); FlavorApi flavorApi = new FlavorRestApi(restClient); final CountDownLatch latch = new CountDownLatch(1); flavorApi.listAllAsync(new FutureCallback<ResourceList<Flavor>>() { @Override public void onSuccess(@Nullable ResourceList<Flavor> result) { assertTrue(result.getItems().containsAll(expectedFlavorResourceList.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 listAllAsync(final FutureCallback<ResourceList<Flavor>> responseCallback) throws IOException { ResourceList<Flavor> flavorResourceList = new ResourceList<>(); FutureCallback<ResourceList<Flavor>> callback = new FutureCallback<ResourceList<Flavor>>() { @Override public void onSuccess(@Nullable ResourceList<Flavor> result) { if (flavorResourceList.getItems() == null) { flavorResourceList.setItems(result.getItems()); } else { flavorResourceList.getItems().addAll(result.getItems()); } if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) { try { getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<Flavor>>() {}); } catch (IOException e) { e.printStackTrace(); } } else { responseCallback.onSuccess(flavorResourceList); } } @Override public void onFailure(Throwable t) { responseCallback.onFailure(t); } }; getObjectByPathAsync(getBasePath(), callback, new TypeReference<ResourceList<Flavor>>() {}); }
FlavorRestApi extends ApiBase implements FlavorApi { @Override public void listAllAsync(final FutureCallback<ResourceList<Flavor>> responseCallback) throws IOException { ResourceList<Flavor> flavorResourceList = new ResourceList<>(); FutureCallback<ResourceList<Flavor>> callback = new FutureCallback<ResourceList<Flavor>>() { @Override public void onSuccess(@Nullable ResourceList<Flavor> result) { if (flavorResourceList.getItems() == null) { flavorResourceList.setItems(result.getItems()); } else { flavorResourceList.getItems().addAll(result.getItems()); } if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) { try { getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<Flavor>>() {}); } catch (IOException e) { e.printStackTrace(); } } else { responseCallback.onSuccess(flavorResourceList); } } @Override public void onFailure(Throwable t) { responseCallback.onFailure(t); } }; getObjectByPathAsync(getBasePath(), callback, new TypeReference<ResourceList<Flavor>>() {}); } }
FlavorRestApi extends ApiBase implements FlavorApi { @Override public void listAllAsync(final FutureCallback<ResourceList<Flavor>> responseCallback) throws IOException { ResourceList<Flavor> flavorResourceList = new ResourceList<>(); FutureCallback<ResourceList<Flavor>> callback = new FutureCallback<ResourceList<Flavor>>() { @Override public void onSuccess(@Nullable ResourceList<Flavor> result) { if (flavorResourceList.getItems() == null) { flavorResourceList.setItems(result.getItems()); } else { flavorResourceList.getItems().addAll(result.getItems()); } if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) { try { getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<Flavor>>() {}); } catch (IOException e) { e.printStackTrace(); } } else { responseCallback.onSuccess(flavorResourceList); } } @Override public void onFailure(Throwable t) { responseCallback.onFailure(t); } }; getObjectByPathAsync(getBasePath(), callback, new TypeReference<ResourceList<Flavor>>() {}); } FlavorRestApi(RestClient restClient); }
FlavorRestApi extends ApiBase implements FlavorApi { @Override public void listAllAsync(final FutureCallback<ResourceList<Flavor>> responseCallback) throws IOException { ResourceList<Flavor> flavorResourceList = new ResourceList<>(); FutureCallback<ResourceList<Flavor>> callback = new FutureCallback<ResourceList<Flavor>>() { @Override public void onSuccess(@Nullable ResourceList<Flavor> result) { if (flavorResourceList.getItems() == null) { flavorResourceList.setItems(result.getItems()); } else { flavorResourceList.getItems().addAll(result.getItems()); } if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) { try { getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<Flavor>>() {}); } catch (IOException e) { e.printStackTrace(); } } else { responseCallback.onSuccess(flavorResourceList); } } @Override public void onFailure(Throwable t) { responseCallback.onFailure(t); } }; getObjectByPathAsync(getBasePath(), callback, new TypeReference<ResourceList<Flavor>>() {}); } FlavorRestApi(RestClient restClient); @Override String getBasePath(); @Override Task create(FlavorCreateSpec flavorCreateSpec); @Override void createAsync(final FlavorCreateSpec flavorCreateSpec, final FutureCallback<Task> responseCallback); @Override ResourceList<Flavor> listAll(); @Override ResourceList<Flavor> listAll(Map<String, String> queryParams); @Override void listAllAsync(final FutureCallback<ResourceList<Flavor>> responseCallback); @Override Flavor getFlavor(String flavorId); @Override void getFlavorAsync(final String flavorId, final FutureCallback<Flavor> responseCallback); @Override Task delete(String id); @Override void deleteAsync(final String flavorId, final FutureCallback<Task> responseCallback); }
FlavorRestApi extends ApiBase implements FlavorApi { @Override public void listAllAsync(final FutureCallback<ResourceList<Flavor>> responseCallback) throws IOException { ResourceList<Flavor> flavorResourceList = new ResourceList<>(); FutureCallback<ResourceList<Flavor>> callback = new FutureCallback<ResourceList<Flavor>>() { @Override public void onSuccess(@Nullable ResourceList<Flavor> result) { if (flavorResourceList.getItems() == null) { flavorResourceList.setItems(result.getItems()); } else { flavorResourceList.getItems().addAll(result.getItems()); } if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) { try { getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<Flavor>>() {}); } catch (IOException e) { e.printStackTrace(); } } else { responseCallback.onSuccess(flavorResourceList); } } @Override public void onFailure(Throwable t) { responseCallback.onFailure(t); } }; getObjectByPathAsync(getBasePath(), callback, new TypeReference<ResourceList<Flavor>>() {}); } FlavorRestApi(RestClient restClient); @Override String getBasePath(); @Override Task create(FlavorCreateSpec flavorCreateSpec); @Override void createAsync(final FlavorCreateSpec flavorCreateSpec, final FutureCallback<Task> responseCallback); @Override ResourceList<Flavor> listAll(); @Override ResourceList<Flavor> listAll(Map<String, String> queryParams); @Override void listAllAsync(final FutureCallback<ResourceList<Flavor>> responseCallback); @Override Flavor getFlavor(String flavorId); @Override void getFlavorAsync(final String flavorId, final FutureCallback<Flavor> responseCallback); @Override Task delete(String id); @Override void deleteAsync(final String flavorId, final FutureCallback<Task> responseCallback);  }