Search in sources :

Example 31 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class UmaPolicyServiceImplTest method mockBackendQuery.

private void mockBackendQuery(Context context, JsonValue... policies) {
    QueryResponse queryResult = newQueryResponse();
    List<ResourceResponse> policyResources = new ArrayList<>();
    for (JsonValue policy : policies) {
        policyResources.add(newResourceResponse("ID_1", "REVISION_1", policy));
    }
    Promise<Pair<QueryResponse, List<ResourceResponse>>, ResourceException> backendQueryPromise = newResultPromise(Pair.of(queryResult, policyResources));
    given(policyResourceDelegate.queryPolicies(eq(context), Matchers.<QueryRequest>anyObject())).willReturn(backendQueryPromise);
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) ArrayList(java.util.ArrayList) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) Pair(org.forgerock.util.Pair)

Example 32 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class UmaPolicyServiceImplTest method shouldHandleFailureToCreateUnderlyingPolicies.

@Test(expectedExceptions = ResourceException.class)
public void shouldHandleFailureToCreateUnderlyingPolicies() throws Exception {
    //Given
    Context context = createContext();
    JsonValue policy = createUmaPolicyJson("RESOURCE_SET_ID");
    ResourceException exception = mock(ResourceException.class);
    Promise<Pair<QueryResponse, List<ResourceResponse>>, ResourceException> queryPromise = Promises.newExceptionPromise((ResourceException) new NotFoundException());
    Promise<List<ResourceResponse>, ResourceException> createPoliciesPromise = Promises.newExceptionPromise(exception);
    given(policyResourceDelegate.queryPolicies(eq(context), Matchers.<QueryRequest>anyObject())).willReturn(queryPromise);
    given(policyResourceDelegate.createPolicies(eq(context), Matchers.<Set<JsonValue>>anyObject())).willReturn(createPoliciesPromise);
    //When
    policyService.createPolicy(context, policy).getOrThrowUninterruptibly();
//Then
//Expected ResourceException
}
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) ResourceResponse(org.forgerock.json.resource.ResourceResponse) JsonValue(org.forgerock.json.JsonValue) NotFoundException(org.forgerock.json.resource.NotFoundException) ResourceException(org.forgerock.json.resource.ResourceException) List(java.util.List) ArrayList(java.util.ArrayList) Pair(org.forgerock.util.Pair) Test(org.testng.annotations.Test)

Example 33 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class UmaPolicyServiceImplTest method shouldCreateUmaPolicy.

@Test
@SuppressWarnings("unchecked")
public void shouldCreateUmaPolicy() throws Exception {
    //Given
    Context context = createContext();
    JsonValue policy = createUmaPolicyJson("RESOURCE_SET_ID");
    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<Pair<QueryResponse, List<ResourceResponse>>, ResourceException> queryPromise = Promises.newExceptionPromise((ResourceException) new NotFoundException());
    setupQueries(queryPromise, createdPolicy1, createdPolicy2);
    Promise<List<ResourceResponse>, ResourceException> createPolicyPromise = newResultPromise(createdPolicies);
    given(policyResourceDelegate.createPolicies(eq(context), Matchers.<Set<JsonValue>>anyObject())).willReturn(createPolicyPromise);
    //When
    UmaPolicy umaPolicy = policyService.createPolicy(context, policy).getOrThrowUninterruptibly();
    //Then
    InOrder inOrder = inOrder(resourceDelegationFilter, policyResourceDelegate, resourceDelegationFilter);
    inOrder.verify(resourceDelegationFilter).beforeResourceShared(any(UmaPolicy.class));
    inOrder.verify(policyResourceDelegate).createPolicies(eq(context), anySetOf(JsonValue.class));
    inOrder.verify(resourceDelegationFilter).afterResourceShared(any(UmaPolicy.class));
    assertThat(umaPolicy.getId()).isEqualTo("RESOURCE_SET_ID");
    assertThat(umaPolicy.getRevision()).isNotNull();
    assertThat(umaPolicy.asJson().asMap()).hasSize(3).contains(entry("policyId", "RESOURCE_SET_ID"), entry("name", "NAME"));
    JsonValue permissions = umaPolicy.asJson().get("permissions");
    assertThat(permissions.asList()).hasSize(2);
    assertThat(permissions.get(0).asMap()).contains(entry("subject", "SUBJECT_ONE"));
    assertThat(permissions.get(0).get("scopes").asList()).containsOnly("SCOPE_A", "SCOPE_B");
    assertThat(permissions.get(1).asMap()).contains(entry("subject", "SUBJECT_TWO"));
    assertThat(permissions.get(1).get("scopes").asList()).containsOnly("SCOPE_A");
}
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) InOrder(org.mockito.InOrder) JsonValue(org.forgerock.json.JsonValue) ArrayList(java.util.ArrayList) NotFoundException(org.forgerock.json.resource.NotFoundException) ResourceResponse(org.forgerock.json.resource.ResourceResponse) ResourceException(org.forgerock.json.resource.ResourceException) List(java.util.List) ArrayList(java.util.ArrayList) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) Pair(org.forgerock.util.Pair) Test(org.testng.annotations.Test)

