Search in sources :

Example 1 with ResourcePatch

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

the class ResourcePatchTest method onGivenRequestResourceShouldThrowException.

@Test
public void onGivenRequestResourceShouldThrowException() throws Exception {
    // GIVEN
    Document newTaskBody = new Document();
    Resource data = createTask();
    newTaskBody.setData(Nullable.of((Object) data));
    data.setType("tasks");
    JsonPath taskPath = pathBuilder.build("/tasks");
    // WHEN
    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 taskId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    assertThat(taskId).isNotNull();
    // GIVEN
    Document taskPatch = new Document();
    data = new Resource();
    taskPatch.setData(Nullable.of((Object) data));
    data.setType("WRONG_AND_MISSING_TYPE");
    data.setAttribute("name", objectMapper.readTree("\"task updated\""));
    JsonPath jsonPath = pathBuilder.build("/tasks/" + taskId);
    ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response response = null;
    try {
        response = sut.handle(jsonPath, emptyTaskQuery, null, taskPatch);
        Assert.fail("Should have recieved exception.");
    } catch (CrnkException rbe) {
    // Got correct exception
    } catch (Error ex) {
        Assert.fail("Got bad exception: " + ex);
    }
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) 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) ResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch) CrnkException(io.crnk.core.exception.CrnkException) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 2 with ResourcePatch

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

the class ResourcePatchTest method onGivenRequestResourcePatchShouldHandleMissingFields.

@Test
public void onGivenRequestResourcePatchShouldHandleMissingFields() throws Exception {
    JsonPath complexPojoPath = pathBuilder.build("/complexpojos/1");
    // WHEN
    ResourceGet resourceGet = new ResourceGet(resourceRegistry, typeParser, documentMapper);
    Response complexPojoResponse = resourceGet.handle(complexPojoPath, emptyComplexPojoQuery, null, null);
    assertThat(complexPojoResponse.getDocument().getSingleData().get().getType()).isEqualTo("complexpojos");
    Long complexPojoId = Long.parseLong(complexPojoResponse.getDocument().getSingleData().get().getId());
    assertThat(complexPojoId).isNotNull();
    assertThat(complexPojoResponse.getDocument().getSingleData().get().getAttributes().get("containedPojo").get("updateableProperty1").asText()).isEqualTo("value from repository mock");
    // GIVEN
    Document complexPojoPatch = new Document();
    Resource data = new Resource();
    complexPojoPatch.setData(Nullable.of((Object) data));
    data.setType("complexpojos");
    String rawContainedPatchData = "  {" + "    'updateableProperty1':'updated value'" + "  }";
    rawContainedPatchData = rawContainedPatchData.replaceAll("'", "\"");
    data.setAttribute("containedPojo", objectMapper.readTree(rawContainedPatchData));
    data.setAttribute("updateableProperty", objectMapper.readTree("\"wasNullBefore\""));
    JsonPath jsonPath = pathBuilder.build("/complexpojos/" + complexPojoId);
    ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response response = sut.handle(jsonPath, emptyComplexPojoQuery, null, complexPojoPatch);
    // THEN
    Assert.assertNotNull(response);
    assertThat(response.getDocument().getSingleData().get().getType()).isEqualTo("complexpojos");
    assertThat(response.getDocument().getSingleData().get().getAttributes().get("containedPojo").get("updateableProperty1").asText()).isEqualTo("updated value");
    assertThat(response.getDocument().getSingleData().get().getAttributes().get("containedPojo").get("updateableProperty2").asText()).isEqualTo("value from repository mock");
    assertThat(response.getDocument().getSingleData().get().getAttributes().get("updateableProperty").asText()).isEqualTo("wasNullBefore");
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) ResourceGet(io.crnk.core.engine.internal.dispatcher.controller.ResourceGet) Resource(io.crnk.core.engine.document.Resource) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) Document(io.crnk.core.engine.document.Document) ResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 3 with ResourcePatch

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

the class ResourcePatchTest method onGivenRequestResourceGetShouldHandleIt.

