use of io.crnk.jpa.model.dto.RelatedDTO in project crnk-framework by crnk-project.
the class RelatedDTOMapper method map.
@Override
public RelatedDTO map(Tuple tuple) {
RelatedDTO dto = new RelatedDTO();
RelatedEntity entity = tuple.get(0, RelatedEntity.class);
dto.setId(entity.getId());
dto.setStringValue(entity.getStringValue());
return dto;
}
use of io.crnk.jpa.model.dto.RelatedDTO 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.jpa.model.dto.RelatedDTO 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