Search in sources :

Example 81 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.

the class JpaRelationshipRepositoryTestBase method testFindManyTargetWithFilter.

@Test
public void testFindManyTargetWithFilter() throws InstantiationException, IllegalAccessException {
    QuerySpec querySpec = new QuerySpec(RelatedEntity.class);
    querySpec.addFilter(new FilterSpec(Arrays.asList("id"), FilterOperator.EQ, 101));
    Iterable<RelatedEntity> relatedEntities = repo.findManyTargets(1L, TestEntity.ATTR_oneRelatedValue, querySpec);
    Iterator<RelatedEntity> iterator = relatedEntities.iterator();
    RelatedEntity relatedEntity = iterator.next();
    Assert.assertFalse(iterator.hasNext());
    Assert.assertNotNull(relatedEntity);
    Assert.assertEquals(101L, relatedEntity.getId().longValue());
}
Also used : RelatedEntity(io.crnk.jpa.model.RelatedEntity) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) Test(org.junit.Test) AbstractJpaTest(io.crnk.jpa.query.AbstractJpaTest)

Example 82 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.

the class JpaRelationshipRepositoryTestBase method testGetManyRelation.

@Test
public void testGetManyRelation() {
    TestEntity test = setupManyRelation(Arrays.asList(101L, 102L));
    QuerySpec querySpec = new QuerySpec(RelatedEntity.class);
    Iterable<RelatedEntity> targets = repo.findManyTargets(test.getId(), TestEntity.ATTR_manyRelatedValues, querySpec);
    List<RelatedEntity> res = new ArrayList<>();
    for (RelatedEntity relatedEntity : targets) {
        res.add(relatedEntity);
    }
    Assert.assertThat(res.size(), Is.is(2));
    Assert.assertThat(res.get(0).getId(), Is.is(101L));
    Assert.assertThat(res.get(1).getId(), Is.is(102L));
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) RelatedEntity(io.crnk.jpa.model.RelatedEntity) ArrayList(java.util.ArrayList) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test) AbstractJpaTest(io.crnk.jpa.query.AbstractJpaTest)

Example 83 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.

the class JpaRelationshipRepositoryTestBase method testFindNulledOneTarget.

@Test
public void testFindNulledOneTarget() throws InstantiationException, IllegalAccessException {
    long nulledEntityId = numTestEntities - 1;
    QuerySpec querySpec = new QuerySpec(RelatedEntity.class);
    RelatedEntity relatedEntity = repo.findOneTarget(nulledEntityId, TestEntity.ATTR_oneRelatedValue, querySpec);
    Assert.assertNull(relatedEntity);
}
Also used : RelatedEntity(io.crnk.jpa.model.RelatedEntity) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test) AbstractJpaTest(io.crnk.jpa.query.AbstractJpaTest)

Example 84 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.

the class DtoMappingTest method testMappedManyRelation.

