Search in sources :

Example 31 with RelatedEntity

use of io.crnk.jpa.model.RelatedEntity in project crnk-framework by crnk-project.

the class JpaRelationshipRepositoryTestBase method testFindManyTarget.

@Test
public void testFindManyTarget() throws InstantiationException, IllegalAccessException {
    Iterable<RelatedEntity> relatedEntities = repo.findManyTargets(1L, TestEntity.ATTR_oneRelatedValue, new QuerySpec(RelatedEntity.class));
    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) Test(org.junit.Test) AbstractJpaTest(io.crnk.jpa.query.AbstractJpaTest)

Example 32 with RelatedEntity

use of io.crnk.jpa.model.RelatedEntity in project crnk-framework by crnk-project.

the class JpaRelationshipRepositoryTestBase method testSetRelation.

@Test
public void testSetRelation() throws InstantiationException, IllegalAccessException {
    RelatedEntity related = em.find(RelatedEntity.class, 101L);
    TestEntity test = em.find(TestEntity.class, 1L);
    Assert.assertNull(related.getTestEntity());
    relatedRepo.setRelation(related, 1L, RelatedEntity.ATTR_testEntity);
    em.flush();
    em.clear();
    test = em.find(TestEntity.class, 1L);
    Assert.assertEquals(1, test.getManyRelatedValues().size());
    related = em.find(RelatedEntity.class, 101L);
    Assert.assertNotNull(related.getTestEntity());
    // test set null
    relatedRepo.setRelation(related, null, RelatedEntity.ATTR_testEntity);
    em.flush();
    em.clear();
    test = em.find(TestEntity.class, 1L);
    Assert.assertEquals(0, test.getManyRelatedValues().size());
    related = em.find(RelatedEntity.class, 101L);
    Assert.assertNull(related.getTestEntity());
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) RelatedEntity(io.crnk.jpa.model.RelatedEntity) Test(org.junit.Test) AbstractJpaTest(io.crnk.jpa.query.AbstractJpaTest)

Example 33 with RelatedEntity

use of io.crnk.jpa.model.RelatedEntity in project crnk-framework by crnk-project.

the class JpaRelationshipRepositoryTestBase method testFindManyTargetWithUnmatchedFilter.

@Test
public void testFindManyTargetWithUnmatchedFilter() throws InstantiationException, IllegalAccessException {
    QuerySpec querySpec = new QuerySpec(RelatedEntity.class);
    querySpec.addFilter(new FilterSpec(Arrays.asList("id"), FilterOperator.EQ, 9999));
    Iterable<RelatedEntity> relatedEntities = repo.findManyTargets(1L, TestEntity.ATTR_oneRelatedValue, querySpec);
    Iterator<RelatedEntity> iterator = relatedEntities.iterator();
    Assert.assertFalse(iterator.hasNext());
}
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 34 with RelatedEntity

use of io.crnk.jpa.model.RelatedEntity in project crnk-framework by crnk-project.

the class JpaRelationshipRepositoryTestBase method testGetManyRelationWithPaging.

@Test
public void testGetManyRelationWithPaging() throws InstantiationException, IllegalAccessException {
    TestEntity test = setupManyRelation(Arrays.asList(100L, 101L, 102L, 103L, 104L));
    QuerySpec querySpec = new QuerySpec(TestEntity.class);
    querySpec.setOffset(2L);
    querySpec.setLimit(2L);
    ResourceList<RelatedEntity> list = repo.findManyTargets(test.getId(), TestEntity.ATTR_manyRelatedValues, querySpec);
    Assert.assertEquals(2, list.size());
    Assert.assertEquals(102, list.get(0).getId().intValue());
    Assert.assertEquals(103, list.get(1).getId().intValue());
    PagedMetaInformation metaInformation = list.getMeta(PagedMetaInformation.class);
    Assert.assertEquals(5, metaInformation.getTotalResourceCount().longValue());
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) PagedMetaInformation(io.crnk.core.resource.meta.PagedMetaInformation) RelatedEntity(io.crnk.jpa.model.RelatedEntity) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test) AbstractJpaTest(io.crnk.jpa.query.AbstractJpaTest)

Example 35 with RelatedEntity

use of io.crnk.jpa.model.RelatedEntity in project crnk-framework by crnk-project.

the class JpaEntityRepositoryTestBase method testIncludeNoRelations.

@Test
public void testIncludeNoRelations() throws InstantiationException, IllegalAccessException {
    em.clear();
    List<TestEntity> list = repo.findAll(new QuerySpec(TestEntity.class));
    Assert.assertEquals(numTestEntities, list.size());
    for (TestEntity entity : list) {
        RelatedEntity relatedValue = entity.getOneRelatedValue();
        if (relatedValue != null)
            Assert.assertFalse(Hibernate.isInitialized(relatedValue));
    }
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) RelatedEntity(io.crnk.jpa.model.RelatedEntity) Test(org.junit.Test) AbstractJpaTest(io.crnk.jpa.query.AbstractJpaTest)

Aggregations

RelatedEntity (io.crnk.jpa.model.RelatedEntity)35 Test (org.junit.Test)27 TestEntity (io.crnk.jpa.model.TestEntity)24 QuerySpec (io.crnk.core.queryspec.QuerySpec)21 AbstractJpaJerseyTest (io.crnk.jpa.AbstractJpaJerseyTest)13 OtherRelatedEntity (io.crnk.jpa.model.OtherRelatedEntity)13 AbstractJpaTest (io.crnk.jpa.query.AbstractJpaTest)11 Serializable (java.io.Serializable)6 FilterSpec (io.crnk.core.queryspec.FilterSpec)3 JsonLinksInformation (io.crnk.client.response.JsonLinksInformation)2 JsonMetaInformation (io.crnk.client.response.JsonMetaInformation)2 PagedMetaInformation (io.crnk.core.resource.meta.PagedMetaInformation)2 CollectionAttributesTestEntity (io.crnk.jpa.model.CollectionAttributesTestEntity)2 UuidTestEntity (io.crnk.jpa.model.UuidTestEntity)2 ObjectProxy (io.crnk.client.internal.proxy.ObjectProxy)1 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 OneToOneTestEntity (io.crnk.jpa.model.OneToOneTestEntity)1 TestSubclassWithSuperclassPk (io.crnk.jpa.model.TestSubclassWithSuperclassPk)1 RelatedDTO (io.crnk.jpa.model.dto.RelatedDTO)1 QueryParams (io.crnk.legacy.queryParams.QueryParams)1