Search in sources :

Example 66 with Response

use of io.crnk.core.engine.dispatcher.Response in project crnk-framework by crnk-project.

the class ResourcePostTest method onUpdatedLazyRelationshipDataShouldReturnThatData.

@Test
public void onUpdatedLazyRelationshipDataShouldReturnThatData() 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 sut = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response taskResponse = sut.handle(taskPath, emptyTaskQuery, null, newTaskBody);
    // THEN
    assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
    assertThat(taskResponse.getDocument().getSingleData().get().getId()).isNotNull();
    assertThat(taskResponse.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("sample task");
    Long taskId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    /* ------- */
    // GIVEN
    Document newProjectBody = new Document();
    data = createProject();
    data.setType("projects");
    List<ResourceIdentifier> taskIds = Collections.singletonList(new ResourceIdentifier(taskId.toString(), "tasks"));
    data.getRelationships().put("tasks", new Relationship(taskIds));
    newProjectBody.setData(Nullable.of((Object) data));
    JsonPath projectsPath = pathBuilder.build("/projects");
    // WHEN
    Response projectsResponse = sut.handle(projectsPath, emptyProjectQuery, null, newProjectBody);
    // THEN
    assertThat(projectsResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects");
    Long userId = Long.parseLong(projectsResponse.getDocument().getSingleData().get().getId());
    assertThat(userId).isNotNull();
    assertThat(projectsResponse.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("sample project");
    assertThat(projectsResponse.getDocument().getSingleData().get().getRelationships().get("tasks").getCollectionData().get()).hasSize(1);
    assertThat(projectsResponse.getDocument().getSingleData().get().getRelationships().get("tasks").getCollectionData().get().get(0).getId()).isEqualTo(taskId.toString());
    Mockito.verify(modificationFilter, Mockito.times(1)).modifyManyRelationship(Mockito.any(), Mockito.any(ResourceField.class), Mockito.eq(ResourceRelationshipModificationType.SET), Mockito.eq(taskIds));
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) ResourceField(io.crnk.core.engine.information.resource.ResourceField) 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) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 67 with Response

use of io.crnk.core.engine.dispatcher.Response in project crnk-framework by crnk-project.

the class ResourcePostTest method onPostingReadOnlyFieldShouldIgnoreWithIgnoreBehavior.

@Test
public void onPostingReadOnlyFieldShouldIgnoreWithIgnoreBehavior() 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.IGNORE.toString();
            }
            return null;
        }
    };
    ResourcePost sut = new ResourcePost(resourceRegistry, propertiesProvider, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response response = sut.handle(path, emptyTaskQuery, null, requestDocument);
    String persistedValue = response.getDocument().getSingleData().get().getAttributes().get("readOnlyValue").asText();
    Assert.assertEquals("someReadOnlyValue", persistedValue);
}
Also used : PropertiesProvider(io.crnk.core.engine.properties.PropertiesProvider) Response(io.crnk.core.engine.dispatcher.Response) 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 68 with Response

use of io.crnk.core.engine.dispatcher.Response in project crnk-framework by crnk-project.

the class ResourcePostTest method onNewInheritedResourceShouldPersistThisResource.

@Test
// TODO
@Ignore
public void onNewInheritedResourceShouldPersistThisResource() throws Exception {
    // GIVEN
    Document newMemorandumBody = new Document();
    Resource data = new Resource();
    newMemorandumBody.setData(Nullable.of((Object) data));
    data.setType("memoranda");
    data.setAttribute("title", objectMapper.readTree("\"sample title\""));
    data.setAttribute("body", objectMapper.readTree("\"sample body\""));
    JsonPath projectPath = pathBuilder.build("/documents");
    ResourcePost sut = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response memorandumResponse = sut.handle(projectPath, emptyMemorandumQuery, null, newMemorandumBody);
    // THEN
    assertThat(memorandumResponse.getDocument().getSingleData().get().getType()).isEqualTo("memoranda");
    Resource persistedMemorandum = memorandumResponse.getDocument().getSingleData().get();
    assertThat(persistedMemorandum.getId()).isNotNull();
    assertThat(persistedMemorandum.getAttributes().get("title").asText()).isEqualTo("sample title");
    assertThat(persistedMemorandum.getAttributes().get("body").asText()).isEqualTo("sample body");
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) 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) Ignore(org.junit.Ignore) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 69 with Response

use of io.crnk.core.engine.dispatcher.Response 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 70 with Response

use of io.crnk.core.engine.dispatcher.Response in project crnk-framework by crnk-project.

the class ResourceFilterTest method checkMutationsOnForbiddenField.

@Test
public void checkMutationsOnForbiddenField() throws IOException {
    ObjectMapper objectMapper = boot.getObjectMapper();
    RegistryEntry entry = resourceRegistry.getEntry(Task.class);
    ResourceInformation resourceInformation = entry.getResourceInformation();
    ResourceField nameField = resourceInformation.findFieldByUnderlyingName("name");
    // prepare test data
    ResourceRepositoryAdapter resourceRepository = entry.getResourceRepository();
    Resource task = new Resource();
    task.setType("tasks");
    task.setId("12");
    task.setAttribute("name", objectMapper.readTree("\"Doe\""));
    String path = "/tasks/";
    String method = HttpMethod.POST.toString();
    Map<String, Set<String>> parameters = Collections.emptyMap();
    Document requestBody = new Document();
    requestBody.setData(Nullable.of((Object) task));
    // try save while forbidden
    Mockito.when(filter.filterField(Mockito.eq(nameField), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.FORBIDDEN);
    Response response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
    Assert.assertEquals(HttpStatus.FORBIDDEN_403, response.getHttpStatus().intValue());
    // try save with ok
    Mockito.when(filter.filterField(Mockito.eq(nameField), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.NONE);
    response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
    Assert.assertEquals(HttpStatus.CREATED_201, response.getHttpStatus().intValue());
    // try update while forbidden
    path = "/tasks/" + task.getId();
    method = HttpMethod.PATCH.toString();
    Mockito.when(filter.filterField(Mockito.eq(nameField), Mockito.eq(HttpMethod.PATCH))).thenReturn(FilterBehavior.FORBIDDEN);
    response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
    Assert.assertEquals(HttpStatus.FORBIDDEN_403, response.getHttpStatus().intValue());
    Mockito.when(filter.filterField(Mockito.eq(nameField), Mockito.eq(HttpMethod.PATCH))).thenReturn(FilterBehavior.NONE);
    response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
    Assert.assertEquals(HttpStatus.OK_200, response.getHttpStatus().intValue());
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Set(java.util.Set) Resource(io.crnk.core.engine.document.Resource) Document(io.crnk.core.engine.document.Document) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Response(io.crnk.core.engine.dispatcher.Response) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpMethod(io.crnk.core.engine.http.HttpMethod) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) 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