### Question:
Requester { public JsonValue query(String location, String queryId, Context context) throws ResourceException { Reject.ifTrue(StringUtils.isEmpty(location), "The endpoint destination may not be null or empty."); final Router rootRouter = router.get(); final QueryRequest queryRequest = Requests.newQueryRequest(location); if (queryId != null) { queryRequest.setQueryId(queryId); } final InMemoryQueryResourceHandler resourceHandler = new InMemoryQueryResourceHandler(); return rootRouter.handleQuery(context, queryRequest, resourceHandler) .thenAsync(new AsyncFunction<QueryResponse, JsonValue, ResourceException>() { @Override public Promise<JsonValue, ResourceException> apply(QueryResponse value) { final JsonArray responses = JsonValueBuilder.jsonValue().array("results"); for (ResourceResponse resource : resourceHandler.getResources()) { responses.add(resource.getContent()); } return newResultPromise(responses.build().build()); } }).getOrThrowUninterruptibly(); } @Inject  Requester(@Named("CrestRootRouter") Provider<Router> router); JsonValue create(String location, String resourceId, JsonValue payload, Context context); JsonValue read(String location, String resourceId, Context context); JsonValue update(String location, String resourceId, JsonValue payload, Context context); JsonValue delete(String location, String resourceId, Context context); JsonValue action(String location, String resourceId, String actionId, JsonValue payload,
                                    Context context); JsonValue query(String location, String queryId, Context context);  }### Answer:
@Test(expectedExceptions = IllegalArgumentException.class) public void emptyLocationBreaksQuery() throws ResourceException { String location = ""; String queryId = "queryId"; Context fakeContext = mock(Context.class); requester.query(location, queryId, fakeContext); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullLocationBreaksQuery() throws ResourceException { String location = null; String queryId = "queryId"; Context fakeContext = mock(Context.class); requester.query(location, queryId, fakeContext); }
@Test public void shouldExecuteQueryOperation() throws ResourceException { String location = "location"; String queryId = "queryId"; JsonValue expectedResult = JsonValueBuilder.toJsonValue("{ \"lemon\" : \"custard\" } "); Promise<QueryResponse, ResourceException> mockResponse = mock(Promise.class); QueryResponse mockResourceResponse = mock(QueryResponse.class); Promise<JsonValue, ResourceException> asyncQueryResponse = mock(Promise.class); given(mockRealmRouterProvider.get()).willReturn(mockRouter); given(mockRouter.handleQuery(any(Context.class), any(QueryRequest.class), any(QueryResourceHandler.class))) .willReturn(mockResponse); given(mockResponse.thenAsync(any(AsyncFunction.class))).willReturn(asyncQueryResponse); given(asyncQueryResponse.getOrThrowUninterruptibly()).willReturn(expectedResult); JsonValue actualResult = requester.query(location, queryId, mockServerContext); verify(mockRealmRouterProvider, times(1)).get(); verifyNoMoreInteractions(mockRealmRouterProvider); verify(mockRouter, times(1)).handleQuery(any(Context.class), any(QueryRequest.class), any(QueryResourceHandler.class)); verifyNoMoreInteractions(mockRouter); verify(mockResponse, times(1)).thenAsync(any(AsyncFunction.class)); verifyNoMoreInteractions(mockResponse); verify(asyncQueryResponse, times(1)).getOrThrowUninterruptibly(); verifyNoMoreInteractions(mockResourceResponse); assertThat(actualResult).isEqualTo(expectedResult); }