use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class RelationshipRepositoryBase method findTargets.
@SuppressWarnings("unchecked")
public MultivaluedMap<I, D> findTargets(Iterable<I> sourceIds, String fieldName, QuerySpec querySpec) {
RegistryEntry sourceEntry = resourceRegistry.findEntry(sourceResourceClass);
ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
RegistryEntry targetEntry = getTargetEntry(field);
String oppositeName = getOppositeName(fieldName);
QuerySpec idQuerySpec = querySpec.duplicate();
idQuerySpec.addFilter(new FilterSpec(Arrays.asList(oppositeName, sourceInformation.getIdField().getUnderlyingName()), FilterOperator.EQ, sourceIds));
idQuerySpec.includeRelation(Arrays.asList(oppositeName));
ResourceRepositoryAdapter<D, J> targetAdapter = targetEntry.getResourceRepository();
JsonApiResponse response = targetAdapter.findAll(new QuerySpecAdapter(idQuerySpec, resourceRegistry));
List<D> results = (List<D>) response.getEntity();
MultivaluedMap<I, D> bulkResult = new MultivaluedMap<I, D>() {
@Override
protected List<D> newList() {
return new DefaultResourceList<>();
}
};
Set<I> sourceIdSet = new HashSet<>();
for (I sourceId : sourceIds) {
sourceIdSet.add(sourceId);
}
for (D result : results) {
handleTarget(bulkResult, result, sourceIdSet, oppositeName, sourceInformation);
}
return bulkResult;
}
use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class JsonApiRequestProcessorTest method createRequestBody.
private String createRequestBody(String name) throws JsonProcessingException {
Task task = new Task();
task.setId(1L);
task.setName(name);
task.setCategory("testCategory");
JsonApiResponse request = new JsonApiResponse();
request.setEntity(task);
Document requestDocument = boot.getDocumentMapper().toDocument(request, new QuerySpecAdapter(new QuerySpec(Task.class), boot.getResourceRegistry()));
return boot.getObjectMapper().writeValueAsString(requestDocument);
}
use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class ResourcePatchTest method omittedFieldsShouldBeIgnored.
@Test
public void omittedFieldsShouldBeIgnored() throws Exception {
// GIVEN
ResourceRepositoryAdapter taskRepo = resourceRegistry.getEntry(Task.class).getResourceRepository(null);
Task task = new Task();
task.setName("Mary Joe");
JsonApiResponse jsonApiResponse = taskRepo.create(task, emptyTaskQuery);
task = (Task) (jsonApiResponse.getEntity());
// GIVEN
Resource data = new Resource();
data.setType("tasks");
data.setAttribute("category", objectMapper.readTree("\"TestCategory\""));
Document taskPatch = new Document();
taskPatch.setData(Nullable.of((Object) data));
JsonPath jsonPath = pathBuilder.build("/tasks/" + task.getId());
ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
// WHEN
Response response = sut.handle(jsonPath, emptyTaskQuery, null, taskPatch);
// THEN
Assert.assertNotNull(response);
assertThat(response.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
Resource updatedTask = response.getDocument().getSingleData().get();
assertThat(updatedTask.getAttributes().get("name").asText()).isEqualTo("Mary Joe");
assertThat(updatedTask.getAttributes().get("category").asText()).isEqualTo("TestCategory");
assertThat(updatedTask.getId()).isEqualTo(task.getId().toString());
Assert.assertEquals("Mary Joe", task.getName());
Assert.assertEquals("TestCategory", task.getCategory());
}
use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class ResourcePatchTest method omittedFieldsSettersAreNotCalled.
/*
* see github #122
*/
@Test
public void omittedFieldsSettersAreNotCalled() throws Exception {
// GIVEN
ResourceRepositoryAdapter taskRepo = resourceRegistry.getEntry(Task.class).getResourceRepository(null);
Task task = new Task();
task.setName("Mary Joe");
JsonApiResponse jsonApiResponse = taskRepo.create(task, emptyTaskQuery);
task = (Task) (jsonApiResponse.getEntity());
// GIVEN
Document taskPatch = new Document();
Resource data = new Resource();
taskPatch.setData(Nullable.of((Object) data));
data.setType("tasks");
data.setAttribute("name", objectMapper.readTree("\"Mary Jane\""));
JsonPath jsonPath = pathBuilder.build("/tasks/" + task.getId());
ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
// WHEN
Response response = sut.handle(jsonPath, emptyTaskQuery, null, taskPatch);
// THEN
Assert.assertNotNull(response);
assertThat(response.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
Resource updatedTask = response.getDocument().getSingleData().get();
assertThat(updatedTask.getAttributes().get("name").asText()).isEqualTo("Mary Jane");
assertThat(updatedTask.getId()).isEqualTo(task.getId().toString());
assertThat(updatedTask.getAttributes().get("category")).isNull();
}
use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class CrnkBootTest method boot.
@Test
public void boot() {
CrnkBoot boot = new CrnkBoot();
boot.setDefaultServiceUrlProvider(new ServiceUrlProvider() {
@Override
public String getUrl() {
return "http://127.0.0.1";
}
});
boot.setServiceDiscovery(new ReflectionsServiceDiscovery(MockConstants.TEST_MODELS_PACKAGE));
boot.addModule(new SimpleModule("test"));
boot.boot();
RequestDispatcher requestDispatcher = boot.getRequestDispatcher();
ResourceRegistry resourceRegistry = boot.getResourceRegistry();
RegistryEntry taskEntry = resourceRegistry.getEntry(Task.class);
Assert.assertNotEquals(0, taskEntry.getRelationshipEntries().size());
ResourceRepositoryAdapter<?, ?> repositoryAdapter = taskEntry.getResourceRepository(null);
Assert.assertNotNull(repositoryAdapter.getResourceRepository());
JsonApiResponse response = repositoryAdapter.findAll(new QueryParamsAdapter(taskEntry.getResourceInformation(), new QueryParams(), boot.getModuleRegistry()));
Assert.assertNotNull(response);
Assert.assertNotNull(requestDispatcher);
ServiceDiscovery serviceDiscovery = boot.getServiceDiscovery();
Assert.assertNotNull(serviceDiscovery);
Assert.assertNotNull(boot.getModuleRegistry());
Assert.assertNotNull(boot.getExceptionMapperRegistry());
List<Module> modules = boot.getModuleRegistry().getModules();
Assert.assertEquals(4, modules.size());
boot.setDefaultPageLimit(20L);
boot.setMaxPageLimit(100L);
Assert.assertEquals(1, boot.getPagingBehaviors().size());
Assert.assertTrue(boot.getPagingBehaviors().get(0) instanceof OffsetLimitPagingBehavior);
}
Aggregations