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;
}
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));
}
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);
}
};
}
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);
}
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");
}
Aggregations