Search in sources :

Example 26 with JsonApiResponse

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();
}
Also used : ResourceRepositoryInformation(io.crnk.core.engine.information.repository.ResourceRepositoryInformation) ExceptionMapper(io.crnk.core.engine.error.ExceptionMapper) ResourceList(io.crnk.core.resource.list.ResourceList) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) HashMap(java.util.HashMap) ResourceField(io.crnk.core.engine.information.resource.ResourceField) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter) ArrayList(java.util.ArrayList) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) ErrorResponse(io.crnk.core.engine.error.ErrorResponse) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResourceFieldNotFoundException(io.crnk.core.exception.ResourceFieldNotFoundException) Map(java.util.Map) AnnotatedResourceEntry(io.crnk.legacy.registry.AnnotatedResourceEntry) DirectResponseResourceEntry(io.crnk.legacy.internal.DirectResponseResourceEntry) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) DirectResponseRelationshipEntry(io.crnk.legacy.internal.DirectResponseRelationshipEntry) ResourceRepositoryV2(io.crnk.core.repository.ResourceRepositoryV2) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) QuerySpec(io.crnk.core.queryspec.QuerySpec) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Collection(java.util.Collection) ModuleRegistry(io.crnk.core.module.ModuleRegistry) Collectors(java.util.stream.Collectors) AnnotatedRelationshipEntryBuilder(io.crnk.legacy.registry.AnnotatedRelationshipEntryBuilder) ErrorData(io.crnk.core.engine.document.ErrorData) Serializable(java.io.Serializable) List(java.util.List) RepositoryMethodParameterProvider(io.crnk.legacy.internal.RepositoryMethodParameterProvider) Optional(java.util.Optional) PreconditionUtil(io.crnk.core.engine.internal.utils.PreconditionUtil) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) RelationshipRepositoryNotFoundException(io.crnk.core.exception.RelationshipRepositoryNotFoundException) ArrayList(java.util.ArrayList) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) ErrorData(io.crnk.core.engine.document.ErrorData) ErrorResponse(io.crnk.core.engine.error.ErrorResponse)

Example 27 with JsonApiResponse

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());
}
Also used : HasMoreResourcesMetaInformation(io.crnk.core.resource.meta.HasMoreResourcesMetaInformation) PagedLinksInformation(io.crnk.core.resource.links.PagedLinksInformation) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) AbstractQuerySpecTest(io.crnk.core.queryspec.AbstractQuerySpecTest) Test(org.junit.Test)

Example 28 with JsonApiResponse

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());
}
Also used : HasMoreResourcesMetaInformation(io.crnk.core.resource.meta.HasMoreResourcesMetaInformation) PagedLinksInformation(io.crnk.core.resource.links.PagedLinksInformation) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) AbstractQuerySpecTest(io.crnk.core.queryspec.AbstractQuerySpecTest) Test(org.junit.Test)

Example 29 with JsonApiResponse

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);
}
Also used : QueryAdapter(io.crnk.core.engine.query.QueryAdapter) ScheduleRepository(io.crnk.core.mock.repository.ScheduleRepository) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) QuerySpec(io.crnk.core.queryspec.QuerySpec) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) AbstractQuerySpecTest(io.crnk.core.queryspec.AbstractQuerySpecTest) Test(org.junit.Test)

Example 30 with JsonApiResponse

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);
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)49 Document (io.crnk.core.engine.document.Document)16 Test (org.junit.Test)16 ResourceField (io.crnk.core.engine.information.resource.ResourceField)15 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)14 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)12 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)11 QuerySpec (io.crnk.core.queryspec.QuerySpec)11 RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)10 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)10 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)10 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)10 Serializable (java.io.Serializable)10 Response (io.crnk.core.engine.dispatcher.Response)9 Resource (io.crnk.core.engine.document.Resource)9 Task (io.crnk.core.mock.models.Task)7 AbstractQuerySpecTest (io.crnk.core.queryspec.AbstractQuerySpecTest)6 BulkRelationshipRepositoryV2 (io.crnk.core.repository.BulkRelationshipRepositoryV2)6 PagedLinksInformation (io.crnk.core.resource.links.PagedLinksInformation)6 HashSet (java.util.HashSet)6