### Question:
PolicyResourceDelegate { public Promise<List<ResourceResponse>, ResourceException> updatePolicies(Context context, Set<JsonValue> policies) { List<Promise<ResourceResponse, ResourceException>> promises = new ArrayList<>(); for (JsonValue policy : policies) { String policyName = policy.get("name").asString(); promises.add(policyResource.handleUpdate(context, Requests.newUpdateRequest(policyName, policy))); } return Promises.when(promises); } @Inject  PolicyResourceDelegate(@Named(UMA_BACKEND_POLICY_RESOURCE_HANDLER) RequestHandler policyResource); Promise<List<ResourceResponse>, ResourceException> createPolicies(Context context,
            Set<JsonValue> policies); Promise<List<ResourceResponse>, ResourceException> updatePolicies(Context context,
            Set<JsonValue> policies); Promise<Pair<QueryResponse, List<ResourceResponse>>, ResourceException> queryPolicies(Context context,
            QueryRequest request); Promise<QueryResponse, ResourceException> queryPolicies(Context context,
            QueryRequest request, QueryResourceHandler resultHandler); Promise<List<ResourceResponse>, ResourceException> deletePolicies(Context context,
            Collection<String> policyIds);  }### Answer:
@Test public void shouldUpdatePolicies() throws ResourceException { Context context = mock(Context.class); Set<JsonValue> policies = new HashSet<JsonValue>(); JsonValue policyOne = json(object(field("name", "POLICY_ONE"))); JsonValue policyTwo = json(object(field("name", "POLICY_TWO"))); policies.add(policyOne); policies.add(policyTwo); List<ResourceResponse> updatedPolicies = new ArrayList<ResourceResponse>(); ResourceResponse updatedPolicyOne = newResourceResponse("ID_1", "REVISION_1", json(object())); ResourceResponse updatedPolicyTwo = newResourceResponse("ID_1", "REVISION_1", json(object())); updatedPolicies.add(updatedPolicyOne); updatedPolicies.add(updatedPolicyTwo); Promise<ResourceResponse, ResourceException> updatePolicyOnePromise = Promises.newResultPromise(updatedPolicyOne); Promise<ResourceResponse, ResourceException> updatePolicyTwoPromise = Promises.newResultPromise(updatedPolicyTwo); given(policyResource.handleUpdate(eq(context), Matchers.<UpdateRequest>anyObject())) .willReturn(updatePolicyOnePromise) .willReturn(updatePolicyTwoPromise); List<ResourceResponse> returnedPolicies = delegate.updatePolicies(context, policies).getOrThrowUninterruptibly(); assertThat(returnedPolicies).isEqualTo(updatedPolicies); }
@Test(expectedExceptions = ResourceException.class) public void shouldHandleFailureToUpdatePolicies() throws ResourceException { Context context = mock(Context.class); Set<JsonValue> policies = new HashSet<JsonValue>(); JsonValue policyOne = json(object(field("name", "POLICY_ONE"))); JsonValue policyTwo = json(object(field("name", "POLICY_TWO"))); policies.add(policyOne); policies.add(policyTwo); ResourceResponse updatedPolicyOne = newResourceResponse("ID_1", "REVISION_1", json(object())); ResourceException exception = mock(ResourceException.class); Promise<ResourceResponse, ResourceException> updatePolicyOnePromise = Promises.newResultPromise(updatedPolicyOne); Promise<ResourceResponse, ResourceException> updatePolicyTwoPromise = Promises.newExceptionPromise(exception); given(policyResource.handleUpdate(eq(context), Matchers.<UpdateRequest>anyObject())) .willReturn(updatePolicyOnePromise) .willReturn(updatePolicyTwoPromise); delegate.updatePolicies(context, policies).getOrThrowUninterruptibly(); }