use of io.crnk.core.engine.internal.document.mapper.DocumentMapper in project crnk-framework by crnk-project.
the class IncludeLookupSetterBaseTest method includeByDefaultSerializeNLevels.
@Test
public void includeByDefaultSerializeNLevels() throws Exception {
Project project = new Project();
project.setId(1L);
Task task = new Task().setId(2L);
project.setTask(task);
Project projectDefault = new Project().setId(3L);
task.setProject(projectDefault);
mapper = new DocumentMapper(resourceRegistry, objectMapper, new EmptyPropertiesProvider(), resourceFilterDirectory);
QuerySpec querySpec = new QuerySpec(Project.class);
querySpec.includeRelation(Collections.singletonList("task"));
Document document = mapper.toDocument(toResponse(project), toAdapter(querySpec));
Resource projectResource = document.getSingleData().get();
Relationship relationship = projectResource.getRelationships().get("task");
assertNotNull(relationship);
assertEquals("2", relationship.getSingleData().get().getId());
assertNotNull(document.getIncluded());
assertEquals(2, document.getIncluded().size());
List<Resource> resources = document.getIncluded();
assertEquals("projects", resources.get(0).getType());
assertEquals("3", resources.get(0).getId());
assertEquals("tasks", resources.get(1).getType());
assertEquals("2", resources.get(1).getId());
}
use of io.crnk.core.engine.internal.document.mapper.DocumentMapper in project crnk-framework by crnk-project.
the class IncludeLookupSetterBaseTest method includePropertiesProviderNonOverwriteRelationshipLookup.
@Test
public void includePropertiesProviderNonOverwriteRelationshipLookup() throws Exception {
PropertiesProvider propertiesProvider = new PropertiesProvider() {
@Override
public String getProperty(String key) {
if (key.equalsIgnoreCase(CrnkProperties.INCLUDE_AUTOMATICALLY_OVERWRITE)) {
return "false";
}
return null;
}
};
mapper = new DocumentMapper(resourceRegistry, objectMapper, propertiesProvider, resourceFilterDirectory);
QuerySpec querySpec = new QuerySpec(Task.class);
querySpec.includeRelation(Arrays.asList("project"));
Project project = new Project();
project.setId(12L);
Task task = new Task();
task.setId(1L);
task.setProject(project);
Document document = mapper.toDocument(toResponse(task), toAdapter(querySpec));
Resource taskResource = document.getSingleData().get();
Relationship relationship = taskResource.getRelationships().get("project");
assertNotNull(relationship);
assertEquals(relationship.getSingleData().get().getId(), "12");
}
use of io.crnk.core.engine.internal.document.mapper.DocumentMapper in project crnk-framework by crnk-project.
the class IncludeLookupSetterBaseTest method includePropertiesProviderAllTrueRelationshipLookup.
@Test
public void includePropertiesProviderAllTrueRelationshipLookup() throws Exception {
PropertiesProvider propertiesProvider = new PropertiesProvider() {
@Override
public String getProperty(String key) {
if (key.equalsIgnoreCase(CrnkProperties.INCLUDE_AUTOMATICALLY_OVERWRITE)) {
return "true";
}
return null;
}
};
mapper = new DocumentMapper(resourceRegistry, objectMapper, propertiesProvider, resourceFilterDirectory);
QuerySpec querySpec = new QuerySpec(Task.class);
querySpec.includeRelation(Arrays.asList("project"));
Task task = new Task();
task.setId(1L);
Document document = mapper.toDocument(toResponse(task), toAdapter(querySpec));
Resource taskResource = document.getSingleData().get();
assertNotNull(taskResource.getRelationships().get("project"));
assertNotNull(taskResource.getRelationships().get("project").getData());
}
use of io.crnk.core.engine.internal.document.mapper.DocumentMapper in project crnk-framework by crnk-project.
the class OperationsCall method toResource.
protected Resource toResource(Object object) {
JsonApiResponse response = new JsonApiResponse();
response.setEntity(object);
QuerySpec querySpec = new QuerySpec(object.getClass());
QueryAdapter queryAdapter = new QuerySpecAdapter(querySpec, client.getCrnk().getRegistry());
CrnkClient crnk = client.getCrnk();
DocumentMapper documentMapper = crnk.getDocumentMapper();
Document document = documentMapper.toDocument(response, queryAdapter);
return document.getSingleData().get();
}
use of io.crnk.core.engine.internal.document.mapper.DocumentMapper in project crnk-framework by crnk-project.
the class JsonApiResponseFilter method filter.
/**
* Creates JSON API responses for custom JAX-RS actions returning Crnk resources.
*/
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
Object response = responseContext.getEntity();
if (response == null) {
if (feature.getBoot().isNullDataResponseEnabled()) {
Document document = new Document();
document.setData(Nullable.nullValue());
responseContext.setEntity(document);
responseContext.setStatus(Response.Status.OK.getStatusCode());
responseContext.getHeaders().put("Content-Type", Collections.singletonList((Object) JsonApiMediaType.APPLICATION_JSON_API));
}
return;
}
// only modify responses which contain a single or a list of Crnk resources
if (isResourceResponse(response)) {
CrnkBoot boot = feature.getBoot();
DocumentMapper documentMapper = boot.getDocumentMapper();
HttpRequestContextProvider httpRequestContextProvider = boot.getModuleRegistry().getHttpRequestContextProvider();
try {
HttpRequestContext context = new HttpRequestContextBaseAdapter(new JaxrsRequestContext(requestContext, feature));
httpRequestContextProvider.onRequestStarted(context);
JsonApiResponse jsonApiResponse = new JsonApiResponse();
jsonApiResponse.setEntity(response);
// use the Crnk document mapper to create a JSON API response
responseContext.setEntity(documentMapper.toDocument(jsonApiResponse, null));
responseContext.getHeaders().put("Content-Type", Collections.singletonList((Object) JsonApiMediaType.APPLICATION_JSON_API));
} finally {
httpRequestContextProvider.onRequestFinished();
}
} else if (isJsonApiResponse(responseContext) && !doNotWrap(response)) {
Document document = new Document();
document.setData(Nullable.of(response));
responseContext.setEntity(document);
}
}
Aggregations