use of io.crnk.core.engine.document.ResourceIdentifier in project crnk-framework by crnk-project.
the class SerializeEagerRelationIdLookupTest method check.
private void check(boolean setRelatedEntity, boolean setRelatedId) {
RelationIdTestResource entity = new RelationIdTestResource();
entity.setId(2L);
entity.setName("test");
if (setRelatedId) {
entity.setTestSerializeEagerId(3L);
}
if (setRelatedEntity) {
entity.setTestSerializeEager(schedule);
}
QuerySpec querySpec = new QuerySpec(RelationIdTestResource.class);
// since eager no inclusion necessary
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("testSerializeEager").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.ResourceIdentifier in project crnk-framework by crnk-project.
the class RelationIdClientTest method checkResourceIdentifierField.
@Test
public void checkResourceIdentifierField() {
Schedule schedule = new Schedule();
schedule.setId(13L);
schedule.setName("mySchedule");
Schedule savedSchedule = scheduleRepo.create(schedule);
RelationIdTestResource resource = new RelationIdTestResource();
resource.setId(14L);
resource.setName("test");
resource.setTestResourceIdRefId(new ResourceIdentifier("13", "schedules"));
ResourceRepositoryV2<RelationIdTestResource, Serializable> repository = client.getRepositoryForType(RelationIdTestResource.class);
RelationIdTestResource createdResource = repository.create(resource);
Assert.assertEquals(resource.getTestResourceIdRefId(), createdResource.getTestResourceIdRefId());
RelationIdTestResource serverResource = RelationIdTestRepository.resources.get(14L);
Assert.assertEquals(resource.getTestResourceIdRefId(), serverResource.getTestResourceIdRefId());
QuerySpec querySpec = new QuerySpec(RelationIdTestResource.class);
RelationIdTestResource getResource = repository.findOne(14L, querySpec);
Assert.assertNull(getResource.getTestResourceIdRefId());
Assert.assertNull(createdResource.getTestResourceIdRef());
querySpec = new QuerySpec(RelationIdTestResource.class);
querySpec.includeRelation(Arrays.asList("testResourceIdRef"));
getResource = repository.findOne(14L, querySpec);
Assert.assertEquals(resource.getTestResourceIdRefId(), getResource.getTestResourceIdRefId());
Assert.assertNotNull(getResource.getTestResourceIdRef());
}
use of io.crnk.core.engine.document.ResourceIdentifier in project crnk-framework by crnk-project.
the class RelationshipRepositoryStubImpl method executeWithIds.
private void executeWithIds(String requestUrl, HttpMethod method, Iterable<?> targetIds) {
Document document = new Document();
ArrayList<ResourceIdentifier> resourceIdentifiers = new ArrayList<>();
for (Object targetId : targetIds) {
resourceIdentifiers.add(sourceResourceInformation.toResourceIdentifier(targetId));
}
document.setData(Nullable.of((Object) resourceIdentifiers));
doExecute(requestUrl, method, document);
}
use of io.crnk.core.engine.document.ResourceIdentifier in project crnk-framework by crnk-project.
the class IncludeLookupSetterInheritanceTest method testPolymorhRelationship.
@Test
public void testPolymorhRelationship() throws Exception {
QuerySpec querySpec = new QuerySpec(Task.class);
querySpec.includeRelation(Arrays.asList("includedProjects"));
Task task = new Task();
task.setId(1L);
Document document = mapper.toDocument(toResponse(task), toAdapter(querySpec));
Resource taskResource = document.getSingleData().get();
Relationship relationship = taskResource.getRelationships().get("includedProjects");
assertNotNull(relationship);
List<ResourceIdentifier> projects = relationship.getCollectionData().get();
assertEquals(2, projects.size());
List<Resource> included = document.getIncluded();
assertEquals(2, included.size());
Assert.assertEquals("projects", included.get(1).getType());
Assert.assertEquals("fancy-projects", included.get(0).getType());
}
use of io.crnk.core.engine.document.ResourceIdentifier in project crnk-framework by crnk-project.
the class LookupNoneRelationIdLookupTest method check.
private void check(boolean setRelatedEntity, boolean setRelatedId) {
RelationIdTestResource entity = new RelationIdTestResource();
entity.setId(2L);
entity.setName("test");
if (setRelatedId) {
entity.setTestLookupNoneId(3L);
}
if (setRelatedEntity) {
entity.setTestLookupNone(schedule);
}
QuerySpec querySpec = new QuerySpec(RelationIdTestResource.class);
querySpec.includeRelation(Arrays.asList("testLookupNone"));
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("testLookupNone").getSingleData();
Assert.assertTrue(data.isPresent());
Assert.assertEquals(0, scheduleRepository.getNumFindAll());
if (setRelatedEntity) {
Assert.assertNotNull(data.get());
Assert.assertEquals(1, document.getIncluded().size());
Assert.assertEquals("3", document.getIncluded().get(0).getId());
} else if (setRelatedId) {
Assert.fail("without lookup related entity should be set if related id is set");
} else {
Assert.assertNull(data.get());
}
}
Aggregations