Search in sources :

Example 86 with Document

use of io.crnk.core.engine.document.Document in project crnk-framework by crnk-project.

the class ResourcePatchTest method onPatchingReadOnlyFieldReturnBadRequestWithFailBehavior.

@Test
public void onPatchingReadOnlyFieldReturnBadRequestWithFailBehavior() throws Exception {
    // GIVEN
    Document requestDocument = new Document();
    Resource data = createTask();
    requestDocument.setData(Nullable.of((Object) data));
    JsonPath postPath = pathBuilder.build("/tasks");
    ResourcePost post = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    post.handle(postPath, emptyTaskQuery, null, requestDocument);
    PropertiesProvider propertiesProvider = new PropertiesProvider() {

        @Override
        public String getProperty(String key) {
            if (CrnkProperties.RESOURCE_FIELD_IMMUTABLE_WRITE_BEHAVIOR.equals(key)) {
                return ResourceFieldImmutableWriteBehavior.FAIL.toString();
            }
            return null;
        }
    };
    ResourcePatch sut = new ResourcePatch(resourceRegistry, propertiesProvider, typeParser, objectMapper, documentMapper, modificationFilters);
    data.getAttributes().put("readOnlyValue", objectMapper.readTree("\"newValue\""));
    // WHEN
    try {
        JsonPath patchPath = pathBuilder.build("/tasks/" + data.getId());
        sut.handle(patchPath, emptyTaskQuery, null, requestDocument);
        Assert.fail("should not be allowed to update read-only field");
    } catch (ForbiddenException e) {
        Assert.assertEquals("field 'readOnlyValue' cannot be modified", e.getMessage());
    }
}
Also used : PropertiesProvider(io.crnk.core.engine.properties.PropertiesProvider) ForbiddenException(io.crnk.core.exception.ForbiddenException) Resource(io.crnk.core.engine.document.Resource) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) ResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 87 with Document

use of io.crnk.core.engine.document.Document in project crnk-framework by crnk-project.

the class ResourcePatchTest method onUnchagedLazyRelationshipDataShouldNotReturnThatData.

@Test
public void onUnchagedLazyRelationshipDataShouldNotReturnThatData() 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 post = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    Response taskResponse = post.handle(taskPath, emptyTaskQuery, null, newTaskBody);
    Long taskId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    Document newProjectBody = new Document();
    data = createProject();
    data.setType("projects");
    data.getRelationships().put("tasks", new Relationship(Collections.singletonList(new ResourceIdentifier(taskId.toString(), "tasks"))));
    newProjectBody.setData(Nullable.of((Object) data));
    JsonPath projectsPath = pathBuilder.build("/projects");
    Response projectsResponse = post.handle(projectsPath, emptyProjectQuery, null, newProjectBody);
    assertThat(projectsResponse.getDocument().getSingleData().get().getRelationships().get("tasks").getCollectionData().get()).hasSize(1);
    // update relationship and availability in response
    ResourcePatch patch = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    data.getRelationships().remove("tasks");
    data.getAttributes().put("name", objectMapper.readTree("\"updated project\""));
    projectsResponse = patch.handle(pathBuilder.build("/projects/2"), emptyProjectQuery, null, newProjectBody);
    assertThat(projectsResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects");
    assertThat(projectsResponse.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("updated project");
    assertThat(projectsResponse.getDocument().getSingleData().get().getRelationships().get("tasks").getCollectionData().isPresent()).isFalse();
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) ResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 88 with Document

use of io.crnk.core.engine.document.Document in project crnk-framework by crnk-project.

the class FilterTest method test.

@Test
public void test() throws Exception {
    // WHEN
    ArgumentCaptor<DocumentFilterContext> captor = ArgumentCaptor.forClass(DocumentFilterContext.class);
    when(collectionGet.isAcceptable(any(JsonPath.class), eq(requestType))).thenCallRealMethod();
    when(filter.filter(any(DocumentFilterContext.class), any(DocumentFilterChain.class))).thenCallRealMethod();
    Map<String, Set<String>> queryParams = new HashMap<>();
    RepositoryMethodParameterProvider parameterProvider = new NewInstanceRepositoryMethodParameterProvider();
    Document requestBody = new Document();
    dispatcher.dispatchRequest(path, requestType, queryParams, parameterProvider, requestBody);
    // THEN
    verify(filter).filter(captor.capture(), any(DocumentFilterChain.class));
    verify(collectionGet, times(1)).handle(any(JsonPath.class), any(QueryAdapter.class), any(RepositoryMethodParameterProvider.class), any(Document.class));
    verify(filter, times(1)).filter(any(DocumentFilterContext.class), any(DocumentFilterChain.class));
    DocumentFilterContext value = captor.getValue();
    Assert.assertEquals("tasks", value.getJsonPath().getElementName());
    Assert.assertEquals(parameterProvider, value.getParameterProvider());
    Assert.assertEquals(requestBody, value.getRequestBody());
    Assert.assertEquals("GET", value.getMethod());
}
Also used : DocumentFilterContext(io.crnk.core.engine.filter.DocumentFilterContext) NewInstanceRepositoryMethodParameterProvider(io.crnk.core.engine.repository.mock.NewInstanceRepositoryMethodParameterProvider) Set(java.util.Set) HashMap(java.util.HashMap) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) DocumentFilterChain(io.crnk.core.engine.filter.DocumentFilterChain) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) Document(io.crnk.core.engine.document.Document) NewInstanceRepositoryMethodParameterProvider(io.crnk.core.engine.repository.mock.NewInstanceRepositoryMethodParameterProvider) RepositoryMethodParameterProvider(io.crnk.legacy.internal.RepositoryMethodParameterProvider) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Example 89 with Document

use of io.crnk.core.engine.document.Document 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)

Example 90 with Document

use of io.crnk.core.engine.document.Document in project crnk-framework by crnk-project.

the class RelationshipsResourcePatchTest method onDeletingToOneRelationshipShouldSetTheValue.

@Test
public void onDeletingToOneRelationshipShouldSetTheValue() throws Exception {
    // GIVEN
    Document newTaskBody = new Document();
    Resource data = createTask();
    newTaskBody.setData(Nullable.of((Object) data));
    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 newTaskToProjectBody = new Document();
    newTaskToProjectBody.setData(Nullable.nullValue());
    JsonPath savedTaskPath = pathBuilder.build("/tasks/" + taskId + "/relationships/project");
    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
    assertThat(projectRelationshipResponse.getHttpStatus()).isEqualTo(HttpStatus.NO_CONTENT_204);
    Project project = localUserToProjectRepository.findOneTarget(1L, "project", new QuerySpec(Project.class));
    assertThat(project).isNull();
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) Project(io.crnk.core.mock.models.Project) 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)

Aggregations

Document (io.crnk.core.engine.document.Document)131 Test (org.junit.Test)95 Resource (io.crnk.core.engine.document.Resource)87 Response (io.crnk.core.engine.dispatcher.Response)56 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)47 QuerySpec (io.crnk.core.queryspec.QuerySpec)45 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)40 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)40 Relationship (io.crnk.core.engine.document.Relationship)39 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)35 Task (io.crnk.core.mock.models.Task)34 Project (io.crnk.core.mock.models.Project)27 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)25 LazyTask (io.crnk.core.mock.models.LazyTask)17 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)14 RelationIdTestResource (io.crnk.core.mock.models.RelationIdTestResource)12 ResourceField (io.crnk.core.engine.information.resource.ResourceField)11 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)11 RelationshipsResourcePost (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost)10 AbstractDocumentMapperTest (io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest)10