Search in sources :

Example 1 with TestEntity

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

the class JpaQuerySpecEndToEndTest method testAddManyRelation.

private void testAddManyRelation(boolean onSave) throws InstantiationException, IllegalAccessException {
    ResourceRepositoryV2<RelatedEntity, Long> relatedRepo = client.getQuerySpecRepository(RelatedEntity.class);
    RelatedEntity related1 = new RelatedEntity();
    related1.setId(1L);
    related1.setStringValue("related1");
    relatedRepo.create(related1);
    RelatedEntity related2 = new RelatedEntity();
    related2.setId(2L);
    related2.setStringValue("related2");
    relatedRepo.create(related2);
    TestEntity test = new TestEntity();
    test.setId(3L);
    test.setStringValue("test");
    if (onSave) {
        test.setManyRelatedValues(Arrays.asList(related1, related2));
    }
    testRepo.create(test);
    // query relation
    RelationshipRepositoryV2<TestEntity, Long, RelatedEntity, Long> relRepo = client.getQuerySpecRepository(TestEntity.class, RelatedEntity.class);
    if (!onSave) {
        relRepo.addRelations(test, Arrays.asList(1L, 2L), TestEntity.ATTR_manyRelatedValues);
    }
    List<RelatedEntity> related = relRepo.findManyTargets(test.getId(), TestEntity.ATTR_manyRelatedValues, new QuerySpec(RelatedEntity.class));
    Assert.assertEquals(2, related.size());
    // query relation in opposite direction
    RelationshipRepositoryV2<RelatedEntity, Serializable, TestEntity, Serializable> backRelRepo = client.getQuerySpecRepository(RelatedEntity.class, TestEntity.class);
    test = backRelRepo.findOneTarget(2L, RelatedEntity.ATTR_testEntity, new QuerySpec(TestEntity.class));
    Assert.assertNotNull(test);
    Assert.assertEquals(3L, test.getId().longValue());
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) Serializable(java.io.Serializable) RelatedEntity(io.crnk.jpa.model.RelatedEntity) OtherRelatedEntity(io.crnk.jpa.model.OtherRelatedEntity) QuerySpec(io.crnk.core.queryspec.QuerySpec)

Example 2 with TestEntity

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

the class JpaQuerySpecEndToEndTest method addTestWithManyRelations.

private TestEntity addTestWithManyRelations() {
    ResourceRepositoryV2<OtherRelatedEntity, Long> otherRepo = client.getQuerySpecRepository(OtherRelatedEntity.class);
    ResourceRepositoryV2<RelatedEntity, Long> relatedRepo = client.getQuerySpecRepository(RelatedEntity.class);
    RelationshipRepositoryV2<TestEntity, Long, RelatedEntity, Long> relRepo = client.getQuerySpecRepository(TestEntity.class, RelatedEntity.class);
    RelationshipRepositoryV2<RelatedEntity, Long, OtherRelatedEntity, Long> otherRelRepo = client.getQuerySpecRepository(RelatedEntity.class, OtherRelatedEntity.class);
    TestEntity test = new TestEntity();
    test.setId(2L);
    test.setStringValue("test");
    testRepo.create(test);
    RelatedEntity related1 = new RelatedEntity();
    related1.setId(101L);
    related1.setStringValue("related1");
    relatedRepo.create(related1);
    RelatedEntity related2 = new RelatedEntity();
    related2.setId(102L);
    related2.setStringValue("related2");
    relatedRepo.create(related2);
    RelatedEntity related3 = new RelatedEntity();
    related3.setId(103L);
    related3.setStringValue("related3");
    relatedRepo.create(related3);
    OtherRelatedEntity other1 = new OtherRelatedEntity();
    other1.setId(101L);
    other1.setStringValue("related1");
    otherRepo.create(other1);
    OtherRelatedEntity other2 = new OtherRelatedEntity();
    other2.setId(102L);
    other2.setStringValue("related2");
    otherRepo.create(other2);
    OtherRelatedEntity other3 = new OtherRelatedEntity();
    other3.setId(103L);
    other3.setStringValue("related3");
    otherRepo.create(other3);
    List<Long> relatedIds = Arrays.asList(related1.getId(), related2.getId(), related3.getId());
    relRepo.addRelations(test, relatedIds, TestEntity.ATTR_manyRelatedValues);
    otherRelRepo.setRelation(related1, other1.getId(), RelatedEntity.ATTR_otherEntity);
    otherRelRepo.setRelation(related2, other2.getId(), RelatedEntity.ATTR_otherEntity);
    otherRelRepo.setRelation(related3, other3.getId(), RelatedEntity.ATTR_otherEntity);
    return test;
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) OtherRelatedEntity(io.crnk.jpa.model.OtherRelatedEntity) RelatedEntity(io.crnk.jpa.model.RelatedEntity) OtherRelatedEntity(io.crnk.jpa.model.OtherRelatedEntity)

