Search in sources :

Example 1 with JsonSchema

use of com.fasterxml.jackson.databind.jsonschema.JsonSchema in project OpenAM by OpenRock.

the class SubjectTypesResourceTest method testSuccessfulJsonificationAndQuery.

@Test
public void testSuccessfulJsonificationAndQuery() throws Exception {
    //given
    SSOTokenContext mockSubjectContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(mockSubjectContext);
    Context mockServerContext = ClientContext.newInternalClientContext(realmContext);
    Subject mockSubject = new Subject();
    given(mockSubjectContext.getCallerSubject()).willReturn(mockSubject);
    QueryRequest mockRequest = mock(QueryRequest.class);
    JsonSchema mockSchema = mock(JsonSchema.class);
    QueryResourceHandler mockHandler = mock(QueryResourceHandler.class);
    given(mockRequest.getPageSize()).willReturn(2);
    given(mockHandler.handleResource(any(ResourceResponse.class))).willReturn(true);
    given(mockMapper.generateJsonSchema((Class<?>) any(Class.class))).willReturn(mockSchema);
    //when
    Promise<QueryResponse, ResourceException> promise = testResource.queryCollection(mockServerContext, mockRequest, mockHandler);
    //then
    assertThat(promise).succeeded();
    verify(mockHandler, times(2)).handleResource(any(ResourceResponse.class));
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) RealmContext(org.forgerock.openam.rest.RealmContext) QueryRequest(org.forgerock.json.resource.QueryRequest) ResourceResponse(org.forgerock.json.resource.ResourceResponse) JsonSchema(com.fasterxml.jackson.databind.jsonschema.JsonSchema) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) EntitlementSubject(com.sun.identity.entitlement.EntitlementSubject) Subject(javax.security.auth.Subject) LogicalSubject(com.sun.identity.entitlement.LogicalSubject) Test(org.testng.annotations.Test)

Example 2 with JsonSchema

use of com.fasterxml.jackson.databind.jsonschema.JsonSchema in project OpenAM by OpenRock.

the class ConditionTypesResourceTest method testSuccessfulJsonificationAndReadAndNamePropertyRemoved.

@Test
public void testSuccessfulJsonificationAndReadAndNamePropertyRemoved() throws JsonMappingException, ExecutionException, InterruptedException {
    //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, TEST_CONDITION_WITH_NAME, mockRequest);
    //then
    Map resultMap = result.get().getContent().asMap();
    assertThat(resultMap.containsKey("title")).isTrue();
    assertThat(resultMap.containsKey("config")).isTrue();
    assertThat(resultMap.containsKey("logical")).isTrue();
    assertThat(resultMap.get("title")).isEqualTo(TEST_CONDITION_WITH_NAME);
    assertThat(resultMap.get("logical")).isEqualTo(false);
    assertThat(resultMap.get("config")).isInstanceOf(JsonSchema.class);
    JsonSchema resultSchema = (JsonSchema) resultMap.get("config");
    assertThat(resultSchema.toString().equals("{\"type\":\"object\",\"properties\":{}}")).isTrue();
}
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) Map(java.util.Map) Subject(javax.security.auth.Subject) ReadRequest(org.forgerock.json.resource.ReadRequest) Test(org.testng.annotations.Test)

Example 3 with JsonSchema

use of com.fasterxml.jackson.databind.jsonschema.JsonSchema in project OpenAM by OpenRock.

the class ConditionTypesResourceTest method testSuccessfulJsonificationAndLogicalIsCorrect.

@Test
public void testSuccessfulJsonificationAndLogicalIsCorrect() throws JsonMappingException, ExecutionException, InterruptedException {
    //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, TEST_LOGICAL_CONDITION, mockRequest);
    //then
    Map resultMap = result.get().getContent().asMap();
    assertThat(resultMap.containsKey("logical")).isTrue();
    assertThat(resultMap.get("logical")).isEqualTo(true);
}
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) Map(java.util.Map) Subject(javax.security.auth.Subject) ReadRequest(org.forgerock.json.resource.ReadRequest) Test(org.testng.annotations.Test)

Example 4 with JsonSchema

use of com.fasterxml.jackson.databind.jsonschema.JsonSchema in project OpenAM by OpenRock.

the class DecisionCombinersResourceTest 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)

Example 5 with JsonSchema

use of com.fasterxml.jackson.databind.jsonschema.JsonSchema in project OpenAM by OpenRock.

the class SubjectTypesResource method jsonify.

/**
     * Transforms a subclass of {@link EntitlementSubject} in to a JsonSchema representation.
     * This schema is then combined with the Subject's name (taken as the resourceId) and all this is
     * compiled together into a new {@link JsonValue} object until "title" and "config" fields respectively.
     *
     * @param subjectClass The class whose schema to produce.
     * @param resourceId The ID of the resource to return
     * @return A JsonValue containing the schema of the EntitlementSubject
     */
private JsonValue jsonify(Class<? extends EntitlementSubject> subjectClass, String resourceId, boolean logical) {
    try {
        final JsonSchema schema = mapper.generateJsonSchema(subjectClass);
        //this will remove the 'subjectName' attribute from those subjects which incorporate it unnecessarily
        final JsonNode node = schema.getSchemaNode().get("properties");
        if (node instanceof ObjectNode) {
            final ObjectNode alter = (ObjectNode) node;
            alter.remove("subjectName");
        }
        return JsonValue.json(JsonValue.object(JsonValue.field(JSON_OBJ_TITLE, resourceId), JsonValue.field(JSON_OBJ_LOGICAL, logical), JsonValue.field(JSON_OBJ_CONFIG, schema)));
    } catch (JsonMappingException e) {
        if (debug.errorEnabled()) {
            debug.error("SubjectTypesResource :: JSONIFY - Error applying " + "jsonification to the Subject class representation.", e);
        }
        return null;
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonSchema(com.fasterxml.jackson.databind.jsonschema.JsonSchema) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

JsonSchema (com.fasterxml.jackson.databind.jsonschema.JsonSchema)14 Subject (javax.security.auth.Subject)11 ResourceResponse (org.forgerock.json.resource.ResourceResponse)11 RealmContext (org.forgerock.openam.rest.RealmContext)11 ClientContext (org.forgerock.services.context.ClientContext)11 Context (org.forgerock.services.context.Context)11 Test (org.testng.annotations.Test)11 ResourceException (org.forgerock.json.resource.ResourceException)9 ReadRequest (org.forgerock.json.resource.ReadRequest)8 SubjectContext (org.forgerock.openam.rest.resource.SubjectContext)7 EntitlementSubject (com.sun.identity.entitlement.EntitlementSubject)4 LogicalSubject (com.sun.identity.entitlement.LogicalSubject)4 QueryResourceHandler (org.forgerock.json.resource.QueryResourceHandler)4 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)4 Map (java.util.Map)3 QueryRequest (org.forgerock.json.resource.QueryRequest)3 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 QueryResponse (org.forgerock.json.resource.QueryResponse)1