Search in sources :

Example 86 with JsonPath

use of io.crnk.core.engine.internal.dispatcher.path.JsonPath in project crnk-framework by crnk-project.

the class ResourcePostTest method onNewResourcesAndRelationshipShouldPersistThoseData.

@Test
public void onNewResourcesAndRelationshipShouldPersistThoseData() throws Exception {
    // GIVEN
    Document newProjectBody = new Document();
    Resource data = createProject();
    newProjectBody.setData(Nullable.of((Object) data));
    JsonPath projectPath = pathBuilder.build("/projects");
    ResourcePost sut = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response projectResponse = sut.handle(projectPath, emptyProjectQuery, null, newProjectBody);
    // THEN
    assertThat(projectResponse.getHttpStatus()).isEqualTo(HttpStatus.CREATED_201);
    assertThat(projectResponse.getDocument().getData().get()).isExactlyInstanceOf(Resource.class);
    assertThat(projectResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects");
    Resource persistedProject = projectResponse.getDocument().getSingleData().get();
    assertThat(persistedProject.getId()).isNotNull();
    assertThat(persistedProject.getAttributes().get("name").asText()).isEqualTo("sample project");
    assertThat(persistedProject.getAttributes().get("data").get("data").asText()).isEqualTo("asd");
    Long projectId = Long.parseLong(projectResponse.getDocument().getSingleData().get().getId());
    Mockito.verify(modificationFilter, Mockito.times(1)).modifyAttribute(Mockito.any(), Mockito.any(ResourceField.class), Mockito.eq("data"), Mockito.any());
    /* ------- */
    // GIVEN
    Document newTasksBody = new Document();
    newTasksBody.setData(Nullable.of((Object) createTask()));
    newTasksBody.getSingleData().get().getRelationships().put("project", new Relationship(new ResourceIdentifier(projectId.toString(), "projects")));
    JsonPath taskPath = pathBuilder.build("/tasks");
    // WHEN
    Response taskResponse = sut.handle(taskPath, emptyTaskQuery, null, newTasksBody);
    // THEN
    assertThat(taskResponse.getHttpStatus()).isEqualTo(HttpStatus.CREATED_201);
    assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
    String taskId = taskResponse.getDocument().getSingleData().get().getId();
    assertThat(taskId).isNotNull();
    assertThat(taskResponse.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("sample task");
    TaskRepository taskRepository = new TaskRepository();
    Task persistedTask = taskRepository.findOne(Long.parseLong(taskId), null);
    assertThat(persistedTask.getProject().getId()).isEqualTo(projectId);
}
Also used : Task(io.crnk.core.mock.models.Task) TaskRepository(io.crnk.core.mock.repository.TaskRepository) Resource(io.crnk.core.engine.document.Resource) 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) Response(io.crnk.core.engine.dispatcher.Response) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Relationship(io.crnk.core.engine.document.Relationship) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 87 with JsonPath

use of io.crnk.core.engine.internal.dispatcher.path.JsonPath in project crnk-framework by crnk-project.

the class ResourcePostTest method onPostingReadOnlyFieldReturnBadRequestWithFailBehavior.

@Test
public void onPostingReadOnlyFieldReturnBadRequestWithFailBehavior() throws Exception {
    // GIVEN
    Document requestDocument = new Document();
    Resource data = createTask();
    data.getAttributes().put("readOnlyValue", objectMapper.readTree("\"newValue\""));
    requestDocument.setData(Nullable.of((Object) data));
    JsonPath path = pathBuilder.build("/tasks");
    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;
        }
    };
    ResourcePost sut = new ResourcePost(resourceRegistry, propertiesProvider, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    try {
        sut.handle(path, 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) Resource(io.crnk.core.engine.document.Resource) 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) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 88 with JsonPath

use of io.crnk.core.engine.internal.dispatcher.path.JsonPath in project crnk-framework by crnk-project.

the class HomeModule method isHomeRequest.

private boolean isHomeRequest(HttpRequestContext requestContext) {
    String path = requestContext.getPath();
    if (!path.endsWith("/") || !requestContext.getMethod().equalsIgnoreCase(HttpMethod.GET.toString())) {
        return false;
    }
    boolean acceptsHome = requestContext.accepts(JSON_HOME_CONTENT_TYPE);
    boolean acceptsJsonApi = requestContext.accepts(HttpHeaders.JSONAPI_CONTENT_TYPE);
    boolean acceptsJson = requestContext.accepts(HttpHeaders.JSON_CONTENT_TYPE);
    boolean acceptsAny = requestContext.acceptsAny();
    if (!(acceptsHome || acceptsAny || acceptsJson || acceptsJsonApi)) {
        return false;
    }
    ResourceRegistry resourceRegistry = moduleContext.getResourceRegistry();
    JsonPath jsonPath = new PathBuilder(resourceRegistry).build(path);
    // no repository with that path
    return jsonPath == null;
}
Also used : PathBuilder(io.crnk.core.engine.internal.dispatcher.path.PathBuilder) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath)

Aggregations

JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)88 Test (org.junit.Test)80 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)72 Response (io.crnk.core.engine.dispatcher.Response)50 Document (io.crnk.core.engine.document.Document)46 Resource (io.crnk.core.engine.document.Resource)41 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)36 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)20 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)16 Project (io.crnk.core.mock.models.Project)14 Relationship (io.crnk.core.engine.document.Relationship)13 RelationshipsResourcePost (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost)12 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)11 ResourcePath (io.crnk.core.engine.internal.dispatcher.path.ResourcePath)10 QuerySpec (io.crnk.core.queryspec.QuerySpec)10 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)10 Task (io.crnk.core.mock.models.Task)9 TaskToProjectRepository (io.crnk.core.mock.repository.TaskToProjectRepository)9 QueryParams (io.crnk.legacy.queryParams.QueryParams)9 ResourceModificationFilter (io.crnk.core.engine.filter.ResourceModificationFilter)8