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());
}
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));
}
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);
}
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());
}
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());
}
Aggregations