Search in sources :

Example 1 with JsonMetaInformation

use of io.crnk.client.response.JsonMetaInformation in project crnk-framework by crnk-project.

the class ClientDocumentMapper method fromDocument.

public Object fromDocument(Document document, boolean getList) {
    ClientResourceUpsert upsert = new ClientResourceUpsert(resourceRegistry, propertiesProvider, typeParser, objectMapper, null, proxyFactory);
    PreconditionUtil.assertFalse("document contains json api errors and cannot be processed", document.getErrors() != null && !document.getErrors().isEmpty());
    if (!document.getData().isPresent()) {
        return null;
    }
    List<Resource> included = document.getIncluded();
    List<Resource> data = document.getCollectionData().get();
    List<Object> dataObjects = upsert.allocateResources(data);
    if (included != null) {
        upsert.allocateResources(included);
    }
    upsert.setRelations(data);
    if (included != null) {
        upsert.setRelations(included);
    }
    if (getList) {
        DefaultResourceList<Object> resourceList = new DefaultResourceList();
        resourceList.addAll(dataObjects);
        if (document.getLinks() != null) {
            resourceList.setLinks(new JsonLinksInformation(document.getLinks(), objectMapper));
        }
        if (document.getMeta() != null) {
            resourceList.setMeta(new JsonMetaInformation(document.getMeta(), objectMapper));
        }
        return resourceList;
    } else {
        if (dataObjects.isEmpty()) {
            return null;
        }
        PreconditionUtil.assertFalse("expected unique result", dataObjects.size() > 1);
        return dataObjects.get(0);
    }
}
Also used : JsonLinksInformation(io.crnk.client.response.JsonLinksInformation) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) JsonMetaInformation(io.crnk.client.response.JsonMetaInformation) Resource(io.crnk.core.engine.document.Resource)

Example 2 with JsonMetaInformation

use of io.crnk.client.response.JsonMetaInformation 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 3 with JsonMetaInformation

use of io.crnk.client.response.JsonMetaInformation 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 4 with JsonMetaInformation

use of io.crnk.client.response.JsonMetaInformation in project crnk-framework by crnk-project.

the class HasNextPagingEndToEndTest 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.assertNull(meta);
    Assert.assertNotNull(links);
    String baseUri = getBaseUri().toString();
    Assert.assertEquals(baseUri + "test/1/relationships/manyRelatedValues?page[limit]=2", links.asJsonNode().get("first").asText());
    Assert.assertNull(links.asJsonNode().get("last"));
    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) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest)

Example 5 with JsonMetaInformation

use of io.crnk.client.response.JsonMetaInformation in project crnk-framework by crnk-project.

the class HasNextPagingEndToEndTest 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());
    JsonLinksInformation links = list.getLinks(JsonLinksInformation.class);
    String baseUri = getBaseUri().toString();
    Assert.assertEquals(baseUri + "test?page[limit]=2", links.asJsonNode().get("first").asText());
    // not available for hasNext
    Assert.assertNull(links.asJsonNode().get("last"));
    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());
    JsonMetaInformation meta = list.getMeta(JsonMetaInformation.class);
    Assert.assertNull(meta);
}
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) Test(org.junit.Test) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest)

Aggregations

JsonLinksInformation (io.crnk.client.response.JsonLinksInformation)6 JsonMetaInformation (io.crnk.client.response.JsonMetaInformation)6 QuerySpec (io.crnk.core.queryspec.QuerySpec)4 AbstractJpaJerseyTest (io.crnk.jpa.AbstractJpaJerseyTest)4 TestEntity (io.crnk.jpa.model.TestEntity)4 Test (org.junit.Test)4 Resource (io.crnk.core.engine.document.Resource)2 DefaultResourceList (io.crnk.core.resource.list.DefaultResourceList)2 RelatedEntity (io.crnk.jpa.model.RelatedEntity)2 OtherRelatedEntity (io.crnk.jpa.model.OtherRelatedEntity)1 List (java.util.List)1