use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.
the class ResourceRepositoryBase method findOne.
/**
* Forwards to {@link #findAll(QuerySpec)}
*
* @param id of the resource
* @param querySpec for field and relation inclusion
* @return resource
*/
@Override
public T findOne(I id, QuerySpec querySpec) {
RegistryEntry entry = resourceRegistry.findEntry(resourceClass);
String idName = entry.getResourceInformation().getIdField().getUnderlyingName();
QuerySpec idQuerySpec = querySpec.duplicate();
idQuerySpec.addFilter(new FilterSpec(Arrays.asList(idName), FilterOperator.EQ, id));
Iterable<T> iterable = findAll(idQuerySpec);
Iterator<T> iterator = iterable.iterator();
if (iterator.hasNext()) {
T resource = iterator.next();
PreconditionUtil.assertFalse("expected unique result", iterator.hasNext());
return resource;
} else {
throw new ResourceNotFoundException("resource not found");
}
}
use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.
the class ResourceRepositoryBase method findAll.
/**
* Forwards to {@link #findAll(QuerySpec)}
*
* @param ids of the resources
* @param querySpec for field and relation inclusion
* @return resources
*/
@Override
public ResourceList<T> findAll(Iterable<I> ids, QuerySpec querySpec) {
RegistryEntry entry = resourceRegistry.findEntry(resourceClass);
String idName = entry.getResourceInformation().getIdField().getUnderlyingName();
QuerySpec idQuerySpec = querySpec.duplicate();
idQuerySpec.addFilter(new FilterSpec(Arrays.asList(idName), FilterOperator.EQ, ids));
return findAll(idQuerySpec);
}
use of io.crnk.core.queryspec.QuerySpec 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.queryspec.QuerySpec in project crnk-framework by crnk-project.
the class RelationshipsResourcePostTest method onDeletingToOneRelationshipShouldSetTheValue.
@Test
public void onDeletingToOneRelationshipShouldSetTheValue() throws Exception {
// GIVEN
Document newTaskBody = new Document();
Resource data = createTask();
newTaskBody.setData(Nullable.of((Object) data));
JsonPath taskPath = pathBuilder.build("/tasks");
ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, OBJECT_MAPPER, documentMapper, modificationFilters);
// WHEN -- adding a task
Response taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
// THEN
assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
Long taskId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
assertThat(taskId).isNotNull();
/* ------- */
// GIVEN
Document newTaskToProjectBody = new Document();
newTaskToProjectBody.setData(Nullable.nullValue());
JsonPath savedTaskPath = pathBuilder.build("/tasks/" + taskId + "/relationships/project");
RelationshipsResourcePost sut = new RelationshipsResourcePost(resourceRegistry, typeParser, modificationFilters);
// WHEN -- adding a relation between user and project
Response projectRelationshipResponse = sut.handle(savedTaskPath, emptyProjectQuery, null, newTaskToProjectBody);
assertThat(projectRelationshipResponse).isNotNull();
// THEN
assertThat(projectRelationshipResponse.getHttpStatus()).isEqualTo(HttpStatus.NO_CONTENT_204);
Project project = localUserToProjectRepository.findOneTarget(1L, "project", new QuerySpec(Project.class));
assertThat(project).isNull();
}
use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.
the class ResourceFilterTest method checkFilterGetOnResourceField.
@Test
public void checkFilterGetOnResourceField() {
// setup test data
RegistryEntry entry = resourceRegistry.getEntry(Task.class);
ResourceRepositoryAdapter resourceRepository = entry.getResourceRepository();
Project project = new Project();
project.setId(13L);
project.setName("myProject");
Task task = new Task();
task.setId(12L);
task.setName("myTask");
task.setProject(project);
resourceRepository.create(task, new QuerySpecAdapter(new QuerySpec(Task.class), resourceRegistry));
// get information
ResourceInformation resourceInformation = entry.getResourceInformation();
ResourceField projectField = resourceInformation.findFieldByUnderlyingName("project");
ResourceField nameField = resourceInformation.findFieldByUnderlyingName("name");
String path = "/tasks/";
String method = HttpMethod.GET.toString();
Map<String, Set<String>> parameters = Collections.emptyMap();
Document requestBody = null;
// forbid field
Mockito.when(filter.filterField(Mockito.eq(projectField), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.FORBIDDEN);
Response response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
Assert.assertEquals(HttpStatus.OK_200, response.getHttpStatus().intValue());
Resource taskResource = response.getDocument().getCollectionData().get().get(0);
Assert.assertTrue(taskResource.getRelationships().containsKey("projects"));
Assert.assertFalse(taskResource.getRelationships().containsKey("project"));
Assert.assertTrue(taskResource.getAttributes().containsKey("name"));
// allow resource
Mockito.when(filter.filterField(Mockito.eq(nameField), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.FORBIDDEN);
response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
Assert.assertEquals(HttpStatus.OK_200, response.getHttpStatus().intValue());
taskResource = response.getDocument().getCollectionData().get().get(0);
Assert.assertTrue(taskResource.getRelationships().containsKey("projects"));
Assert.assertFalse(taskResource.getRelationships().containsKey("project"));
Assert.assertFalse(taskResource.getAttributes().containsKey("name"));
}
Aggregations