Search in sources :

Example 71 with Resource

use of io.crnk.core.engine.document.Resource in project crnk-framework by crnk-project.

the class IncludeLookupSetter method getIds.

private List<Serializable> getIds(Collection<Resource> resources, ResourceInformation resourceInformation) {
    List<Serializable> ids = new ArrayList<>();
    for (Resource resource : resources) {
        Serializable id = resourceInformation.parseIdString(resource.getId());
        ids.add(id);
    }
    return ids;
}
Also used : Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) Resource(io.crnk.core.engine.document.Resource)

Example 72 with Resource

use of io.crnk.core.engine.document.Resource in project crnk-framework by crnk-project.

the class ResourceMapper method toData.

public Resource toData(Object entity, QueryAdapter queryAdapter, ResourceMappingConfig mappingConfig) {
    if (entity instanceof Resource) {
        // Resource and ResourceId
        return (Resource) entity;
    } else {
        // map resource objects
        Class<?> dataClass = entity.getClass();
        ResourceInformation resourceInformation = util.getResourceInformation(dataClass);
        Resource resource = new Resource();
        resource.setId(util.getIdString(entity, resourceInformation));
        resource.setType(resourceInformation.getResourceType());
        if (!client) {
            if (mappingConfig.getSerializeLinks()) {
                util.setLinks(resource, getResourceLinks(entity, resourceInformation), queryAdapter);
            }
            util.setMeta(resource, getResourceMeta(entity, resourceInformation));
        }
        setAttributes(resource, entity, resourceInformation, queryAdapter);
        setRelationships(resource, entity, resourceInformation, queryAdapter, mappingConfig);
        return resource;
    }
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Resource(io.crnk.core.engine.document.Resource)

Example 73 with Resource

use of io.crnk.core.engine.document.Resource in project crnk-framework by crnk-project.

the class IncludeLookupUtil method getRelationshipFields.

public Set<ResourceField> getRelationshipFields(Collection<Resource> resources) {
    Set<ResourceField> fields = new HashSet<>();
    Set<String> processedTypes = new HashSet<>();
    for (Resource resource : resources) {
        process(resource.getType(), processedTypes, fields);
    }
    return fields;
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) Resource(io.crnk.core.engine.document.Resource) HashSet(java.util.HashSet)

Example 74 with Resource

use of io.crnk.core.engine.document.Resource in project crnk-framework by crnk-project.

the class JsonApiRequestProcessorTest method getTasksWithCompactHeader.

@Test
public void getTasksWithCompactHeader() throws IOException {
    Mockito.when(requestContextBase.getMethod()).thenReturn("GET");
    Mockito.when(requestContextBase.getPath()).thenReturn("/tasks/");
    Mockito.when(requestContextBase.getRequestHeader("Accept")).thenReturn("*");
    Mockito.when(requestContextBase.getRequestHeader(HttpHeaders.HTTP_HEADER_CRNK_COMPACT)).thenReturn("true");
    processor.process(requestContext);
    ArgumentCaptor<byte[]> contentCaptor = ArgumentCaptor.forClass(byte[].class);
    Mockito.verify(requestContextBase, Mockito.times(1)).setResponse(Mockito.eq(200), contentCaptor.capture());
    String json = new String(contentCaptor.getValue());
    Document document = boot.getObjectMapper().readerFor(Document.class).readValue(json);
    Assert.assertTrue(document.getData().isPresent());
    List<Resource> resources = document.getCollectionData().get();
    Assert.assertEquals(1, resources.size());
    Resource resource = resources.get(0);
    Assert.assertNull(resource.getLinks().get("self"));
    Assert.assertNotNull(resource.getLinks().get("value"));
}
Also used : Resource(io.crnk.core.engine.document.Resource) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Example 75 with Resource

use of io.crnk.core.engine.document.Resource in project crnk-framework by crnk-project.

the class JsonApiRequestProcessorTest method postTasks.

@Test
public void postTasks() throws IOException {
    String requestBody = createRequestBody("test");
    Mockito.when(requestContextBase.getMethod()).thenReturn("POST");
    Mockito.when(requestContextBase.getPath()).thenReturn("/tasks/");
    Mockito.when(requestContextBase.getRequestBody()).thenReturn(requestBody.getBytes());
    Mockito.when(requestContextBase.getRequestHeader(HttpHeaders.HTTP_CONTENT_TYPE)).thenReturn(HttpHeaders.JSONAPI_CONTENT_TYPE);
    Mockito.when(requestContextBase.getRequestHeader("Accept")).thenReturn(HttpHeaders.JSONAPI_CONTENT_TYPE);
    processor.process(requestContext);
    ArgumentCaptor<byte[]> contentCaptor = ArgumentCaptor.forClass(byte[].class);
    Mockito.verify(requestContextBase, Mockito.times(1)).setResponse(Mockito.eq(HttpStatus.CREATED_201), contentCaptor.capture());
    String json = new String(contentCaptor.getValue());
    Document document = boot.getObjectMapper().readerFor(Document.class).readValue(json);
    Assert.assertTrue(document.getData().isPresent());
    Resource updatedTask = (Resource) document.getData().get();
    Assert.assertEquals("1", updatedTask.getId());
    Assert.assertEquals("tasks", updatedTask.getType());
}
Also used : Resource(io.crnk.core.engine.document.Resource) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Aggregations

Resource (io.crnk.core.engine.document.Resource)130 Document (io.crnk.core.engine.document.Document)86 Test (org.junit.Test)85 Relationship (io.crnk.core.engine.document.Relationship)48 Response (io.crnk.core.engine.dispatcher.Response)43 QuerySpec (io.crnk.core.queryspec.QuerySpec)43 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)42 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)41 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)37 Task (io.crnk.core.mock.models.Task)32 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)31 Project (io.crnk.core.mock.models.Project)24 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)17 LazyTask (io.crnk.core.mock.models.LazyTask)15 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)14 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)14 RelationIdTestResource (io.crnk.core.mock.models.RelationIdTestResource)13 ResourceField (io.crnk.core.engine.information.resource.ResourceField)12 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)11 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)10