use of io.crnk.core.engine.document.Resource in project crnk-framework by crnk-project.
the class LookupAlwaysRelationIdLookupTest method check.
private void check(boolean setRelatedEntity, boolean setRelatedId) {
RelationIdTestResource entity = new RelationIdTestResource();
entity.setId(2L);
entity.setName("test");
if (setRelatedId) {
entity.setTestLookupAlwaysId(3L);
}
if (setRelatedEntity) {
entity.setTestLookupAlways(schedule);
}
QuerySpec querySpec = new QuerySpec(RelationIdTestResource.class);
querySpec.includeRelation(Arrays.asList("testLookupAlways"));
Document document = mapper.toDocument(toResponse(entity), toAdapter(querySpec));
Resource resource = document.getSingleData().get();
Assert.assertEquals("2", resource.getId());
Assert.assertEquals("relationIdTest", resource.getType());
Assert.assertEquals("test", resource.getAttributes().get("name").asText());
Nullable<ResourceIdentifier> data = resource.getRelationships().get("testLookupAlways").getSingleData();
Assert.assertTrue(data.isPresent());
if (setRelatedId) {
Assert.assertNotNull(data.get());
Assert.assertEquals(1, document.getIncluded().size());
Assert.assertEquals("3", document.getIncluded().get(0).getId());
Assert.assertEquals(1, scheduleRepository.getNumFindAll());
} else {
Assert.assertNull(data.get());
Assert.assertEquals(0, scheduleRepository.getNumFindAll());
}
}
use of io.crnk.core.engine.document.Resource in project crnk-framework by crnk-project.
the class LookupNotNullRelationIdLookupTest method check.
private void check(boolean setRelatedEntity, boolean setRelatedId) {
RelationIdTestResource entity = new RelationIdTestResource();
entity.setId(2L);
entity.setName("test");
if (setRelatedId) {
entity.setTestLookupWhenNullId(3L);
}
if (setRelatedEntity) {
entity.setTestLookupWhenNull(schedule);
}
QuerySpec querySpec = new QuerySpec(RelationIdTestResource.class);
querySpec.includeRelation(Arrays.asList("testLookupWhenNull"));
Document document = mapper.toDocument(toResponse(entity), toAdapter(querySpec));
Resource resource = document.getSingleData().get();
Assert.assertEquals("2", resource.getId());
Assert.assertEquals("relationIdTest", resource.getType());
Assert.assertEquals("test", resource.getAttributes().get("name").asText());
Nullable<ResourceIdentifier> data = resource.getRelationships().get("testLookupWhenNull").getSingleData();
Assert.assertTrue(data.isPresent());
if (setRelatedId) {
Assert.assertNotNull(data.get());
Assert.assertEquals(1, document.getIncluded().size());
Assert.assertEquals("3", document.getIncluded().get(0).getId());
if (setRelatedEntity) {
Assert.assertEquals(0, scheduleRepository.getNumFindAll());
} else {
Assert.assertEquals(1, scheduleRepository.getNumFindAll());
}
} else {
Assert.assertNull(data.get());
}
}
use of io.crnk.core.engine.document.Resource in project crnk-framework by crnk-project.
the class InconsistentRelationshipTest method check.
private void check(boolean setRelatedEntity, boolean setRelatedId) {
RelationIdTestResource entity = new RelationIdTestResource();
entity.setId(2L);
entity.setName("test");
if (setRelatedId) {
entity.setTestResourceIdRefId(new ResourceIdentifier("3", "schedules"));
}
if (setRelatedEntity) {
entity.setTestResourceIdRef(schedule);
}
QuerySpec querySpec = new QuerySpec(RelationIdTestResource.class);
querySpec.includeRelation(Arrays.asList("testResourceIdRef"));
Document document = mapper.toDocument(toResponse(entity), toAdapter(querySpec));
Resource resource = document.getSingleData().get();
Assert.assertEquals("2", resource.getId());
Assert.assertEquals("relationIdTest", resource.getType());
Assert.assertEquals("test", resource.getAttributes().get("name").asText());
Nullable<ResourceIdentifier> data = resource.getRelationships().get("testResourceIdRef").getSingleData();
Assert.assertTrue(data.isPresent());
if (setRelatedId) {
Assert.assertNotNull(data.get());
Assert.assertEquals(1, document.getIncluded().size());
Assert.assertEquals("3", document.getIncluded().get(0).getId());
Assert.assertEquals(setRelatedEntity ? 0 : 1, scheduleRepository.getNumFindAll());
} else {
Assert.assertNull(data.get());
Assert.assertEquals(0, scheduleRepository.getNumFindAll());
}
}
use of io.crnk.core.engine.document.Resource in project crnk-framework by crnk-project.
the class DynamicRelationshipRepository method createResource.
private Resource createResource() {
ObjectMapper mapper = new ObjectMapper();
Resource resource = new Resource();
resource.setId("john");
resource.setType(resourceType);
try {
resource.getAttributes().put("value", mapper.readTree("\"doe\""));
} catch (IOException e) {
throw new IllegalArgumentException();
}
return resource;
}
use of io.crnk.core.engine.document.Resource in project crnk-framework by crnk-project.
the class ResourceInformationTest method toResourceIdentifier.
@Test
public void toResourceIdentifier() {
Resource resource = new Resource();
resource.setId("13");
resource.setType("tasks");
Task task = new Task();
task.setId(13L);
ResourceIdentifier expected = new ResourceIdentifier("13", "tasks");
Assert.assertSame(expected, sut.toResourceIdentifier(expected));
Assert.assertNull(sut.toResourceIdentifier(null));
Assert.assertEquals(expected, sut.toResourceIdentifier("13"));
Assert.assertEquals(expected, sut.toResourceIdentifier(13L));
Assert.assertEquals(expected, sut.toResourceIdentifier(task));
Assert.assertEquals(expected, sut.toResourceIdentifier(resource));
Assert.assertNotSame(resource, sut.toResourceIdentifier(resource));
}
Aggregations