Search in sources :

Example 66 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec 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 67 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec 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 68 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec 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 69 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec 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 70 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.

the class JpaQuerySpecEndToEndTest method testEmbeddableIds.

@Test
public void testEmbeddableIds() throws InstantiationException, IllegalAccessException {
    ResourceRepositoryV2<TestEmbeddedIdEntity, Serializable> rep = client.getQuerySpecRepository(TestEmbeddedIdEntity.class);
    // add
    TestEmbeddedIdEntity entity = new TestEmbeddedIdEntity();
    entity.setId(new TestIdEmbeddable(13, "test"));
    entity.setLongValue(100L);
    rep.create(entity);
    List<TestEmbeddedIdEntity> list = rep.findAll(new QuerySpec(TestEntity.class));
    Assert.assertEquals(1, list.size());
    TestEmbeddedIdEntity savedEntity = list.get(0);
    Assert.assertNotNull(savedEntity);
    Assert.assertEquals(100L, savedEntity.getLongValue());
    Assert.assertEquals(13, savedEntity.getId().getEmbIntValue().intValue());
    Assert.assertEquals("test", savedEntity.getId().getEmbStringValue());
    // update
    savedEntity.setLongValue(101L);
    rep.save(savedEntity);
    list = rep.findAll(new QuerySpec(TestEntity.class));
    Assert.assertEquals(1, list.size());
    savedEntity = list.get(0);
    Assert.assertEquals(101L, savedEntity.getLongValue());
    // delete
    rep.delete(entity.getId());
    list = rep.findAll(new QuerySpec(TestEntity.class));
    Assert.assertEquals(0, list.size());
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) Serializable(java.io.Serializable) TestIdEmbeddable(io.crnk.jpa.model.TestIdEmbeddable) TestEmbeddedIdEntity(io.crnk.jpa.model.TestEmbeddedIdEntity) QuerySpec(io.crnk.core.queryspec.QuerySpec) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest) Test(org.junit.Test)

Aggregations

QuerySpec (io.crnk.core.queryspec.QuerySpec)306 Test (org.junit.Test)233 FilterSpec (io.crnk.core.queryspec.FilterSpec)51 Document (io.crnk.core.engine.document.Document)45 Resource (io.crnk.core.engine.document.Resource)43 Set (java.util.Set)39 HashMap (java.util.HashMap)37 HashSet (java.util.HashSet)36 AbstractQuerySpecTest (io.crnk.core.queryspec.AbstractQuerySpecTest)34 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)32 AbstractJpaJerseyTest (io.crnk.jpa.AbstractJpaJerseyTest)32 Task (io.crnk.test.mock.models.Task)32 Project (io.crnk.core.mock.models.Project)28 Relationship (io.crnk.core.engine.document.Relationship)26 Task (io.crnk.core.mock.models.Task)26 TestEntity (io.crnk.jpa.model.TestEntity)26 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)25 Serializable (java.io.Serializable)24 RelatedEntity (io.crnk.jpa.model.RelatedEntity)21 ResourceRegistryTest (io.crnk.core.resource.registry.ResourceRegistryTest)20