### Question:
PolicyRequestFactory { public PolicyRequest buildRequest(final PolicyAction action, final Context context, final ActionRequest request) throws EntitlementException { Reject.ifNull(action); if (!builders.containsKey(action)) { throw new EntitlementException(EntitlementException.UNSUPPORTED_OPERATION); } return builders.get(action).buildRequest(context, request); } @Inject  PolicyRequestFactory(final SSOTokenManager tokenManager); PolicyRequest buildRequest(final PolicyAction action, final Context context,
                                      final ActionRequest request);  }### Answer:
@Test public void shouldRetrieveBatchRequest() throws EntitlementException { given(subjectContext.getCallerSubject()).willReturn(restSubject); Map<String, Object> properties = new HashMap<String, Object>(); properties.put("resources", Arrays.asList("/resource/a", "/resource/b")); given(actionRequest.getContent()).willReturn(JsonValue.json(properties)); Context context = buildContextStructure("/abc"); PolicyRequest request = factory.buildRequest(PolicyAction.EVALUATE, context, actionRequest); assertThat(request).isNotNull(); assertThat(request.getRealm()).isEqualTo("/abc"); assertThat(request).isInstanceOfAny(BatchPolicyRequest.class); BatchPolicyRequest batchRequest = (BatchPolicyRequest)request; assertThat(batchRequest.getResources()).containsOnly("/resource/a", "/resource/b"); verify(subjectContext).getCallerSubject(); verify(actionRequest, times(2)).getContent(); verifyNoMoreInteractions(subjectContext, actionRequest); }
@Test public void shouldRetrieveTreeRequest() throws EntitlementException { given(subjectContext.getCallerSubject()).willReturn(restSubject); Map<String, Object> properties = new HashMap<String, Object>(); properties.put("resource", "/resource/a"); given(actionRequest.getContent()).willReturn(JsonValue.json(properties)); Context context = buildContextStructure("/abc"); PolicyRequest request = factory.buildRequest(PolicyAction.TREE_EVALUATE, context, actionRequest); assertThat(request).isNotNull(); assertThat(request.getRealm()).isEqualTo("/abc"); assertThat(request).isInstanceOfAny(TreePolicyRequest.class); TreePolicyRequest treeRequest = (TreePolicyRequest)request; assertThat(treeRequest.getResource()).isEqualTo("/resource/a"); verify(subjectContext).getCallerSubject(); verify(actionRequest, times(2)).getContent(); verifyNoMoreInteractions(subjectContext, actionRequest); }
@Test(expectedExceptions = EntitlementException.class) public void shouldRejectUnsupportedAction() throws EntitlementException { Context context = buildContextStructure("/abc"); factory.buildRequest(PolicyAction.UNKNOWN, context, actionRequest); }
@Test(expectedExceptions = NullPointerException.class) public void shouldRejectNullAction() throws EntitlementException { Context context = buildContextStructure("/abc"); factory.buildRequest(null, context, actionRequest); }