Search in sources :

Example 31 with Response

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();
            }
        });
    }
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) DocumentFilterContext(io.crnk.core.engine.filter.DocumentFilterContext) HashSet(java.util.HashSet) Set(java.util.Set) DocumentFilterChain(io.crnk.core.engine.filter.DocumentFilterChain) DocumentFilter(io.crnk.core.engine.filter.DocumentFilter) ResourceLookup(io.crnk.core.module.discovery.ResourceLookup)

Example 32 with Response

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();
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) Project(io.crnk.core.mock.models.Project) TaskToProjectRepository(io.crnk.core.mock.repository.TaskToProjectRepository) Resource(io.crnk.core.engine.document.Resource) RelationshipsResourcePost(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) RelationshipsResourcePost(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost) RelationshipsResourceDelete(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourceDelete) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 33 with Response

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());
}
Also used : Task(io.crnk.core.mock.models.Task) Resource(io.crnk.core.engine.document.Resource) JsonApiResource(io.crnk.core.resource.annotations.JsonApiResource) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) ProjectPolymorphicRepository(io.crnk.core.mock.repository.ProjectPolymorphicRepository) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) Response(io.crnk.core.engine.dispatcher.Response) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) ProjectPolymorphic(io.crnk.core.mock.models.ProjectPolymorphic) Relationship(io.crnk.core.engine.document.Relationship) ResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch) RelationshipsResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePatch) QuerySpec(io.crnk.core.queryspec.QuerySpec) JsonApiResource(io.crnk.core.resource.annotations.JsonApiResource) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 34 with Response

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);
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) Project(io.crnk.core.mock.models.Project) UserToProjectRepository(io.crnk.core.mock.repository.UserToProjectRepository) RelationshipsResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePatch) Resource(io.crnk.core.engine.document.Resource) JsonApiResource(io.crnk.core.resource.annotations.JsonApiResource) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) QuerySpec(io.crnk.core.queryspec.QuerySpec) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 35 with Response

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();
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter) ResourceDelete(io.crnk.core.engine.internal.dispatcher.controller.ResourceDelete) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Aggregations

Response (io.crnk.core.engine.dispatcher.Response)72 Document (io.crnk.core.engine.document.Document)56 Test (org.junit.Test)54 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)52 Resource (io.crnk.core.engine.document.Resource)43 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)41 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)30 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)18 Project (io.crnk.core.mock.models.Project)15 Relationship (io.crnk.core.engine.document.Relationship)14 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)14 ResourceField (io.crnk.core.engine.information.resource.ResourceField)12 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)12 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)12 Task (io.crnk.core.mock.models.Task)12 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)11 QuerySpec (io.crnk.core.queryspec.QuerySpec)11 TaskToProjectRepository (io.crnk.core.mock.repository.TaskToProjectRepository)9 QueryParams (io.crnk.legacy.queryParams.QueryParams)9 RelationshipsResourcePost (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost)8