Search in sources :

Example 86 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.

the class UmaPolicyServiceImplDelegationTest method mockPolicyResourceDelegateForNewPolicy.

private void mockPolicyResourceDelegateForNewPolicy() {
    final List<ResourceResponse> createdPolicies = new ArrayList<>();
    ResourceResponse createdPolicy1 = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectOnePolicyJson());
    ResourceResponse createdPolicy2 = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectTwoPolicyJson());
    createdPolicies.add(createdPolicy1);
    createdPolicies.add(createdPolicy2);
    Promise<List<ResourceResponse>, ResourceException> createPolicyPromise = newResultPromise(createdPolicies);
    given(policyResourceDelegate.createPolicies(any(Context.class), anySetOf(JsonValue.class))).willReturn(createPolicyPromise);
    Promise<Pair<QueryResponse, List<ResourceResponse>>, ResourceException> queryPromise = new NotFoundException().asPromise();
    given(policyResourceDelegate.queryPolicies(any(Context.class), any(QueryRequest.class))).willReturn(queryPromise);
    given(policyResourceDelegate.queryPolicies(any(Context.class), any(QueryRequest.class), any(QueryResourceHandler.class))).willAnswer(new Answer<Promise<QueryResponse, ResourceException>>() {

        @Override
        public Promise<QueryResponse, ResourceException> answer(InvocationOnMock invocation) throws Throwable {
            final PolicyGraph policyGraph = (PolicyGraph) invocation.getArguments()[2];
            for (ResourceResponse r : createdPolicies) {
                policyGraph.handleResource(r);
            }
            policyGraph.handleResult(newQueryResponse());
            return newResultPromise(newQueryResponse());
        }
    });
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Context(org.forgerock.services.context.Context) QueryRequest(org.forgerock.json.resource.QueryRequest) ArrayList(java.util.ArrayList) JsonValue(org.forgerock.json.JsonValue) NotFoundException(org.forgerock.json.resource.NotFoundException) Promise(org.forgerock.util.promise.Promise) ResourceResponse(org.forgerock.json.resource.ResourceResponse) InvocationOnMock(org.mockito.invocation.InvocationOnMock) List(java.util.List) ArrayList(java.util.ArrayList) ResourceException(org.forgerock.json.resource.ResourceException) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) Pair(org.forgerock.util.Pair)

Example 87 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.

the class UmaPolicyServiceImplDelegationTest method mockPolicyResourceDelegateForUpdatedPolicy.

private void mockPolicyResourceDelegateForUpdatedPolicy() {
    List<ResourceResponse> currentPolicies = new ArrayList<>();
    ResourceResponse currentPolicy1 = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectOnePolicyJson());
    ResourceResponse currentPolicy2 = newResourceResponse("ID_2", "REVISION_1", createBackendSubjectTwoPolicyJson());
    currentPolicies.add(currentPolicy1);
    currentPolicies.add(currentPolicy2);
    Promise<Pair<QueryResponse, List<ResourceResponse>>, ResourceException> queryPromise = newResultPromise(Pair.of((QueryResponse) null, currentPolicies));
    given(policyResourceDelegate.queryPolicies(any(Context.class), any(QueryRequest.class))).willReturn(queryPromise);
    final List<ResourceResponse> updatedPolicies = new ArrayList<>();
    ResourceResponse updatedPolicy1 = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectOnePolicyJson());
    ResourceResponse updatedPolicy3 = newResourceResponse("ID_3", "REVISION_1", createBackendSubjectTwoPolicyJson());
    updatedPolicies.add(updatedPolicy1);
    updatedPolicies.add(updatedPolicy3);
    Promise<List<ResourceResponse>, ResourceException> updatePolicyPromise = newResultPromise(updatedPolicies);
    given(policyResourceDelegate.updatePolicies(any(Context.class), Matchers.<Set<JsonValue>>anyObject())).willReturn(updatePolicyPromise);
    given(policyResourceDelegate.queryPolicies(any(Context.class), any(QueryRequest.class), any(QueryResourceHandler.class))).willAnswer(new Answer<Promise<QueryResponse, ResourceException>>() {

        @Override
        public Promise<QueryResponse, ResourceException> answer(InvocationOnMock invocation) throws Throwable {
            final PolicyGraph policyGraph = (PolicyGraph) invocation.getArguments()[2];
            for (ResourceResponse r : updatedPolicies) {
                policyGraph.handleResource(r);
            }
            policyGraph.handleResult(newQueryResponse());
            return newResultPromise(newQueryResponse());
        }
    });
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Context(org.forgerock.services.context.Context) QueryRequest(org.forgerock.json.resource.QueryRequest) ArrayList(java.util.ArrayList) JsonValue(org.forgerock.json.JsonValue) Promise(org.forgerock.util.promise.Promise) ResourceResponse(org.forgerock.json.resource.ResourceResponse) InvocationOnMock(org.mockito.invocation.InvocationOnMock) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) List(java.util.List) ArrayList(java.util.ArrayList) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) Pair(org.forgerock.util.Pair)