@Test
public void onGivenRequestResourceGetShouldHandleIt() throws Exception {
    // GIVEN
    Document newTaskBody = new Document();
    Resource data = createTask();
    newTaskBody.setData(Nullable.of((Object) data));
    JsonPath taskPath = pathBuilder.build("/tasks");
    // WHEN
    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 taskId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    assertThat(taskId).isNotNull();
    // GIVEN
    Document taskPatch = new Document();
    data = createTask();
    taskPatch.setData(Nullable.of((Object) data));
    data.setAttribute("name", objectMapper.readTree("\"task updated\""));
    JsonPath jsonPath = pathBuilder.build("/tasks/" + taskId);
    ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response response = sut.handle(jsonPath, emptyTaskQuery, null, taskPatch);
    // THEN
    Assert.assertNotNull(response);
    assertThat(response.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
    assertThat(response.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("task updated");
    Mockito.verify(modificationFilter, Mockito.times(1)).modifyAttribute(Mockito.any(), Mockito.any(ResourceField.class), Mockito.eq("name"), Mockito.eq("task updated"));
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) ResourceField(io.crnk.core.engine.information.resource.ResourceField) 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 4 with ResourcePatch

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

the class ResourcePatchTest method omittedFieldsShouldBeIgnored.

@Test
public void omittedFieldsShouldBeIgnored() throws Exception {
    // GIVEN
    ResourceRepositoryAdapter taskRepo = resourceRegistry.getEntry(Task.class).getResourceRepository(null);
    Task task = new Task();
    task.setName("Mary Joe");
    JsonApiResponse jsonApiResponse = taskRepo.create(task, emptyTaskQuery);
    task = (Task) (jsonApiResponse.getEntity());
    // GIVEN
    Resource data = new Resource();
    data.setType("tasks");
    data.setAttribute("category", objectMapper.readTree("\"TestCategory\""));
    Document taskPatch = new Document();
    taskPatch.setData(Nullable.of((Object) data));
    JsonPath jsonPath = pathBuilder.build("/tasks/" + task.getId());
    ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response response = sut.handle(jsonPath, emptyTaskQuery, null, taskPatch);
    // THEN
    Assert.assertNotNull(response);
    assertThat(response.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
    Resource updatedTask = response.getDocument().getSingleData().get();
    assertThat(updatedTask.getAttributes().get("name").asText()).isEqualTo("Mary Joe");
    assertThat(updatedTask.getAttributes().get("category").asText()).isEqualTo("TestCategory");
    assertThat(updatedTask.getId()).isEqualTo(task.getId().toString());
    Assert.assertEquals("Mary Joe", task.getName());
    Assert.assertEquals("TestCategory", task.getCategory());
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) Task(io.crnk.core.mock.models.Task) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) Resource(io.crnk.core.engine.document.Resource) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) 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) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 5 with ResourcePatch

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

the class ResourcePatchTest method omittedFieldsSettersAreNotCalled.

/*
	 * see github #122
	 */
@Test
public void omittedFieldsSettersAreNotCalled() throws Exception {
    // GIVEN
    ResourceRepositoryAdapter taskRepo = resourceRegistry.getEntry(Task.class).getResourceRepository(null);
    Task task = new Task();
    task.setName("Mary Joe");
    JsonApiResponse jsonApiResponse = taskRepo.create(task, emptyTaskQuery);
    task = (Task) (jsonApiResponse.getEntity());
    // GIVEN
    Document taskPatch = new Document();
    Resource data = new Resource();
    taskPatch.setData(Nullable.of((Object) data));
    data.setType("tasks");
    data.setAttribute("name", objectMapper.readTree("\"Mary Jane\""));
    JsonPath jsonPath = pathBuilder.build("/tasks/" + task.getId());
    ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response response = sut.handle(jsonPath, emptyTaskQuery, null, taskPatch);
    // THEN
    Assert.assertNotNull(response);
    assertThat(response.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
    Resource updatedTask = response.getDocument().getSingleData().get();
    assertThat(updatedTask.getAttributes().get("name").asText()).isEqualTo("Mary Jane");
    assertThat(updatedTask.getId()).isEqualTo(task.getId().toString());
    assertThat(updatedTask.getAttributes().get("category")).isNull();
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) Task(io.crnk.core.mock.models.Task) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) Resource(io.crnk.core.engine.document.Resource) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) 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) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Aggregations

ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)16 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)16 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)15 Test (org.junit.Test)15 Document (io.crnk.core.engine.document.Document)14 Resource (io.crnk.core.engine.document.Resource)14 Response (io.crnk.core.engine.dispatcher.Response)12 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)10 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)9 Relationship (io.crnk.core.engine.document.Relationship)4 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)3 Task (io.crnk.core.mock.models.Task)3 RelationshipsResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePatch)2 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)2 QuerySpec (io.crnk.core.queryspec.QuerySpec)2 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 ResourceGet (io.crnk.core.engine.internal.dispatcher.controller.ResourceGet)1 NullPropertiesProvider (io.crnk.core.engine.properties.NullPropertiesProvider)1 PropertiesProvider (io.crnk.core.engine.properties.PropertiesProvider)1 CrnkException (io.crnk.core.exception.CrnkException)1