use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class DocumentMapperTest method testDocumentInformation.
@Test
public void testDocumentInformation() {
Task task = createTask(2, "sample task");
TestLinksInformation links = new TestLinksInformation();
links.value = "linksValue";
TestMetaInformation meta = new TestMetaInformation();
meta.value = "metaValue";
JsonApiResponse response = toResponse(task);
response.setMetaInformation(meta);
response.setLinksInformation(links);
Document document = mapper.toDocument(response, createAdapter(Task.class));
Assert.assertEquals("linksValue", getLinkText(document.getLinks().get("value")));
Assert.assertEquals("metaValue", document.getMeta().get("value").asText());
}
use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class DocumentMapperTest method testAttributesSelection.
@Test
public void testAttributesSelection() {
Task task = createTask(2, "sample task");
task.setCategory("sample category");
task.setProject(new Project());
JsonApiResponse response = new JsonApiResponse();
response.setEntity(task);
QuerySpec querySpec = new QuerySpec(Task.class);
querySpec.includeField(Arrays.asList("category"));
Document document = mapper.toDocument(response, toAdapter(querySpec));
Resource resource = document.getSingleData().get();
Assert.assertEquals("2", resource.getId());
Assert.assertEquals("tasks", resource.getType());
Assert.assertNull(resource.getAttributes().get("name"));
Assert.assertNull(resource.getRelationships().get("project"));
Assert.assertEquals("sample category", resource.getAttributes().get("category").asText());
}
use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class DocumentMapperTest method testErrors.
@Test
public void testErrors() {
JsonApiResponse response = new JsonApiResponse();
ErrorData error = Mockito.mock(ErrorData.class);
response.setErrors(Arrays.asList(error));
Document document = mapper.toDocument(response, createAdapter(Task.class));
List<ErrorData> errors = document.getErrors();
Assert.assertEquals(1, errors.size());
Assert.assertSame(error, errors.get(0));
}
use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class SleuthRepositoryFilter method filterRequest.
@Override
public JsonApiResponse filterRequest(RepositoryFilterContext context, RepositoryRequestFilterChain chain) {
RepositoryRequestSpec request = context.getRequest();
String query = SleuthUtil.getQuery(request, moduleContext.getResourceRegistry());
Span span = tracer.createSpan(SleuthUtil.getSpanName(request));
JsonApiResponse result = null;
Exception exception = null;
try {
span.tag("lc", COMPONENT_NAME);
result = chain.doFilter(context);
return result;
} catch (RuntimeException e) {
exception = e;
throw e;
} finally {
boolean resultError = result != null && result.getErrors() != null && result.getErrors().iterator().hasNext();
boolean failed = exception != null || resultError;
String status = failed ? STRING_EXCEPTION : STRING_OK;
span.tag(STATUS_CODE_ANNOTATION, status);
writeQuery(span, query);
writeResults(span, result);
tracer.close(span);
}
}
use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class ResourceRepositoryStubImpl method executeUpdate.
private Object executeUpdate(String requestUrl, T resource, boolean create) {
JsonApiResponse response = new JsonApiResponse();
response.setEntity(resource);
ClientDocumentMapper documentMapper = client.getDocumentMapper();
final Document requestDocument = documentMapper.toDocument(response, null);
final ObjectMapper objectMapper = client.getObjectMapper();
String requestBodyValue = ExceptionUtil.wrapCatchedExceptions(new Callable<String>() {
@Override
public String call() throws Exception {
return objectMapper.writeValueAsString(requestDocument);
}
});
HttpMethod method = create || client.getPushAlways() ? HttpMethod.POST : HttpMethod.PATCH;
return execute(requestUrl, ResponseType.RESOURCE, method, requestBodyValue);
}
Aggregations