Search in sources :

Example 11 with RelatedEntity

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());
}
Also used : 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 12 with RelatedEntity

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());
}
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 13 with RelatedEntity

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());
}
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 14 with RelatedEntity

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());
}
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 15 with RelatedEntity

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));
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) RelatedEntity(io.crnk.jpa.model.RelatedEntity) ArrayList(java.util.ArrayList) QuerySpec(io.crnk.core.queryspec.QuerySpec) 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