use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class RegistryEntry method toResource.
private Object toResource(JsonApiResponse response) {
if (response.getErrors() != null && response.getErrors().iterator().hasNext()) {
List<ErrorData> errorList = new ArrayList<>();
response.getErrors().forEach(it -> errorList.add(it));
Optional<Integer> errorCode = errorList.stream().filter(it -> it.getStatus() != null).map(it -> Integer.parseInt(it.getStatus())).collect(Collectors.maxBy(Integer::compare));
ErrorResponse errorResponse = new ErrorResponse(errorList, errorCode.get());
ExceptionMapperRegistry exceptionMapperRegistry = moduleRegistry.getExceptionMapperRegistry();
ExceptionMapper<Throwable> exceptionMapper = exceptionMapperRegistry.findMapperFor(errorResponse).get();
return exceptionMapper.fromErrorResponse(errorResponse);
}
return response.getEntity();
}
use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class HasNextBasedPagedLinksInformationTest method testPagingLast.
@Test
public void testPagingLast() throws InstantiationException, IllegalAccessException {
QuerySpecAdapter querySpec = new QuerySpecAdapter(querySpec(4L, 4L), resourceRegistry);
JsonApiResponse results = adapter.findAll(querySpec);
HasMoreResourcesMetaInformation metaInformation = (HasMoreResourcesMetaInformation) results.getMetaInformation();
Assert.assertFalse(metaInformation.getHasMoreResources());
PagedLinksInformation linksInformation = (PagedLinksInformation) results.getLinksInformation();
Assert.assertEquals("http://127.0.0.1/tasks?page[limit]=4", linksInformation.getFirst());
Assert.assertNull(linksInformation.getLast());
Assert.assertEquals("http://127.0.0.1/tasks?page[limit]=4", linksInformation.getFirst());
Assert.assertNull(linksInformation.getNext());
}
use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class HasNextBasedPagedLinksInformationTest method testPagingFirst.
@Test
public void testPagingFirst() throws InstantiationException, IllegalAccessException {
QuerySpecAdapter querySpec = new QuerySpecAdapter(querySpec(0L, 3L), resourceRegistry);
JsonApiResponse results = adapter.findAll(querySpec);
HasMoreResourcesMetaInformation metaInformation = (HasMoreResourcesMetaInformation) results.getMetaInformation();
Assert.assertTrue(metaInformation.getHasMoreResources());
PagedLinksInformation linksInformation = (PagedLinksInformation) results.getLinksInformation();
Assert.assertEquals("http://127.0.0.1/tasks?page[limit]=3", linksInformation.getFirst());
Assert.assertNull(linksInformation.getLast());
Assert.assertNull(linksInformation.getPrev());
Assert.assertEquals("http://127.0.0.1/tasks?page[limit]=3&page[offset]=3", linksInformation.getNext());
}
use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class QuerySpecRepositoryTest method findAllWithResourceListResult.
@Test
public void findAllWithResourceListResult() {
QuerySpec querySpec = new QuerySpec(Schedule.class);
QueryAdapter adapter = new QuerySpecAdapter(querySpec, resourceRegistry);
JsonApiResponse response = scheduleAdapter.findAll(adapter);
Assert.assertTrue(response.getEntity() instanceof ScheduleRepository.ScheduleList);
Assert.assertTrue(response.getLinksInformation() instanceof ScheduleRepository.ScheduleListLinks);
Assert.assertTrue(response.getMetaInformation() instanceof ScheduleRepository.ScheduleListMeta);
}
use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class ResourcePatch method extractAttributesFromResourceAsJson.
private String extractAttributesFromResourceAsJson(Resource resource) throws IOException {
JsonApiResponse response = new JsonApiResponse();
response.setEntity(resource);
// deserialize using the objectMapper so it becomes json-api
String newRequestBody = objectMapper.writeValueAsString(resource);
JsonNode node = objectMapper.readTree(newRequestBody);
JsonNode attributes = node.findValue("attributes");
return objectMapper.writeValueAsString(attributes);
}
Aggregations