Example 88 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.

the class UmaLabelResourceTest method createInstance.

/**
     * Should successfully create an UMA Label.
     */
@Test
public void createInstance() throws ResourceException {
    //Given
    JsonValue umaLabel = json(object(field(NAME_ATTRIBUTE_NAME, LABEL_NAME), field(TYPE_ATTRIBUTE_NAME, LABEL_TYPE)));
    given(createRequest.getContent()).willReturn(umaLabel);
    given(contextHelper.getRealm(serverContext)).willReturn(REALM_NAME);
    given(contextHelper.getUserId(serverContext)).willReturn(RESOURCE_OWNER_ID);
    final ResourceSetLabel resourceSetLabel = new ResourceSetLabel(null, LABEL_NAME, LabelType.valueOf(LABEL_TYPE), Collections.EMPTY_SET);
    given(umaLabelsStore.create(REALM_NAME, RESOURCE_OWNER_ID, resourceSetLabel)).willReturn(resourceSetLabel);
    //When
    Promise<ResourceResponse, ResourceException> promise = umaLabelResource.createInstance(serverContext, createRequest);
    //Then
    verify(umaLabelsStore, Mockito.times(1)).create(REALM_NAME, RESOURCE_OWNER_ID, resourceSetLabel);
    assertThat(promise).succeeded();
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) ResourceSetLabel(org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel) Test(org.testng.annotations.Test)

Example 89 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.

the class UmaPolicyResourceTest method shouldSuccessfullyQueryPolicies.

@Test
public void shouldSuccessfullyQueryPolicies() {
    //Given
    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);
    //When
    policyResource.queryCollection(context, request, handler);
    //Then
    verify(handler, times(2)).handleResource(Matchers.<ResourceResponse>anyObject());
}
Also used : Context(org.forgerock.services.context.Context) QueryRequest(org.forgerock.json.resource.QueryRequest) ResourceResponse(org.forgerock.json.resource.ResourceResponse) Responses.newQueryResponse(org.forgerock.json.resource.Responses.newQueryResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) HashSet(java.util.HashSet) Pair(org.forgerock.util.Pair) Test(org.testng.annotations.Test)

Example 90 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.

the class UmaPolicyResourceTest method shouldSuccessfullyDeletePolicy.

@Test
@SuppressWarnings("unchecked")
public void shouldSuccessfullyDeletePolicy() {
    //Given
    Context context = mock(Context.class);
    DeleteRequest request = Requests.newDeleteRequest("/policies");
    Promise<Void, ResourceException> promise = Promises.newResultPromise(null);
    given(policyService.deletePolicy(context, "RESOURCE_SET_UID")).willReturn(promise);
    //When
    Promise<ResourceResponse, ResourceException> result = policyResource.deleteInstance(context, "RESOURCE_SET_UID", request);
    //Then
    assertThat(result).succeeded().withId().isEqualTo("RESOURCE_SET_UID");
    assertThat(result).succeeded().withRevision().isEqualTo("0");
    assertThat(result).succeeded().withContent().isEmpty();
}
Also used : Context(org.forgerock.services.context.Context) ResourceResponse(org.forgerock.json.resource.ResourceResponse) ResourceException(org.forgerock.json.resource.ResourceException) DeleteRequest(org.forgerock.json.resource.DeleteRequest) Test(org.testng.annotations.Test)

Aggregations

ResourceResponse (org.forgerock.json.resource.ResourceResponse)227 ResourceException (org.forgerock.json.resource.ResourceException)171 Test (org.testng.annotations.Test)154 JsonValue (org.forgerock.json.JsonValue)123 Context (org.forgerock.services.context.Context)83 RealmContext (org.forgerock.openam.rest.RealmContext)72 Subject (javax.security.auth.Subject)57 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)48 Responses.newResourceResponse (org.forgerock.json.resource.Responses.newResourceResponse)44 CreateRequest (org.forgerock.json.resource.CreateRequest)41 ClientContext (org.forgerock.services.context.ClientContext)41 ArrayList (java.util.ArrayList)38 EntitlementException (com.sun.identity.entitlement.EntitlementException)30 ReadRequest (org.forgerock.json.resource.ReadRequest)30 NotFoundException (org.forgerock.json.resource.NotFoundException)29 UpdateRequest (org.forgerock.json.resource.UpdateRequest)29 Matchers.anyString (org.mockito.Matchers.anyString)28 HashSet (java.util.HashSet)27 BadRequestException (org.forgerock.json.resource.BadRequestException)26 DeleteRequest (org.forgerock.json.resource.DeleteRequest)26