Example 34 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class UmaPolicyServiceImplTest method shouldNotCreateUmaPolicyIfAlreadyExists.

@Test(expectedExceptions = ConflictException.class)
@SuppressWarnings("unchecked")
public void shouldNotCreateUmaPolicyIfAlreadyExists() throws Exception {
    //Given
    Context context = createContext();
    JsonValue policy = createUmaPolicyJson("RESOURCE_SET_ID");
    ResourceResponse policyResource = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectOnePolicyJson());
    Promise<Pair<QueryResponse, List<ResourceResponse>>, ResourceException> queryPromise = newResultPromise(Pair.of(newQueryResponse(), Collections.singletonList(policyResource)));
    given(policyResourceDelegate.queryPolicies(eq(context), Matchers.<QueryRequest>anyObject())).willReturn(queryPromise);
    //When
    try {
        policyService.createPolicy(context, policy).getOrThrowUninterruptibly();
    } catch (ResourceException e) {
        //Then
        verify(policyResourceDelegate, never()).createPolicies(eq(context), anySetOf(JsonValue.class));
        throw e;
    }
}
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) ResourceResponse(org.forgerock.json.resource.ResourceResponse) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) Pair(org.forgerock.util.Pair) Test(org.testng.annotations.Test)

Example 35 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class UmaLabelResourceTest method createInstanceFailsWhenTypePropertyIsMissing.

@Test
public void createInstanceFailsWhenTypePropertyIsMissing() {
    //Given
    JsonValue umaLabel = json(object(field(NAME_ATTRIBUTE_NAME, LABEL_NAME)));
    given(createRequest.getContent()).willReturn(umaLabel);
    given(contextHelper.getRealm(serverContext)).willReturn(REALM_NAME);
    given(contextHelper.getUserId(serverContext)).willReturn(RESOURCE_OWNER_ID);
    //When
    Promise<ResourceResponse, ResourceException> promise = umaLabelResource.createInstance(serverContext, createRequest);
    //Then
    verifyZeroInteractions(umaLabelsStore);
    assertThat(promise).failedWithResourceException().isInstanceOf(BadRequestException.class);
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) Test(org.testng.annotations.Test)

Aggregations

JsonValue (org.forgerock.json.JsonValue)575 Test (org.testng.annotations.Test)333 ResourceException (org.forgerock.json.resource.ResourceException)144 ResourceResponse (org.forgerock.json.resource.ResourceResponse)123 RealmContext (org.forgerock.openam.rest.RealmContext)70 Context (org.forgerock.services.context.Context)63 HashSet (java.util.HashSet)56 SSOException (com.iplanet.sso.SSOException)54 ArrayList (java.util.ArrayList)51 BadRequestException (org.forgerock.json.resource.BadRequestException)47 Privilege (com.sun.identity.entitlement.Privilege)46 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)46 SSOToken (com.iplanet.sso.SSOToken)43 SMSException (com.sun.identity.sm.SMSException)42 HashMap (java.util.HashMap)42 NotFoundException (org.forgerock.json.resource.NotFoundException)41 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)41 CreateRequest (org.forgerock.json.resource.CreateRequest)40 OpenSSOPrivilege (com.sun.identity.entitlement.opensso.OpenSSOPrivilege)39 Subject (javax.security.auth.Subject)32