### Question:
PolicyResource implements CollectionResourceProvider { @Override public Promise<QueryResponse, ResourceException> queryCollection(Context context, QueryRequest request, QueryResourceHandler handler) { try { List<ResourceResponse> results = new ArrayList<>(); for (Privilege policy: policyStoreProvider.getPolicyStore(context).query(request)) { results.add(policyResource(policy)); } QueryResponsePresentation.enableDeprecatedRemainingQueryResponse(request); return QueryResponsePresentation.perform(handler, request, results); } catch (EntitlementException ex) { DEBUG.error("PolicyResource :: QUERY : Error querying policy collection.", ex); return resourceErrorHandler.handleError(context, request, ex).asPromise(); } catch (IllegalArgumentException ex) { DEBUG.error("PolicyResource :: QUERY : Error querying policy collection due to bad request.", ex); return new BadRequestException(ex.getMessage()).asPromise(); } } @Inject  PolicyResource(final PolicyEvaluatorFactory factory,
                          final PolicyRequestFactory requestFactory,
                          final PolicyParser parser,
                          final PolicyStoreProvider provider,
                          final ExceptionMappingHandler<EntitlementException, ResourceException> handler); @Override Promise<ActionResponse, ResourceException> actionCollection(Context context,
            ActionRequest actionRequest); @Override Promise<ActionResponse, ResourceException> actionInstance(Context context, String resourceId,
            ActionRequest request); @Override Promise<ResourceResponse, ResourceException> createInstance(Context context, CreateRequest request); @Override Promise<ResourceResponse, ResourceException> deleteInstance(Context context, String resourceId,
            DeleteRequest request); @Override Promise<ResourceResponse, ResourceException> patchInstance(Context context, String resourceId,
            PatchRequest request); @Override Promise<QueryResponse, ResourceException> queryCollection(Context context, QueryRequest request,
            QueryResourceHandler handler); @Override Promise<ResourceResponse, ResourceException> readInstance(Context context, String resourceId,
            ReadRequest request); @Override Promise<ResourceResponse, ResourceException> updateInstance(Context context, String resourceId,
            UpdateRequest request);  }### Answer:
@Test public void shouldHandleQueryRequests() throws Exception { QueryRequest request = mock(QueryRequest.class); QueryResourceHandler handler = mock(QueryResourceHandler.class); List<Privilege> policies = Arrays.<Privilege>asList( new StubPrivilege("one"), new StubPrivilege("two") ); given(mockStore.query(request)).willReturn(policies); given(handler.handleResource(any(ResourceResponse.class))).willReturn(true); policyResource.queryCollection(mockServerContext, request, handler); verify(handler, times(policies.size())).handleResource(any(ResourceResponse.class)); }
@Test public void shouldHandleInvalidQueryErrors() throws Exception { QueryRequest request = mock(QueryRequest.class); QueryResourceHandler handler = mock(QueryResourceHandler.class); given(mockStore.query(request)).willThrow(new EntitlementException(EntitlementException.INVALID_SEARCH_FILTER)); Promise<QueryResponse, ResourceException> promise = policyResource.queryCollection(mockServerContext, request, handler); assertQueryPromiseFailedWithCodes(promise, ResourceException.BAD_REQUEST, EntitlementException.INVALID_SEARCH_FILTER); }