Search in sources :

Example 11 with JsonRepresentation

use of org.restlet.ext.json.JsonRepresentation in project OpenAM by OpenRock.

the class ResourceSetRegistrationEndpointTest method createUpdateRequestRepresentation.

private JsonRepresentation createUpdateRequestRepresentation() throws JSONException, JsonProcessingException, BadRequestException {
    JsonRepresentation entity = mock(JsonRepresentation.class);
    JSONObject jsonObject = mock(JSONObject.class);
    String jsonString = new ObjectMapper().writeValueAsString(RESOURCE_SET_DESCRIPTION_UPDATED_CONTENT.asMap());
    given(entity.getJsonObject()).willReturn(jsonObject);
    given(jsonObject.toString()).willReturn(jsonString);
    given(validator.validate(anyMapOf(String.class, Object.class))).willReturn(RESOURCE_SET_DESCRIPTION_UPDATED_CONTENT.asMap());
    return entity;
}
Also used : JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 12 with JsonRepresentation

use of org.restlet.ext.json.JsonRepresentation in project OpenAM by OpenRock.

the class ResourceSetRegistrationEndpointTest method shouldCreateResourceSetDescription.

@Test
@SuppressWarnings("unchecked")
public void shouldCreateResourceSetDescription() throws Exception {
    //Given
    JsonRepresentation entity = createCreateRequestRepresentation();
    doAnswer(new Answer<Void>() {

        public Void answer(InvocationOnMock invocation) throws Throwable {
            ResourceSetDescription resourceSetDescription = (ResourceSetDescription) invocation.getArguments()[1];
            resourceSetDescription.setId("123");
            return null;
        }
    }).when(store).create(any(OAuth2Request.class), any(ResourceSetDescription.class));
    setUriResourceSetId();
    noConditions();
    //When
    Representation response = endpoint.createResourceSet(entity);
    //Then
    ArgumentCaptor<ResourceSetDescription> resourceSetCaptor = ArgumentCaptor.forClass(ResourceSetDescription.class);
    InOrder inOrder = inOrder(resourceRegistrationFilter, store, resourceRegistrationFilter);
    inOrder.verify(resourceRegistrationFilter).beforeResourceRegistration(any(ResourceSetDescription.class));
    inOrder.verify(store).create(Matchers.<OAuth2Request>anyObject(), resourceSetCaptor.capture());
    inOrder.verify(resourceRegistrationFilter).afterResourceRegistration(any(ResourceSetDescription.class));
    assertThat(resourceSetCaptor.getValue().getId()).isNotNull().isNotEmpty();
    assertThat(resourceSetCaptor.getValue().getClientId()).isEqualTo("CLIENT_ID");
    assertThat(resourceSetCaptor.getValue().getName()).isEqualTo("NAME");
    assertThat(resourceSetCaptor.getValue().getUri()).isEqualTo(URI.create("URI"));
    assertThat(resourceSetCaptor.getValue().getType()).isEqualTo("TYPE");
    assertThat(resourceSetCaptor.getValue().getScopes()).containsExactly("SCOPE");
    assertThat(resourceSetCaptor.getValue().getIconUri()).isEqualTo(URI.create("ICON_URI"));
    Map<String, Object> responseBody = (Map<String, Object>) new ObjectMapper().readValue(response.getText(), Map.class);
    assertThat(responseBody).containsKey("_id");
    verify(hook).resourceSetCreated(anyString(), Matchers.<ResourceSetDescription>anyObject());
    verify(labelRegistration).updateLabelsForNewResourceSet(any(ResourceSetDescription.class));
}
Also used : InOrder(org.mockito.InOrder) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Representation(org.restlet.representation.Representation) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) InvocationOnMock(org.mockito.invocation.InvocationOnMock) JSONObject(org.json.JSONObject) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 13 with JsonRepresentation

use of org.restlet.ext.json.JsonRepresentation in project OpenAM by OpenRock.

the class RestletBodyAuditor method jsonAuditor.

/**
     * Create a body auditor for JSON bodies.
     * @param fields The fields that should be captured if they exist.
     * @return The auditor object.
     */
