use of io.crnk.jpa.model.RelatedEntity in project crnk-framework by crnk-project.
the class JpaQuerySpecEndToEndTest method testRelationPaging.
@Test
public void testRelationPaging() {
TestEntity test = new TestEntity();
test.setId(1L);
test.setStringValue("test");
testRepo.create(test);
ResourceRepositoryStub<RelatedEntity, Long> relatedRepo = client.getQueryParamsRepository(RelatedEntity.class);
RelationshipRepositoryV2<TestEntity, Long, RelatedEntity, Long> relRepo = client.getQuerySpecRepository(TestEntity.class, RelatedEntity.class);
for (long i = 0; i < 5; i++) {
RelatedEntity related1 = new RelatedEntity();
related1.setId(i);
related1.setStringValue("related" + i);
relatedRepo.create(related1);
relRepo.addRelations(test, Arrays.asList(i), TestEntity.ATTR_manyRelatedValues);
}
QuerySpec querySpec = new QuerySpec(RelatedEntity.class);
querySpec.setOffset(2L);
querySpec.setLimit(2L);
ResourceList<RelatedEntity> list = relRepo.findManyTargets(test.getId(), TestEntity.ATTR_manyRelatedValues, querySpec);
Assert.assertEquals(2, list.size());
Assert.assertEquals(2, list.get(0).getId().intValue());
Assert.assertEquals(3, list.get(1).getId().intValue());
JsonMetaInformation meta = list.getMeta(JsonMetaInformation.class);
JsonLinksInformation links = list.getLinks(JsonLinksInformation.class);
Assert.assertNotNull(meta);
Assert.assertNotNull(links);
String baseUri = getBaseUri().toString();
Assert.assertEquals(baseUri + "test/1/relationships/manyRelatedValues?page[limit]=2", links.asJsonNode().get("first").asText());
Assert.assertEquals(baseUri + "test/1/relationships/manyRelatedValues?page[limit]=2&page[offset]=4", links.asJsonNode().get("last").asText());
Assert.assertEquals(baseUri + "test/1/relationships/manyRelatedValues?page[limit]=2", links.asJsonNode().get("prev").asText());
Assert.assertEquals(baseUri + "test/1/relationships/manyRelatedValues?page[limit]=2&page[offset]=4", links.asJsonNode().get("next").asText());
}
use of io.crnk.jpa.model.RelatedEntity in project crnk-framework by crnk-project.
the class JpaQuerySpecEndToEndTest method testLazyManyRelation.
@Test
public void testLazyManyRelation() throws InstantiationException, IllegalAccessException {
addTestWithManyRelations();
QuerySpec querySpec = new QuerySpec(TestEntity.class);
List<TestEntity> list = testRepo.findAll(querySpec);
Assert.assertEquals(1, list.size());
TestEntity testEntity = list.get(0);
List<RelatedEntity> manyRelatedValues = testEntity.getManyRelatedValues();
Assert.assertNotNull(manyRelatedValues);
ObjectProxy proxy = (ObjectProxy) manyRelatedValues;
Assert.assertFalse(proxy.isLoaded());
Assert.assertEquals(3, manyRelatedValues.size());
for (RelatedEntity relatedEntity : manyRelatedValues) {
Assert.assertNotNull(relatedEntity.getStringValue());
}
}
use of io.crnk.jpa.model.RelatedEntity 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.RelatedEntity in project crnk-framework by crnk-project.
the class RelatedDTOMapper method unmap.
@Override
public RelatedEntity unmap(RelatedDTO dto) {
RelatedEntity entity = em.find(RelatedEntity.class, dto.getId());
if (entity == null) {
entity = new RelatedEntity();
entity.setId(dto.getId());
}
entity.setStringValue(dto.getStringValue());
return entity;
}
use of io.crnk.jpa.model.RelatedEntity in project crnk-framework by crnk-project.
the class JpaRepositoryDecoratorTest method addTestWithOneRelation.
private TestEntity addTestWithOneRelation() {
ResourceRepositoryV2<RelatedEntity, Long> relatedRepo = client.getQuerySpecRepository(RelatedEntity.class);
RelatedEntity related = new RelatedEntity();
related.setId(1L);
related.setStringValue("project");
relatedRepo.create(related);
TestEntity test = new TestEntity();
test.setId(2L);
test.setStringValue("test");
testRepo.create(test);
RelationshipRepositoryV2<TestEntity, Long, RelatedEntity, Long> relRepo = client.getQuerySpecRepository(TestEntity.class, RelatedEntity.class);
relRepo.setRelation(test, related.getId(), TestEntity.ATTR_oneRelatedValue);
return test;
}
Aggregations