Search in sources :

Example 1 with CrnkException

use of io.crnk.core.exception.CrnkException 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)

Aggregations

Response (io.crnk.core.engine.dispatcher.Response)1 Document (io.crnk.core.engine.document.Document)1 Resource (io.crnk.core.engine.document.Resource)1 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)1 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)1 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)1 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)1 CrnkException (io.crnk.core.exception.CrnkException)1 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)1 Test (org.junit.Test)1