TasksRestApi extends ApiBase implements TasksApi { @Override public void getTaskAsync(final String taskId, final FutureCallback<Task> responseCallback) throws IOException { String path = getBasePath() + "/" + taskId; getObjectByPathAsync(path, responseCallback, new TypeReference<Task>() { }); } TasksRestApi(RestClient restClient); @Override String getBasePath(); @Override Task getTask(final String taskId); @Override void getTaskAsync(final String taskId, final FutureCallback<Task> responseCallback);  }
@Test public void testGetTaskAsync() throws Throwable { final Task responseTask = new Task(); responseTask.setId("12345"); responseTask.setState("QUEUED"); responseTask.setQueuedTime(Date.from(Instant.now())); ObjectMapper mapper = new ObjectMapper(); String serializedTask = mapper.writeValueAsString(responseTask); setupMocks(serializedTask, HttpStatus.SC_OK); TasksApi tasksApi = new TasksRestApi(this.restClient); tasksApi.getTaskAsync("foo", new FutureCallback<Task>() { @Override public void onSuccess(@Nullable Task task) { assertEquals(task, responseTask); } @Override public void onFailure(Throwable t) { fail(t.toString()); } }); }
@Test(expectedExceptions = IOException.class) public void testGetTaskAsyncThrowsException() throws IOException { setupMocksToThrow(new IOException()); TasksApi tasksApi = new TasksRestApi(this.restClient); tasksApi.getTaskAsync("foo", new FutureCallback<Task>() { @Override public void onSuccess(@Nullable Task task) { fail("Unexpected success invoking api"); } @Override public void onFailure(Throwable t) { fail("Unexpected exception invoking api"); } }); fail("Unexpected success invoking api"); }
@Test public void testGetTaskAsyncBadHttpResponse() throws Throwable { setupMocks("GARBAGE", HttpStatus.SC_OK); TasksApi tasksApi = new TasksRestApi(this.restClient); tasksApi.getTaskAsync("foo", new FutureCallback<Task>() { @Override public void onSuccess(@Nullable Task task) { fail("Unexpected success invoking api"); } @Override public void onFailure(Throwable t) { assertTrue(t.getMessage().contains("GARBAGE")); } }); }
@Test(dataProvider = "failureStatusCodes") public void testGetTaskAsyncWrongResponseCode(final int status) throws IOException { setupMocks("IGNORED", status); TasksApi tasksApi = new TasksRestApi(this.restClient); tasksApi.getTaskAsync("foo", new FutureCallback<Task>() { @Override public void onSuccess(@Nullable Task task) { fail("Unexpected success invoking api"); } @Override public void onFailure(Throwable t) { assertTrue(t.getMessage().contains("HTTP request failed with: " + status)); } }); }