use of io.crnk.core.engine.dispatcher.Response in project crnk-framework by crnk-project.
the class MetaModule method setupModule.
@Override
public void setupModule(ModuleContext context) {
this.context = context;
informationBuilder = registerInformationBuilder(context.getPropertiesProvider());
if (context.isServer()) {
context.addFilter(new DocumentFilter() {
@Override
public Response filter(DocumentFilterContext filterRequestContext, DocumentFilterChain chain) {
try {
return chain.doFilter(filterRequestContext);
} finally {
lookupRequestLocal.remove();
}
}
});
} else {
context.addResourceLookup(new ResourceLookup() {
@SuppressWarnings("unchecked")
@Override
public Set<Class<?>> getResourceClasses() {
return (Set) collectMetaClasses();
}
});
}
}
use of io.crnk.core.engine.dispatcher.Response in project crnk-framework by crnk-project.
the class RelationshipsResourceDeleteTest method onExistingToOneRelationshipShouldRemoveIt.
@Test
public void onExistingToOneRelationshipShouldRemoveIt() throws Exception {
// GIVEN
Document newTaskBody = new Document();
Resource data = createTask();
newTaskBody.setData(Nullable.of((Object) data));
data.setType("tasks");
JsonPath taskPath = pathBuilder.build("/tasks");
ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, OBJECT_MAPPER, documentMapper, modificationFilters);
// WHEN -- adding a task
Response taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
// THEN
assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
Long taskId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
assertThat(taskId).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 = new Resource();
newTaskToProjectBody.setData(Nullable.of((Object) data));
data.setType("projects");
data.setId(projectId.toString());
JsonPath projectRelationPath = pathBuilder.build("/tasks/" + taskId + "/relationships/project");
RelationshipsResourcePost relationshipsResourcePost = new RelationshipsResourcePost(resourceRegistry, typeParser, modificationFilters);
// WHEN -- adding a relation between task and project
Response projectRelationshipResponse = relationshipsResourcePost.handle(projectRelationPath, emptyProjectQuery, null, newTaskToProjectBody);
assertThat(projectRelationshipResponse).isNotNull();
// THEN
TaskToProjectRepository taskToProjectRepository = new TaskToProjectRepository();
Project project = taskToProjectRepository.findOneTarget(taskId, "project", REQUEST_PARAMS);
assertThat(project.getId()).isEqualTo(projectId);
/* ------- */
// GIVEN
RelationshipsResourceDelete sut = new RelationshipsResourceDelete(resourceRegistry, typeParser, modificationFilters);
// WHEN -- removing a relation between task and project
Response result = sut.handle(projectRelationPath, emptyProjectQuery, null, newTaskToProjectBody);
assertThat(result).isNotNull();
taskToProjectRepository.removeRelations("project");
// THEN
assertThat(result.getHttpStatus()).isEqualTo(HttpStatus.NO_CONTENT_204);
Project nullProject = taskToProjectRepository.findOneTarget(taskId, "project", REQUEST_PARAMS);
assertThat(nullProject).isNull();
}
use of io.crnk.core.engine.dispatcher.Response 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.dispatcher.Response 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.dispatcher.Response 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();
}
Aggregations