Search in sources :

Example 16 with ReadRequest

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

the class UmaPolicyResourceTest method shouldHandledFailedReadPolicy.

@Test
@SuppressWarnings("unchecked")
public void shouldHandledFailedReadPolicy() {
    //Given
    Context context = mock(Context.class);
    ReadRequest request = Requests.newReadRequest("/policies");
    ResourceException resourceException = mock(ResourceException.class);
    Promise<UmaPolicy, ResourceException> promise = Promises.newExceptionPromise(resourceException);
    given(policyService.readPolicy(context, "RESOURCE_SET_UID")).willReturn(promise);
    //When
    Promise<ResourceResponse, ResourceException> result = policyResource.readInstance(context, "RESOURCE_SET_UID", request);
    //Then
    assertThat(result).failedWithResourceException().isEqualTo(resourceException);
}
Also used : Context(org.forgerock.services.context.Context) ResourceResponse(org.forgerock.json.resource.ResourceResponse) ResourceException(org.forgerock.json.resource.ResourceException) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) ReadRequest(org.forgerock.json.resource.ReadRequest) Test(org.testng.annotations.Test)

Example 17 with ReadRequest

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

the class PolicyResourceWithCopyMoveSupport method copyPolicy.

private ActionResponse copyPolicy(Context context, String resourceId, ActionRequest request) throws ResourceException {
    String sourceRealm = RealmContext.getRealm(context);
    JsonValue payload = request.getContent().get("to");
    if (payload.isNull()) {
        throw new BadRequestException("to definition is missing");
    }
    String destinationRealm = payload.get("realm").defaultTo(sourceRealm).asString();
    ReadRequest readRequest = Requests.newReadRequest("policies", resourceId);
    JsonValue policy = router.handleRead(context, readRequest).getOrThrowUninterruptibly().getContent();
    String sourceApplication = policy.get("applicationName").asString();
    String sourceResourceType = policy.get("resourceTypeUuid").asString();
    String destinationApplication = payload.get("application").defaultTo(sourceApplication).asString();
    String destinationResourceTypeId = payload.get("resourceType").defaultTo(sourceResourceType).asString();
    String copiedName = payload.get("name").defaultTo(resourceId).asString();
    if (sourceRealm.equals(destinationRealm) && resourceId.equals(copiedName)) {
        throw new BadRequestException("policy name already exists within the realm");
    }
    policy.put("name", copiedName);
    policy.put("applicationName", destinationApplication);
    policy.put("resourceTypeUuid", destinationResourceTypeId);
    RealmContext updatedContext = new RealmContext(context);
    updatedContext.setOverrideRealm(destinationRealm);
    CreateRequest createRequest = Requests.newCreateRequest("policies", policy);
    JsonValue copiedPolicy = router.handleCreate(updatedContext, createRequest).getOrThrowUninterruptibly().getContent();
    return Responses.newActionResponse(copiedPolicy);
}
Also used : RealmContext(org.forgerock.openam.rest.RealmContext) CreateRequest(org.forgerock.json.resource.CreateRequest) JsonValue(org.forgerock.json.JsonValue) BadRequestException(org.forgerock.json.resource.BadRequestException) ReadRequest(org.forgerock.json.resource.ReadRequest)

Example 18 with ReadRequest

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

the class ApplicationTypesResourceTest method undefinedSubjectShouldFail.

@Test(expectedExceptions = InternalServerErrorException.class)
public void undefinedSubjectShouldFail() throws ResourceException {
    //given
    SSOTokenContext mockSubjectContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(mockSubjectContext);
    Context mockServerContext = ClientContext.newInternalClientContext(realmContext);
    Subject subject = null;
    given(mockSubjectContext.getCallerSubject()).willReturn(subject);
    ReadRequest request = mock(ReadRequest.class);
    //when
    Promise<ResourceResponse, ResourceException> result = testResource.readInstance(mockServerContext, "test", request);
    result.getOrThrowUninterruptibly();
}
Also used : Context(org.forgerock.services.context.Context) ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) RealmContext(org.forgerock.openam.rest.RealmContext) ResourceResponse(org.forgerock.json.resource.ResourceResponse) ResourceException(org.forgerock.json.resource.ResourceException) Subject(javax.security.auth.Subject) ReadRequest(org.forgerock.json.resource.ReadRequest) Test(org.testng.annotations.Test)

