use of io.crnk.core.engine.internal.dispatcher.path.JsonPath in project crnk-framework by crnk-project.
the class RelationshipsResourcePatchTest method supportPolymorphicRelationshipTypes.
@Test
public void supportPolymorphicRelationshipTypes() {
// GIVEN
Document newTaskBody = new Document();
Resource data = new Resource();
data.setType(ClassUtils.getAnnotation(Task.class, JsonApiResource.class).get().type());
newTaskBody.setData(Nullable.of((Object) data));
JsonPath taskPath = pathBuilder.build("/tasks");
ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
Response taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
Long taskIdOne = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
assertThat(taskIdOne).isNotNull();
taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
Long taskIdTwo = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
assertThat(taskIdOne).isNotNull();
taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
Long taskIdThree = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
assertThat(taskIdOne).isNotNull();
newTaskBody = new Document();
// Create ProjectPolymorphic object
Document newProjectBody = new Document();
data = new Resource();
String type = ClassUtils.getAnnotation(ProjectPolymorphic.class, JsonApiResource.class).get().type();
data.setType(type);
data.getRelationships().put("task", new Relationship(new ResourceIdentifier(taskIdOne.toString(), "tasks")));
data.getRelationships().put("tasks", new Relationship(Arrays.asList(new ResourceIdentifier(taskIdTwo.toString(), "tasks"), new ResourceIdentifier(taskIdThree.toString(), "tasks"))));
newProjectBody.setData(Nullable.of((Object) data));
JsonPath projectPolymorphicTypePath = pathBuilder.build("/" + type);
Response projectResponse = resourcePost.handle(projectPolymorphicTypePath, emptyProjectQuery, null, newProjectBody);
assertThat(projectResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects-polymorphic");
Long projectId = Long.parseLong(projectResponse.getDocument().getSingleData().get().getId());
assertThat(projectId).isNotNull();
Resource projectPolymorphic = projectResponse.getDocument().getSingleData().get();
assertNotNull(projectPolymorphic.getRelationships().get("task").getSingleData().get());
assertNotNull(projectPolymorphic.getRelationships().get("tasks"));
ProjectPolymorphicRepository resourceRepository = (ProjectPolymorphicRepository) resourceRegistry.getEntry(ProjectPolymorphic.class).getResourceRepository(null).getResourceRepository();
ProjectPolymorphic projectPolymorphicObj = resourceRepository.findOne(projectId, null);
assertEquals(2, projectPolymorphicObj.getTasks().size());
projectPolymorphicTypePath = pathBuilder.build("/" + type + "/" + projectPolymorphic.getId());
ResourcePatch resourcePatch = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
data = newProjectBody.getSingleData().get();
data.setId(projectId.toString());
projectPolymorphic.setId(Long.toString(projectId));
data.getRelationships().get("tasks").setData(Nullable.of((Object) new ArrayList<ResourceIdentifier>()));
// WHEN
Response baseResponseContext = resourcePatch.handle(projectPolymorphicTypePath, new QuerySpecAdapter(new QuerySpec(ProjectPolymorphic.class), resourceRegistry), null, newProjectBody);
assertThat(baseResponseContext.getDocument().getSingleData().get().getType()).isEqualTo("projects-polymorphic");
projectId = Long.parseLong(baseResponseContext.getDocument().getSingleData().get().getId());
assertThat(projectId).isNotNull();
projectPolymorphic = baseResponseContext.getDocument().getSingleData().get();
assertNotNull(projectPolymorphic.getRelationships().get("task").getSingleData().get());
assertNotNull(projectPolymorphic.getRelationships().get("tasks"));
projectPolymorphicObj = resourceRepository.findOne(projectId, null);
assertEquals(0, projectPolymorphicObj.getTasks().size());
}
use of io.crnk.core.engine.internal.dispatcher.path.JsonPath in project crnk-framework by crnk-project.
the class RelationshipsResourcePatchTest method onExistingResourcesShouldAddToManyRelationship.
@Test
public void onExistingResourcesShouldAddToManyRelationship() throws Exception {
// GIVEN
Document newUserBody = new Document();
Resource data = createUser();
newUserBody.setData(Nullable.of((Object) data));
JsonPath taskPath = pathBuilder.build("/users");
ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, OBJECT_MAPPER, documentMapper, modificationFilters);
// WHEN -- adding a user
Response taskResponse = resourcePost.handle(taskPath, emptyUserQuery, null, newUserBody);
// THEN
assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("users");
Long userId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
assertThat(userId).isNotNull();
/* ------- */
// GIVEN
Document newProjectBody = new Document();
data = createProject();
newProjectBody.setData(Nullable.of((Object) data));
JsonPath projectPath = pathBuilder.build("/projects");
// WHEN -- adding a project
Response projectResponse = resourcePost.handle(projectPath, emptyProjectQuery, null, newProjectBody);
// THEN
assertThat(projectResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects");
assertThat(projectResponse.getDocument().getSingleData().get().getId()).isNotNull();
assertThat(projectResponse.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("sample project");
Long projectId = Long.parseLong(projectResponse.getDocument().getSingleData().get().getId());
assertThat(projectId).isNotNull();
/* ------- */
// GIVEN
Document newTaskToProjectBody = new Document();
data = createProject();
data.setId(projectId.toString());
newTaskToProjectBody.setData(Nullable.of((Object) Collections.singletonList(data)));
JsonPath savedTaskPath = pathBuilder.build("/users/" + userId + "/relationships/assignedProjects");
RelationshipsResourcePatch sut = new RelationshipsResourcePatch(resourceRegistry, typeParser, modificationFilters);
// WHEN -- adding a relation between user and project
Response projectRelationshipResponse = sut.handle(savedTaskPath, emptyProjectQuery, null, newTaskToProjectBody);
assertThat(projectRelationshipResponse).isNotNull();
// THEN
UserToProjectRepository userToProjectRepository = new UserToProjectRepository();
Project project = userToProjectRepository.findOneTarget(userId, "assignedProjects", new QuerySpec(Project.class));
assertThat(project.getId()).isEqualTo(projectId);
}
use of io.crnk.core.engine.internal.dispatcher.path.JsonPath in project crnk-framework by crnk-project.
the class ResourceDeleteTest method onGivenRequestResourceGetShouldHandleIt.
@Test
public void onGivenRequestResourceGetShouldHandleIt() throws Exception {
// GIVEN
JsonPath jsonPath = pathBuilder.build("/tasks/1");
ResourceDelete sut = new ResourceDelete(resourceRegistry, new ArrayList<ResourceModificationFilter>());
// WHEN
Response response = sut.handle(jsonPath, emptyTaskQuery, null, null);
// THEN
assertThat(response.getDocument()).isNull();
}
use of io.crnk.core.engine.internal.dispatcher.path.JsonPath in project crnk-framework by crnk-project.
the class ResourceDeleteTest method onValidRequestShouldAcceptIt.
@Test
public void onValidRequestShouldAcceptIt() {
// GIVEN
JsonPath jsonPath = pathBuilder.build("tasks/1");
ResourceRegistry resourceRegistry = mock(ResourceRegistry.class);
ResourceDelete sut = new ResourceDelete(resourceRegistry, new ArrayList<ResourceModificationFilter>());
// WHEN
boolean result = sut.isAcceptable(jsonPath, REQUEST_TYPE);
// THEN
assertThat(result).isTrue();
}
use of io.crnk.core.engine.internal.dispatcher.path.JsonPath in project crnk-framework by crnk-project.
the class ResourceDeleteTest method onNonRelationRequestShouldDenyIt.
@Test
public void onNonRelationRequestShouldDenyIt() {
// GIVEN
JsonPath jsonPath = new ResourcePath("tasks/1/relationships/project");
ResourceRegistry resourceRegistry = mock(ResourceRegistry.class);
ResourceDelete sut = new ResourceDelete(resourceRegistry, new ArrayList<ResourceModificationFilter>());
// WHEN
boolean result = sut.isAcceptable(jsonPath, REQUEST_TYPE);
// THEN
assertThat(result).isFalse();
}
Aggregations