### Question:
UmaPolicyResource implements CollectionResourceProvider { @Override public Promise<QueryResponse, ResourceException> queryCollection(Context context, final QueryRequest request, final QueryResourceHandler handler) { return umaPolicyService.queryPolicies(context, request) .thenAsync(new AsyncFunction<Pair<QueryResponse, Collection<UmaPolicy>>, QueryResponse, ResourceException>() { @Override public Promise<QueryResponse, ResourceException> apply(Pair<QueryResponse, Collection<UmaPolicy>> result) { List<ResourceResponse> values = new ArrayList<>(); for (UmaPolicy policy : result.getSecond()) { values.add(newResourceResponse(policy.getId(), null, policy.asJson())); } return QueryResponsePresentation.perform(handler, request, values); } }); } @Inject  UmaPolicyResource(UmaPolicyService umaPolicyService); @Override Promise<ResourceResponse, ResourceException> createInstance(Context context, CreateRequest request); @Override Promise<ResourceResponse, ResourceException> readInstance(Context context, final String resourceId,
            ReadRequest request); @Override Promise<ResourceResponse, ResourceException> updateInstance(Context context, String resourceId,
            UpdateRequest request); @Override Promise<ResourceResponse, ResourceException> deleteInstance(Context context, final String resourceId,
            DeleteRequest request); @Override Promise<ResourceResponse, ResourceException> patchInstance(Context context, String resourceId,
            PatchRequest request); @Override Promise<ActionResponse, ResourceException> actionCollection(Context context, ActionRequest request); @Override Promise<ActionResponse, ResourceException> actionInstance(Context context, String resourceId,
            ActionRequest request); @Override Promise<QueryResponse, ResourceException> queryCollection(Context context, final QueryRequest request,
            final QueryResourceHandler handler);  }### Answer:
@Test public void shouldSuccessfullyQueryPolicies() { Context context = mock(Context.class); QueryRequest request = Requests.newQueryRequest("/policies"); QueryResourceHandler handler = mock(QueryResourceHandler.class); given(handler.handleResource(any(ResourceResponse.class))).willReturn(true); QueryResponse queryResult = newQueryResponse(); Collection<UmaPolicy> umaPolicies = new HashSet<UmaPolicy>(); UmaPolicy policy1 = mock(UmaPolicy.class); UmaPolicy policy2 = mock(UmaPolicy.class); umaPolicies.add(policy1); umaPolicies.add(policy2); Promise<Pair<QueryResponse, Collection<UmaPolicy>>, ResourceException> promise = Promises.newResultPromise(Pair.of(queryResult, umaPolicies)); given(policyService.queryPolicies(context, request)).willReturn(promise); policyResource.queryCollection(context, request, handler); verify(handler, times(2)).handleResource(Matchers.<ResourceResponse>anyObject()); }
@Test public void shouldHandleFailedQueryOfPolicies() { Context context = mock(Context.class); QueryRequest request = Requests.newQueryRequest("/policies"); QueryResourceHandler handler = mock(QueryResourceHandler.class); ResourceException resourceException = mock(ResourceException.class); Promise<Pair<QueryResponse, Collection<UmaPolicy>>, ResourceException> promise = Promises.newExceptionPromise(resourceException); given(policyService.queryPolicies(context, request)).willReturn(promise); Promise<QueryResponse, ResourceException> result = policyResource.queryCollection(context, request, handler); assertThat(result).failedWithException().isEqualTo(resourceException); }