use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.
the class MetaFilteringTest method checkReadOnlyAttribute.
@Test
public void checkReadOnlyAttribute() throws IOException {
RegistryEntry entry = boot.getResourceRegistry().getEntry(Task.class);
ResourceInformation resourceInformation = entry.getResourceInformation();
ResourceField projectField = resourceInformation.findFieldByUnderlyingName("project");
Mockito.when(filter.filterField(Mockito.eq(projectField), Mockito.eq(HttpMethod.POST))).thenReturn(FilterBehavior.FORBIDDEN);
Mockito.when(filter.filterField(Mockito.eq(projectField), Mockito.eq(HttpMethod.PATCH))).thenReturn(FilterBehavior.FORBIDDEN);
QuerySpec querySpec = new QuerySpec(MetaResource.class);
querySpec.addFilter(new FilterSpec(Arrays.asList("name"), FilterOperator.EQ, "Tasks"));
ResourceList<MetaResource> list = repository.findAll(querySpec);
Assert.assertEquals(1, list.size());
MetaResource taskMeta = list.get(0);
Assert.assertTrue(taskMeta.hasAttribute("project"));
MetaAttribute projectAttr = taskMeta.getAttribute("project");
Assert.assertFalse(projectAttr.isInsertable());
Assert.assertFalse(projectAttr.isUpdatable());
}
use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.
the class AbstractBraveModuleTest method testFindTargets.
@Test
public void testFindTargets() {
RelationshipRepositoryV2<Project, Serializable, Task, Serializable> relRepo = client.getRepositoryForType(Project.class, Task.class);
relRepo.findManyTargets(123L, "tasks", new QuerySpec(Task.class));
// check client call and link span
ArgumentCaptor<Span> clientSpanCaptor = ArgumentCaptor.forClass(Span.class);
Mockito.verify(clientReporter, Mockito.times(1)).report(clientSpanCaptor.capture());
List<Span> clientSpans = clientSpanCaptor.getAllValues();
Span callSpan = clientSpans.get(0);
Assert.assertEquals("get", callSpan.name());
Assert.assertEquals(Span.Kind.CLIENT, callSpan.kind());
// check server local span
ArgumentCaptor<Span> serverSpanCaptor = ArgumentCaptor.forClass(Span.class);
Mockito.verify(serverReporter, Mockito.times(2)).report(serverSpanCaptor.capture());
List<Span> serverSpans = serverSpanCaptor.getAllValues();
Span repositorySpan0 = serverSpans.get(0);
Assert.assertEquals("crnk:get:/tasks/", repositorySpan0.name());
Assert.assertTrue(repositorySpan0.toString().contains("\"lc\""));
assertTag(repositorySpan0, "lc", "crnk");
assertTag(repositorySpan0, "crnk.results", "0");
assertTag(repositorySpan0, "crnk.status", "OK");
Span repositorySpan1 = serverSpans.get(1);
Assert.assertEquals("crnk:get:/projects/123/tasks/", repositorySpan1.name());
Assert.assertTrue(repositorySpan1.toString().contains("\"lc\""));
assertTag(repositorySpan1, "lc", "crnk");
assertTag(repositorySpan1, "crnk.query", "?");
assertTag(repositorySpan1, "crnk.results", "0");
assertTag(repositorySpan1, "crnk.status", "OK");
}
use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.
the class AbstractBraveModuleTest method testFindAll.
@Test
public void testFindAll() {
Task task = new Task();
task.setId(13L);
task.setName("myTask");
QuerySpec querySpec = new QuerySpec(Task.class);
querySpec.addFilter(new FilterSpec(Arrays.asList("name"), FilterOperator.EQ, "doe"));
taskRepo.findAll(querySpec);
// check client call and link span
ArgumentCaptor<Span> clientSpanCaptor = ArgumentCaptor.forClass(Span.class);
Mockito.verify(clientReporter, Mockito.times(isOkHttp ? 1 : 1)).report(clientSpanCaptor.capture());
List<Span> clientSpans = clientSpanCaptor.getAllValues();
Span callSpan = clientSpans.get(0);
Assert.assertEquals("get", callSpan.name());
Assert.assertEquals(Span.Kind.CLIENT, callSpan.kind());
// check server local span
ArgumentCaptor<Span> serverSpanCaptor = ArgumentCaptor.forClass(Span.class);
Mockito.verify(serverReporter, Mockito.times(1)).report(serverSpanCaptor.capture());
List<Span> serverSpans = serverSpanCaptor.getAllValues();
Span repositorySpan = serverSpans.get(0);
Assert.assertEquals("crnk:get:/tasks/", repositorySpan.name());
Assert.assertTrue(repositorySpan.toString().contains("\"lc\""));
assertTag(repositorySpan, "lc", "crnk");
assertTag(repositorySpan, "crnk.query", "?filter[tasks][name][EQ]=doe");
assertTag(repositorySpan, "crnk.results", "0");
assertTag(repositorySpan, "crnk.status", "OK");
}
use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.
the class OperationsDeleteRelationTest method testDeleteRelation.
@Test
public void testDeleteRelation() {
PersonEntity person1 = newPerson("1");
PersonEntity person2 = newPerson("2");
MovieEntity movie = newMovie("test");
movie.setDirectors(new HashSet<>(Arrays.asList(person1, person2)));
OperationsCall insertCall = operationsClient.createCall();
insertCall.add(HttpMethod.POST, movie);
insertCall.add(HttpMethod.POST, person1);
insertCall.add(HttpMethod.POST, person2);
insertCall.execute();
QuerySpec querySpec = new QuerySpec(MovieEntity.class);
querySpec.includeRelation(Arrays.asList("directors"));
MovieEntity updatedMovie = movieRepo.findOne(movie.getId(), querySpec);
Set<PersonEntity> directors = updatedMovie.getDirectors();
PersonEntity deletedDirector = directors.iterator().next();
directors.remove(deletedDirector);
OperationsCall call = operationsClient.createCall();
call.add(HttpMethod.PATCH, updatedMovie);
call.add(HttpMethod.DELETE, deletedDirector);
call.execute();
// check whether updated relationship is included in the response
Resource movieResource = call.getResponse(0).getSingleData().get();
Relationship directorsRelationship = movieResource.getRelationships().get("directors");
List<ResourceIdentifier> directorIds = directorsRelationship.getCollectionData().get();
Assert.assertEquals(1, directorIds.size());
}
use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.
the class JpaQuerySpecEndToEndTest method testAddManyRelation.
private void testAddManyRelation(boolean onSave) throws InstantiationException, IllegalAccessException {
ResourceRepositoryV2<RelatedEntity, Long> relatedRepo = client.getQuerySpecRepository(RelatedEntity.class);
RelatedEntity related1 = new RelatedEntity();
related1.setId(1L);
related1.setStringValue("related1");
relatedRepo.create(related1);
RelatedEntity related2 = new RelatedEntity();
related2.setId(2L);
related2.setStringValue("related2");
relatedRepo.create(related2);
TestEntity test = new TestEntity();
test.setId(3L);
test.setStringValue("test");
if (onSave) {
test.setManyRelatedValues(Arrays.asList(related1, related2));
}
testRepo.create(test);
// query relation
RelationshipRepositoryV2<TestEntity, Long, RelatedEntity, Long> relRepo = client.getQuerySpecRepository(TestEntity.class, RelatedEntity.class);
if (!onSave) {
relRepo.addRelations(test, Arrays.asList(1L, 2L), TestEntity.ATTR_manyRelatedValues);
}
List<RelatedEntity> related = relRepo.findManyTargets(test.getId(), TestEntity.ATTR_manyRelatedValues, new QuerySpec(RelatedEntity.class));
Assert.assertEquals(2, related.size());
// query relation in opposite direction
RelationshipRepositoryV2<RelatedEntity, Serializable, TestEntity, Serializable> backRelRepo = client.getQuerySpecRepository(RelatedEntity.class, TestEntity.class);
test = backRelRepo.findOneTarget(2L, RelatedEntity.ATTR_testEntity, new QuerySpec(TestEntity.class));
Assert.assertNotNull(test);
Assert.assertEquals(3L, test.getId().longValue());
}
Aggregations