Search in sources :

Example 16 with Response

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

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

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

Example 19 with Response

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

the class ResourceFilterTest method checkFilterGetOnResourceField.

@Test
public void checkFilterGetOnResourceField() {
    // setup test data
    RegistryEntry entry = resourceRegistry.getEntry(Task.class);
    ResourceRepositoryAdapter resourceRepository = entry.getResourceRepository();
    Project project = new Project();
    project.setId(13L);
    project.setName("myProject");
    Task task = new Task();
    task.setId(12L);
    task.setName("myTask");
    task.setProject(project);
    resourceRepository.create(task, new QuerySpecAdapter(new QuerySpec(Task.class), resourceRegistry));
    // get information
    ResourceInformation resourceInformation = entry.getResourceInformation();
    ResourceField projectField = resourceInformation.findFieldByUnderlyingName("project");
    ResourceField nameField = resourceInformation.findFieldByUnderlyingName("name");
    String path = "/tasks/";
    String method = HttpMethod.GET.toString();
    Map<String, Set<String>> parameters = Collections.emptyMap();
    Document requestBody = null;
    // forbid field
    Mockito.when(filter.filterField(Mockito.eq(projectField), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.FORBIDDEN);
    Response response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
    Assert.assertEquals(HttpStatus.OK_200, response.getHttpStatus().intValue());
    Resource taskResource = response.getDocument().getCollectionData().get().get(0);
    Assert.assertTrue(taskResource.getRelationships().containsKey("projects"));
    Assert.assertFalse(taskResource.getRelationships().containsKey("project"));
    Assert.assertTrue(taskResource.getAttributes().containsKey("name"));
    // allow resource
    Mockito.when(filter.filterField(Mockito.eq(nameField), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.FORBIDDEN);
    response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
    Assert.assertEquals(HttpStatus.OK_200, response.getHttpStatus().intValue());
    taskResource = response.getDocument().getCollectionData().get().get(0);
    Assert.assertTrue(taskResource.getRelationships().containsKey("projects"));
    Assert.assertFalse(taskResource.getRelationships().containsKey("project"));
    Assert.assertFalse(taskResource.getAttributes().containsKey("name"));
}
Also used : Task(io.crnk.core.mock.models.Task) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Set(java.util.Set) Resource(io.crnk.core.engine.document.Resource) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) Document(io.crnk.core.engine.document.Document) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Response(io.crnk.core.engine.dispatcher.Response) Project(io.crnk.core.mock.models.Project) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec) HttpMethod(io.crnk.core.engine.http.HttpMethod) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Example 20 with Response

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

the class FieldResourceGetTest method onGivenRequestFieldResourceGetShouldHandleIt.

@Test
public void onGivenRequestFieldResourceGetShouldHandleIt() throws Exception {
    // GIVEN
    JsonPath jsonPath = pathBuilder.build("/tasks/1/project");
    FieldResourceGet sut = new FieldResourceGet(resourceRegistry, typeParser, documentMapper);
    // WHEN
    Response response = sut.handle(jsonPath, emptyProjectQuery, null, null);
    // THEN
    Assert.assertNotNull(response);
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) FieldResourceGet(io.crnk.core.engine.internal.dispatcher.controller.FieldResourceGet) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) 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