public static RestletBodyAuditor jsonAuditor(String... fields) {
    return new RestletBodyAuditor<JSONObject>(fields) {

        @Override
        public JsonValue apply(Representation representation) throws AuditException {
            try {
                boolean isBufferingRepresentation = (representation instanceof BufferingRepresentation);
                boolean isEmptyBufferingRepresentation = isBufferingRepresentation && ((BufferingRepresentation) representation).getWrappedRepresentation().isEmpty();
                if (isEmptyBufferingRepresentation || (!isBufferingRepresentation && representation.isEmpty())) {
                    return json(object());
                }
                return extractValues(new JsonRepresentation(representation).getJsonObject());
            } catch (IOException | JSONException e) {
                throw new AuditException("Could not parse body as JSON - wrong body auditor?", e);
            }
        }

        @Override
        Object getValue(String field, JSONObject object) throws AuditException {
            return object.opt(field);
        }
    };
}
Also used : BufferingRepresentation(org.restlet.representation.BufferingRepresentation) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) AuditException(org.forgerock.audit.AuditException) BufferingRepresentation(org.restlet.representation.BufferingRepresentation) Representation(org.restlet.representation.Representation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) IOException(java.io.IOException) JsonRepresentation(org.restlet.ext.json.JsonRepresentation)

Example 14 with JsonRepresentation

use of org.restlet.ext.json.JsonRepresentation in project OpenAM by OpenRock.

the class AbstractRestletAccessAuditFilterTest method shouldCaptureResponseBodyProperties.

@Test
public void shouldCaptureResponseBodyProperties() throws Exception {
    // Given
    auditFilter = new RestletAccessAuditFilterTest(restlet, eventPublisher, eventFactory, RestletBodyAuditor.jsonAuditor("fred"), RestletBodyAuditor.jsonAuditor("gary"));
    Request request = new Request();
    request.setDate(new Date());
    Response response = new Response(request);
    response.setEntity(new JsonRepresentation((Map<String, Object>) object(field("fred", "v"), field("gary", 7))));
    when(eventPublisher.isAuditing(anyString(), anyString(), any(EventName.class))).thenReturn(true);
    // When
    auditFilter.afterHandle(request, response);
    // Then
    ArgumentCaptor<AuditEvent> captor = ArgumentCaptor.forClass(AuditEvent.class);
    verify(eventPublisher).tryPublish(anyString(), captor.capture());
    assertThat(captor.getValue().getValue()).isObject().hasObject("response").hasObject("detail").contains("gary", 7);
}
Also used : Response(org.restlet.Response) Request(org.restlet.Request) AuditEvent(org.forgerock.audit.events.AuditEvent) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 15 with JsonRepresentation

use of org.restlet.ext.json.JsonRepresentation in project OpenAM by OpenRock.

the class AbstractRestletAccessAuditFilterTest method shouldCaptureRequestBodyProperties.

@Test
public void shouldCaptureRequestBodyProperties() throws Exception {
    // Given
    auditFilter = new RestletAccessAuditFilterTest(restlet, eventPublisher, eventFactory, RestletBodyAuditor.jsonAuditor("fred"), RestletBodyAuditor.jsonAuditor("gary"));
    Request request = new Request();
    request.setDate(new Date());
    Response response = new Response(request);
    request.setEntity(new JsonRepresentation((Map<String, Object>) object(field("fred", "v"), field("gary", 7))));
    when(eventPublisher.isAuditing(anyString(), anyString(), any(EventName.class))).thenReturn(true);
    // When
    auditFilter.beforeHandle(request, response);
    // Then
    ArgumentCaptor<AuditEvent> captor = ArgumentCaptor.forClass(AuditEvent.class);
    verify(eventPublisher).tryPublish(anyString(), captor.capture());
    assertThat(captor.getValue().getValue()).isObject().hasObject("request").hasObject("detail").contains("fred", "v");
}
Also used : Response(org.restlet.Response) Request(org.restlet.Request) AuditEvent(org.forgerock.audit.events.AuditEvent) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Date(java.util.Date) Test(org.testng.annotations.Test)

Aggregations

JsonRepresentation (org.restlet.ext.json.JsonRepresentation)22 JSONObject (org.json.JSONObject)16 Test (org.testng.annotations.Test)13 ResourceSetDescription (org.forgerock.oauth2.resources.ResourceSetDescription)7 Representation (org.restlet.representation.Representation)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 Map (java.util.Map)5 JacksonRepresentation (org.restlet.ext.jackson.JacksonRepresentation)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)4 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 Date (java.util.Date)2 AuditEvent (org.forgerock.audit.events.AuditEvent)2 JsonValue (org.forgerock.json.JsonValue)2 OAuth2Exception (org.forgerock.oauth2.core.exceptions.OAuth2Exception)2 OAuth2RestletException (org.forgerock.oauth2.restlet.OAuth2RestletException)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 TextMatch (org.opensextant.extraction.TextMatch)2