Search in sources :

Example 6 with TestEntity

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

the class JpaQuerySpecEndToEndTest method testIncludeAndFilterManyRelations.

@Test
public void testIncludeAndFilterManyRelations() throws InstantiationException, IllegalAccessException {
    addTestWithManyRelations();
    QuerySpec querySpec = new QuerySpec(TestEntity.class);
    querySpec.includeRelation(Arrays.asList(TestEntity.ATTR_manyRelatedValues));
    QuerySpec relatedSpec = querySpec.getOrCreateQuerySpec(RelatedEntity.class);
    relatedSpec.addFilter(new FilterSpec(Arrays.asList(RelatedEntity.ATTR_id), FilterOperator.LT, 103L));
    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(2, manyRelatedValues.size());
    for (RelatedEntity manyRelatedValue : manyRelatedValues) {
        Assert.assertTrue(manyRelatedValue.getId() == 101L || manyRelatedValue.getId() == 102L);
    }
}
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) FilterSpec(io.crnk.core.queryspec.FilterSpec) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest) Test(org.junit.Test)

Example 7 with TestEntity

use of io.crnk.jpa.model.TestEntity 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());
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) JsonLinksInformation(io.crnk.client.response.JsonLinksInformation) JsonMetaInformation(io.crnk.client.response.JsonMetaInformation) 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 8 with TestEntity

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

the class JpaQuerySpecEndToEndTest method testRootPaging.

@Test
public void testRootPaging() {
    for (long i = 0; i < 5; i++) {
        TestEntity task = new TestEntity();
        task.setId(i);
        task.setStringValue("test");
        testRepo.create(task);
    }
    QuerySpec querySpec = new QuerySpec(TestEntity.class);
    querySpec.setOffset(2L);
    querySpec.setLimit(2L);
    ResourceList<TestEntity> list = testRepo.findAll(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?page[limit]=2", links.asJsonNode().get("first").asText());
    Assert.assertEquals(baseUri + "test?page[limit]=2&page[offset]=4", links.asJsonNode().get("last").asText());
    Assert.assertEquals(baseUri + "test?page[limit]=2", links.asJsonNode().get("prev").asText());
    Assert.assertEquals(baseUri + "test?page[limit]=2&page[offset]=4", links.asJsonNode().get("next").asText());
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) JsonLinksInformation(io.crnk.client.response.JsonLinksInformation) JsonMetaInformation(io.crnk.client.response.JsonMetaInformation) QuerySpec(io.crnk.core.queryspec.QuerySpec) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest) Test(org.junit.Test)

Example 9 with TestEntity

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

the class JpaQuerySpecEndToEndTest method testSaveAndFind.

@Test
public void testSaveAndFind() {
    TestEntity task = new TestEntity();
    task.setId(1L);
    task.setStringValue("test");
    testRepo.create(task);
    // check retrievable with findAll
    List<TestEntity> list = testRepo.findAll(new QuerySpec(TestEntity.class));
    Assert.assertEquals(1, list.size());
    TestEntity savedTask = list.get(0);
    Assert.assertEquals(task.getId(), savedTask.getId());
    Assert.assertEquals(task.getStringValue(), savedTask.getStringValue());
    // check retrievable with findAll(ids)
    list = testRepo.findAll(Arrays.asList(1L), new QuerySpec(TestEntity.class));
    Assert.assertEquals(1, list.size());
    savedTask = list.get(0);
    Assert.assertEquals(task.getId(), savedTask.getId());
    Assert.assertEquals(task.getStringValue(), savedTask.getStringValue());
    // check retrievable with findOne
    savedTask = testRepo.findOne(1L, new QuerySpec(TestEntity.class));
    Assert.assertEquals(task.getId(), savedTask.getId());
    Assert.assertEquals(task.getStringValue(), savedTask.getStringValue());
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) QuerySpec(io.crnk.core.queryspec.QuerySpec) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest) Test(org.junit.Test)

Example 10 with TestEntity

use of io.crnk.jpa.model.TestEntity 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());
    }
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) RelatedEntity(io.crnk.jpa.model.RelatedEntity) OtherRelatedEntity(io.crnk.jpa.model.OtherRelatedEntity) ObjectProxy(io.crnk.client.internal.proxy.ObjectProxy) QuerySpec(io.crnk.core.queryspec.QuerySpec) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest) Test(org.junit.Test)

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