Search in sources :

Example 6 with RelationshipsResourcePost

use of io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost in project crnk-framework by crnk-project.

the class RelationshipsResourcePostTest 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));
    data.setType("projects");
    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) Collections.singletonList(data)));
    data.setType("projects");
    data.setId(projectId.toString());
    JsonPath savedTaskPath = pathBuilder.build("/users/" + userId + "/relationships/assignedProjects");
    RelationshipsResourcePost sut = new RelationshipsResourcePost(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) Resource(io.crnk.core.engine.document.Resource) JsonApiResource(io.crnk.core.resource.annotations.JsonApiResource) 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) QuerySpec(io.crnk.core.queryspec.QuerySpec) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) RelationshipsResourcePost(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 7 with RelationshipsResourcePost

use of io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost in project crnk-framework by crnk-project.

the class RelationshipsResourcePostTest method onMissingBodyThrowException.

@Test(expected = RequestBodyNotFoundException.class)
public void onMissingBodyThrowException() throws Exception {
    JsonPath savedTaskPath = pathBuilder.build("/tasks/1/relationships/project");
    RelationshipsResourcePost sut = new RelationshipsResourcePost(resourceRegistry, typeParser, modificationFilters);
    // do not sent along a body
    Document newTaskToProjectBody = null;
    sut.handle(savedTaskPath, emptyProjectQuery, null, newTaskToProjectBody);
}
Also used : RelationshipsResourcePost(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) Document(io.crnk.core.engine.document.Document) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 8 with RelationshipsResourcePost

use of io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost in project crnk-framework by crnk-project.

the class RelationshipsResourcePostTest method onValidRequestShouldAcceptIt.

@Test
public void onValidRequestShouldAcceptIt() {
    // GIVEN
    JsonPath jsonPath = pathBuilder.build("tasks/1/relationships/project");
    ResourceRegistry resourceRegistry = mock(ResourceRegistry.class);
    RelationshipsResourcePost sut = new RelationshipsResourcePost(resourceRegistry, typeParser, modificationFilters);
    // WHEN
    boolean result = sut.isAcceptable(jsonPath, REQUEST_TYPE);
    // THEN
    assertThat(result).isTrue();
}
Also used : ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) RelationshipsResourcePost(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 9 with RelationshipsResourcePost

use of io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost in project crnk-framework by crnk-project.

the class RelationshipsResourcePostTest method onNonRelationRequestShouldDenyIt.

@Test
public void onNonRelationRequestShouldDenyIt() {
    // GIVEN
    JsonPath jsonPath = new ResourcePath("tasks");
    ResourceRegistry resourceRegistry = mock(ResourceRegistry.class);
    RelationshipsResourcePost sut = new RelationshipsResourcePost(resourceRegistry, typeParser, modificationFilters);
    // WHEN
    boolean result = sut.isAcceptable(jsonPath, REQUEST_TYPE);
    // THEN
    assertThat(result).isFalse();
}
Also used : ResourcePath(io.crnk.core.engine.internal.dispatcher.path.ResourcePath) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) RelationshipsResourcePost(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 10 with RelationshipsResourcePost

use of io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost in project crnk-framework by crnk-project.

the class RelationshipsResourceDeleteTest method onExistingToManyRelationshipShouldRemoveIt.

@Test
public void onExistingToManyRelationshipShouldRemoveIt() throws Exception {
    // GIVEN
    Document newUserDocument = new Document();
    newUserDocument.setData(Nullable.of((Object) createUser()));
    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, new QuerySpecAdapter(new QuerySpec(User.class), resourceRegistry), null, newUserDocument);
    // THEN
    assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("users");
    Long userId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    assertThat(userId).isNotNull();
    /* ------- */
    // GIVEN
    Document newProjectDocument = new Document();
    newProjectDocument.setData(Nullable.of((Object) createProject()));
    JsonPath projectPath = pathBuilder.build("/projects");
    // WHEN -- adding a project
    Response projectResponse = resourcePost.handle(projectPath, emptyProjectQuery, null, newProjectDocument);
    // 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 newProjectDocument2 = new Document();
    newProjectDocument2.setData(Nullable.of((Object) createProject(projectId.toString())));
    JsonPath savedTaskPath = pathBuilder.build("/users/" + userId + "/relationships/assignedProjects");
    RelationshipsResourcePost relationshipsResourcePost = new RelationshipsResourcePost(resourceRegistry, typeParser, modificationFilters);
    // WHEN -- adding a relation between user and project
    Response projectRelationshipResponse = relationshipsResourcePost.handle(savedTaskPath, emptyProjectQuery, null, newProjectDocument2);
    assertThat(projectRelationshipResponse).isNotNull();
    // THEN
    UserToProjectRepository userToProjectRepository = new UserToProjectRepository();
    Project project = userToProjectRepository.findOneTarget(userId, "assignedProjects", new QuerySpec(Project.class));
    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(savedTaskPath, emptyProjectQuery, null, newProjectDocument2);
    assertThat(result).isNotNull();
    // THEN
    Project nullProject = userToProjectRepository.findOneTarget(userId, "assignedProjects", new QuerySpec(Project.class));
    assertThat(nullProject).isNull();
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) Project(io.crnk.core.mock.models.Project) UserToProjectRepository(io.crnk.core.mock.repository.UserToProjectRepository) 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) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec) 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)

Aggregations

BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)10 RelationshipsResourcePost (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost)10 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)10 Test (org.junit.Test)10 Document (io.crnk.core.engine.document.Document)8 Response (io.crnk.core.engine.dispatcher.Response)6 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)6 Project (io.crnk.core.mock.models.Project)6 Resource (io.crnk.core.engine.document.Resource)4 TaskToProjectRepository (io.crnk.core.mock.repository.TaskToProjectRepository)3 QuerySpec (io.crnk.core.queryspec.QuerySpec)3 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)2 RelationshipsResourceDelete (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourceDelete)2 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)2 UserToProjectRepository (io.crnk.core.mock.repository.UserToProjectRepository)2 JsonApiResource (io.crnk.core.resource.annotations.JsonApiResource)2 QueryParams (io.crnk.legacy.queryParams.QueryParams)2 ResourceModificationFilter (io.crnk.core.engine.filter.ResourceModificationFilter)1 ResourceGet (io.crnk.core.engine.internal.dispatcher.controller.ResourceGet)1 ResourcePath (io.crnk.core.engine.internal.dispatcher.path.ResourcePath)1