Example 19 with ReadRequest

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

the class ApplicationTypesResourceTest method readShouldFailOnInvalidApplicationType.

@Test(expectedExceptions = NotFoundException.class)
public void readShouldFailOnInvalidApplicationType() throws ResourceException {
    //given
    SSOTokenContext mockSubjectContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(mockSubjectContext);
    Context mockServerContext = ClientContext.newInternalClientContext(realmContext);
    Subject subject = new Subject();
    given(mockSubjectContext.getCallerSubject()).willReturn(subject);
    ReadRequest request = mock(ReadRequest.class);
    //when
    Promise<ResourceResponse, ResourceException> result = testResource.readInstance(mockServerContext, "test", request);
    //then
    result.getOrThrowUninterruptibly();
}
Also used : Context(org.forgerock.services.context.Context) ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) RealmContext(org.forgerock.openam.rest.RealmContext) ResourceResponse(org.forgerock.json.resource.ResourceResponse) ResourceException(org.forgerock.json.resource.ResourceException) Subject(javax.security.auth.Subject) ReadRequest(org.forgerock.json.resource.ReadRequest) Test(org.testng.annotations.Test)

Example 20 with ReadRequest

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

the class ConditionTypesResourceTest method shouldThrowErrorWthInvalidCondition.

@Test(expectedExceptions = NotFoundException.class)
public void shouldThrowErrorWthInvalidCondition() throws JsonMappingException, ResourceException {
    //given
    SubjectContext mockSubjectContext = mock(SubjectContext.class);
    RealmContext realmContext = new RealmContext(mockSubjectContext);
    Context mockServerContext = ClientContext.newInternalClientContext(realmContext);
    Subject mockSubject = new Subject();
    given(mockSubjectContext.getCallerSubject()).willReturn(mockSubject);
    ReadRequest mockRequest = mock(ReadRequest.class);
    JsonSchema mockSchema = mock(JsonSchema.class);
    given(mockMapper.generateJsonSchema((Class<?>) any(Class.class))).willReturn(mockSchema);
    //when
    Promise<ResourceResponse, ResourceException> result = testResource.readInstance(mockServerContext, "invalidCondition", mockRequest);
    //then
    result.getOrThrowUninterruptibly();
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) RealmContext(org.forgerock.openam.rest.RealmContext) ResourceResponse(org.forgerock.json.resource.ResourceResponse) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) JsonSchema(com.fasterxml.jackson.databind.jsonschema.JsonSchema) ResourceException(org.forgerock.json.resource.ResourceException) Subject(javax.security.auth.Subject) ReadRequest(org.forgerock.json.resource.ReadRequest) Test(org.testng.annotations.Test)

Aggregations

ReadRequest (org.forgerock.json.resource.ReadRequest)35 Test (org.testng.annotations.Test)31 ResourceException (org.forgerock.json.resource.ResourceException)28 ResourceResponse (org.forgerock.json.resource.ResourceResponse)27 Context (org.forgerock.services.context.Context)23 RealmContext (org.forgerock.openam.rest.RealmContext)15 Subject (javax.security.auth.Subject)13 ClientContext (org.forgerock.services.context.ClientContext)11 JsonSchema (com.fasterxml.jackson.databind.jsonschema.JsonSchema)8 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)7 JsonValue (org.forgerock.json.JsonValue)6 SubjectContext (org.forgerock.openam.rest.resource.SubjectContext)6 RequestHandler (org.forgerock.json.resource.RequestHandler)4 EntitlementSubject (com.sun.identity.entitlement.EntitlementSubject)3 LogicalSubject (com.sun.identity.entitlement.LogicalSubject)3 Map (java.util.Map)3 AuthorizationResult (org.forgerock.authz.filter.api.AuthorizationResult)3 Router (org.forgerock.json.resource.Router)3 EntitlementException (com.sun.identity.entitlement.EntitlementException)2 CreateRequest (org.forgerock.json.resource.CreateRequest)2