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