@Test
public void testMappedManyRelation() {
    ResourceRepositoryV2<TestDTO, Serializable> testRepo = client.getQuerySpecRepository(TestDTO.class);
    ResourceRepositoryV2<RelatedDTO, Serializable> relatedRepo = client.getQuerySpecRepository(RelatedDTO.class);
    RelationshipRepositoryV2<TestDTO, Long, RelatedDTO, Long> relRepo = client.getQuerySpecRepository(TestDTO.class, RelatedDTO.class);
    TestDTO test = new TestDTO();
    test.setId(2L);
    test.setStringValue("createdDto");
    test = testRepo.create(test);
    RelatedDTO related1 = new RelatedDTO();
    related1.setId(1L);
    related1.setStringValue("related1");
    related1 = relatedRepo.create(related1);
    RelatedDTO related2 = new RelatedDTO();
    related2.setId(2L);
    related2.setStringValue("related2");
    related2 = relatedRepo.create(related2);
    Assert.assertEquals(1, testRepo.findAll(new QuerySpec(TestDTO.class)).size());
    relRepo.addRelations(test, Arrays.asList(related1.getId(), related2.getId()), TestEntity.ATTR_manyRelatedValues);
    Assert.assertEquals(1, testRepo.findAll(new QuerySpec(TestDTO.class)).size());
    // test relationship access
    List<RelatedDTO> actualRelatedList = relRepo.findManyTargets(test.getId(), TestEntity.ATTR_manyRelatedValues, new QuerySpec(RelatedDTO.class));
    Assert.assertEquals(2, actualRelatedList.size());
    // test include
    Assert.assertEquals(1, testRepo.findAll(new QuerySpec(TestDTO.class)).size());
// TODO distinct problem in H2 to investigate
// QuerySpec querySpec = new QuerySpec(TestDTO.class);
// querySpec.includeRelation(Arrays.asList(TestEntity.ATTR_manyRelatedValues));
// ResourceList<TestDTO> list = testRepo.findAll(querySpec);
// Assert.assertEquals(1, list.size());
// TestDTO actualTest = list.get(0);
// actualRelatedList = actualTest.getManyRelatedValues();
// Assert.assertEquals(2, actualRelatedList.size());
// test removal
// TODO DELETE request with body not supported by jersey?
// relRepo.removeRelations(test, Arrays.asList(related2.getId()), TestEntity.ATTR_manyRelatedValues);
// actualRelatedList = relRepo.findManyTargets(test.getId(), TestEntity.ATTR_manyRelatedValues,
// new QuerySpec(RelatedDTO.class));
// Assert.assertEquals(1, actualRelatedList.size());
// Assert.assertEquals(related1.getId(), actualRelatedList.get(0).getId());
}
Also used : TestDTO(io.crnk.jpa.model.dto.TestDTO) Serializable(java.io.Serializable) RelatedDTO(io.crnk.jpa.model.dto.RelatedDTO) QuerySpec(io.crnk.core.queryspec.QuerySpec) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest) Test(org.junit.Test)

Example 85 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.

the class DtoMappingTest method testMappedOneRelation.

@Test
public void testMappedOneRelation() {
    ResourceRepositoryV2<TestDTO, Serializable> testRepo = client.getQuerySpecRepository(TestDTO.class);
    ResourceRepositoryV2<RelatedDTO, Serializable> relatedRepo = client.getQuerySpecRepository(RelatedDTO.class);
    RelationshipRepositoryV2<TestDTO, Serializable, RelatedDTO, Serializable> relRepo = client.getQuerySpecRepository(TestDTO.class, RelatedDTO.class);
    TestDTO test = new TestDTO();
    test.setId(2L);
    test.setStringValue("createdDto");
    test = testRepo.create(test);
    RelatedDTO related = new RelatedDTO();
    related.setId(3L);
    related.setStringValue("createdDto");
    related = relatedRepo.create(related);
    relRepo.setRelation(test, related.getId(), TestEntity.ATTR_oneRelatedValue);
    // test relationship access
    RelatedDTO actualRelated = relRepo.findOneTarget(test.getId(), TestEntity.ATTR_oneRelatedValue, new QuerySpec(RelatedDTO.class));
    Assert.assertNotNull(actualRelated);
    Assert.assertEquals(related.getId(), actualRelated.getId());
    // test include
    QuerySpec querySpec = new QuerySpec(TestDTO.class);
    querySpec.includeRelation(Arrays.asList(TestEntity.ATTR_oneRelatedValue));
    ResourceList<TestDTO> list = testRepo.findAll(querySpec);
    Assert.assertEquals(1, list.size());
    TestDTO actualTest = list.get(0);
    actualRelated = actualTest.getOneRelatedValue();
    Assert.assertNotNull(actualRelated);
    Assert.assertEquals(related.getId(), actualRelated.getId());
}
Also used : TestDTO(io.crnk.jpa.model.dto.TestDTO) Serializable(java.io.Serializable) RelatedDTO(io.crnk.jpa.model.dto.RelatedDTO) QuerySpec(io.crnk.core.queryspec.QuerySpec) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest) Test(org.junit.Test)

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