Search in sources :

Example 61 with QuerySpec

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());
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) MetaResource(io.crnk.meta.model.resource.MetaResource) MetaAttribute(io.crnk.meta.model.MetaAttribute) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Test(org.junit.Test)

Example 62 with QuerySpec

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");
}
Also used : Project(io.crnk.monitor.brave.mock.models.Project) Serializable(java.io.Serializable) Task(io.crnk.monitor.brave.mock.models.Task) QuerySpec(io.crnk.core.queryspec.QuerySpec) Span(zipkin2.Span) Test(org.junit.Test)

Example 63 with QuerySpec

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");
}
Also used : Task(io.crnk.monitor.brave.mock.models.Task) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) Span(zipkin2.Span) Test(org.junit.Test)

Example 64 with QuerySpec

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());
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) MovieEntity(io.crnk.operations.model.MovieEntity) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) OperationsCall(io.crnk.operations.client.OperationsCall) PersonEntity(io.crnk.operations.model.PersonEntity) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test)

Example 65 with QuerySpec

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());
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) Serializable(java.io.Serializable) RelatedEntity(io.crnk.jpa.model.RelatedEntity) OtherRelatedEntity(io.crnk.jpa.model.OtherRelatedEntity) QuerySpec(io.crnk.core.queryspec.QuerySpec)

Aggregations

QuerySpec (io.crnk.core.queryspec.QuerySpec)306 Test (org.junit.Test)233 FilterSpec (io.crnk.core.queryspec.FilterSpec)51 Document (io.crnk.core.engine.document.Document)45 Resource (io.crnk.core.engine.document.Resource)43 Set (java.util.Set)39 HashMap (java.util.HashMap)37 HashSet (java.util.HashSet)36 AbstractQuerySpecTest (io.crnk.core.queryspec.AbstractQuerySpecTest)34 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)32 AbstractJpaJerseyTest (io.crnk.jpa.AbstractJpaJerseyTest)32 Task (io.crnk.test.mock.models.Task)32 Project (io.crnk.core.mock.models.Project)28 Relationship (io.crnk.core.engine.document.Relationship)26 Task (io.crnk.core.mock.models.Task)26 TestEntity (io.crnk.jpa.model.TestEntity)26 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)25 Serializable (java.io.Serializable)24 RelatedEntity (io.crnk.jpa.model.RelatedEntity)21 ResourceRegistryTest (io.crnk.core.resource.registry.ResourceRegistryTest)20