Example 3 with TestEntity

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

the class JpaQuerySpecEndToEndTest method testFindEmpty.

@Test
public void testFindEmpty() {
    List<TestEntity> list = testRepo.findAll(new QuerySpec(TestEntity.class));
    Assert.assertTrue(list.isEmpty());
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) QuerySpec(io.crnk.core.queryspec.QuerySpec) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest) Test(org.junit.Test)

Example 4 with TestEntity

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

the class JpaQuerySpecEndToEndTest method testIncludeNested.

@Test
public void testIncludeNested() throws InstantiationException, IllegalAccessException {
    addTestWithManyRelations();
    QuerySpec querySpec = new QuerySpec(TestEntity.class);
    querySpec.includeRelation(Arrays.asList(TestEntity.ATTR_manyRelatedValues, RelatedEntity.ATTR_otherEntity));
    List<TestEntity> list = testRepo.findAll(querySpec);
    Assert.assertEquals(1, list.size());
    TestEntity testEntity = list.get(0);
    List<RelatedEntity> manyRelatedValues = testEntity.getManyRelatedValues();
    Assert.assertNotNull(manyRelatedValues);
    Assert.assertEquals(3, manyRelatedValues.size());
    for (RelatedEntity relatedEntity : manyRelatedValues) {
        Assert.assertNotNull(relatedEntity.getOtherEntity());
    }
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) RelatedEntity(io.crnk.jpa.model.RelatedEntity) OtherRelatedEntity(io.crnk.jpa.model.OtherRelatedEntity) QuerySpec(io.crnk.core.queryspec.QuerySpec) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest) Test(org.junit.Test)

Example 5 with TestEntity

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

the class JpaQuerySpecEndToEndTest 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");
    test.setOneRelatedValue(related);
    testRepo.create(test);
    return test;
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) RelatedEntity(io.crnk.jpa.model.RelatedEntity) OtherRelatedEntity(io.crnk.jpa.model.OtherRelatedEntity)

Aggregations

TestEntity (io.crnk.jpa.model.TestEntity)61 Test (org.junit.Test)50 AbstractJpaJerseyTest (io.crnk.jpa.AbstractJpaJerseyTest)28 QuerySpec (io.crnk.core.queryspec.QuerySpec)25 RelatedEntity (io.crnk.jpa.model.RelatedEntity)24 AbstractJpaTest (io.crnk.jpa.query.AbstractJpaTest)16 OtherRelatedEntity (io.crnk.jpa.model.OtherRelatedEntity)12 QueryParams (io.crnk.legacy.queryParams.QueryParams)6 CollectionAttributesTestEntity (io.crnk.jpa.model.CollectionAttributesTestEntity)5 UuidTestEntity (io.crnk.jpa.model.UuidTestEntity)5 Serializable (java.io.Serializable)5 JsonLinksInformation (io.crnk.client.response.JsonLinksInformation)4 JsonMetaInformation (io.crnk.client.response.JsonMetaInformation)4 PagedMetaInformation (io.crnk.core.resource.meta.PagedMetaInformation)4 TestDTO (io.crnk.jpa.model.dto.TestDTO)2 ObjectProxy (io.crnk.client.internal.proxy.ObjectProxy)1 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 FilterSpec (io.crnk.core.queryspec.FilterSpec)1 JpaEntityRepository (io.crnk.jpa.JpaEntityRepository)1 JpaRepositoryFilterBase (io.crnk.jpa.JpaRepositoryFilterBase)1