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);
}
}
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());
}
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());
}
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());
}
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);
}
Aggregations