use of io.crnk.core.mock.models.RelationIdTestResource in project crnk-framework by crnk-project.
the class ImplicitOwnerBasedRelationshipRepositoryTest method setup.
@Before
public void setup() {
MockRepositoryUtil.clear();
CrnkBoot boot = new CrnkBoot();
boot.setServiceDiscovery(new ReflectionsServiceDiscovery(MockConstants.TEST_MODELS_PACKAGE));
boot.setServiceUrlProvider(new ConstantServiceUrlProvider(ResourceRegistryTest.TEST_MODELS_URL));
boot.boot();
resourceRegistry = boot.getResourceRegistry();
RegistryEntry entry = resourceRegistry.getEntry(RelationIdTestResource.class);
relRepository = new ImplicitOwnerBasedRelationshipRepository(RelationIdTestResource.class, Schedule.class);
relRepository.setResourceRegistry(resourceRegistry);
taskProjectRepository = new ImplicitOwnerBasedRelationshipRepository(Task.class, Project.class);
taskProjectRepository.setResourceRegistry(resourceRegistry);
testRepository = (RelationIdTestRepository) entry.getResourceRepository().getResourceRepository();
testRepository.setResourceRegistry(resourceRegistry);
resource = new RelationIdTestResource();
resource.setId(2L);
resource.setName("relationId");
testRepository.create(resource);
scheduleRepository = new ScheduleRepositoryImpl();
schedule3 = new Schedule();
schedule3.setId(3L);
schedule3.setName("schedule");
scheduleRepository.create(schedule3);
for (int i = 0; i < 10; i++) {
schedule = new Schedule();
schedule.setId(4L + i);
schedule.setName("schedule");
scheduleRepository.create(schedule);
projectRepository = new ProjectRepository();
project = new Project();
project.setId(42L + i);
project.setName("project");
projectRepository.save(project);
taskRepository = new TaskRepository();
task = new Task();
task.setId(13L + i);
task.setName("task");
taskRepository.save(task);
}
}
use of io.crnk.core.mock.models.RelationIdTestResource in project crnk-framework by crnk-project.
the class ResourceIdControllerTest method checkPatchResource.
private Resource checkPatchResource(Resource resource) {
JsonPath path = pathBuilder.build("/relationIdTest/" + resource.getId());
resource.getRelationships().put("testLookupAlways", new Relationship(schedule2Id));
Document newDocument = createDocument(resource);
// WHEN PATCH
ResourcePatch sut = new ResourcePatch(resourceRegistry, new NullPropertiesProvider(), typeParser, objectMapper, documentMapper, modificationFilters);
Response taskResponse = sut.handle(path, emptyTaskQuery, null, newDocument);
// THEN PATCH
assertThat(taskResponse.getHttpStatus()).isEqualTo(HttpStatus.OK_200);
Resource updatedResource = taskResponse.getDocument().getSingleData().get();
Assert.assertEquals(resource.getType(), updatedResource.getType());
Assert.assertEquals(resource.getId(), updatedResource.getId());
Assert.assertEquals(resource.getAttributes().get("name"), updatedResource.getAttributes().get("name"));
Relationship relationship = updatedResource.getRelationships().get("testLookupAlways");
Assert.assertTrue(relationship.getData().isPresent());
Assert.assertEquals(schedule2Id, relationship.getData().get());
// validate relationship not accessed, only id set => performance
RelationIdTestResource entity = repository.findOne(1L, new QuerySpec(RelationIdTestResource.class));
Assert.assertEquals(schedule2.getId(), entity.getTestLookupAlwaysId());
Assert.assertNull(entity.getTestLookupAlways());
return updatedResource;
}
use of io.crnk.core.mock.models.RelationIdTestResource 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.mock.models.RelationIdTestResource 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.mock.models.RelationIdTestResource 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());
}
}
Aggregations