use of io.crnk.jpa.model.RelatedEntity in project crnk-framework by crnk-project.
the class JpaRelationshipRepositoryTestBase method testFindNulledOneTargetWithLimit.
/**
* Note that implementation behaves slightly differently with a LIMIT in place. Does only work for single requests.
*/
@Test
public void testFindNulledOneTargetWithLimit() throws InstantiationException, IllegalAccessException {
long nulledEntityId = numTestEntities - 1;
QuerySpec querySpec = new QuerySpec(RelatedEntity.class);
querySpec.setLimit(5L);
RelatedEntity relatedEntity = repo.findOneTarget(nulledEntityId, TestEntity.ATTR_oneRelatedValue, querySpec);
Assert.assertNull(relatedEntity);
ResourceList<RelatedEntity> list = repo.findManyTargets(nulledEntityId, TestEntity.ATTR_oneRelatedValue, querySpec);
Assert.assertEquals(0, list.size());
PagedMetaInformation pagedMeta = list.getMeta(PagedMetaInformation.class);
Assert.assertNotNull(pagedMeta.getTotalResourceCount());
Assert.assertEquals(Long.valueOf(0L), pagedMeta.getTotalResourceCount());
}
use of io.crnk.jpa.model.RelatedEntity in project crnk-framework by crnk-project.
the class JpaRelationshipRepositoryTestBase method testAddRemoveRelations.
@Test
public void testAddRemoveRelations() throws InstantiationException, IllegalAccessException {
TestEntity test = em.find(TestEntity.class, 1L);
Assert.assertEquals(0, test.getManyRelatedValues().size());
repo.addRelations(test, Arrays.asList(101L), TestEntity.ATTR_manyRelatedValues);
em.flush();
em.clear();
test = em.find(TestEntity.class, 1L);
Assert.assertEquals(1, test.getManyRelatedValues().size());
RelatedEntity relatedEntity = test.getManyRelatedValues().iterator().next();
Assert.assertEquals(101L, relatedEntity.getId().longValue());
// add second relations
repo.addRelations(test, Arrays.asList(102L), TestEntity.ATTR_manyRelatedValues);
em.flush();
em.clear();
test = em.find(TestEntity.class, 1L);
Assert.assertEquals(2, test.getManyRelatedValues().size());
// remove relation
repo.removeRelations(test, Arrays.asList(102L), TestEntity.ATTR_manyRelatedValues);
em.flush();
em.clear();
test = em.find(TestEntity.class, 1L);
Assert.assertEquals(1, test.getManyRelatedValues().size());
// remove relation
repo.removeRelations(test, Arrays.asList(101L), TestEntity.ATTR_manyRelatedValues);
em.flush();
em.clear();
test = em.find(TestEntity.class, 1L);
Assert.assertEquals(0, test.getManyRelatedValues().size());
// set relations
repo.setRelations(test, Arrays.asList(101L, 102L), TestEntity.ATTR_manyRelatedValues);
em.flush();
em.clear();
test = em.find(TestEntity.class, 1L);
Assert.assertEquals(2, test.getManyRelatedValues().size());
// set relations
repo.setRelations(test, Arrays.asList(101L), TestEntity.ATTR_manyRelatedValues);
em.flush();
em.clear();
test = em.find(TestEntity.class, 1L);
Assert.assertEquals(1, test.getManyRelatedValues().size());
RelatedEntity related = test.getManyRelatedValues().iterator().next();
Assert.assertEquals(101L, related.getId().longValue());
}
use of io.crnk.jpa.model.RelatedEntity in project crnk-framework by crnk-project.
the class JpaRelationshipRepositoryTestBase method testFindOneTarget.
@Test
public void testFindOneTarget() throws InstantiationException, IllegalAccessException {
RelatedEntity relatedEntity = repo.findOneTarget(1L, TestEntity.ATTR_oneRelatedValue, new QuerySpec(RelatedEntity.class));
Assert.assertNotNull(relatedEntity);
Assert.assertEquals(101L, relatedEntity.getId().longValue());
}
use of io.crnk.jpa.model.RelatedEntity 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.jpa.model.RelatedEntity 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));
}
Aggregations