Search in sources :

Example 1 with ResourceGet

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

the class ResourceGetTest method onGivenRequestResourceShouldLoadAutoIncludeFields.

@Test
public void onGivenRequestResourceShouldLoadAutoIncludeFields() throws Exception {
    // GIVEN
    JsonPath jsonPath = pathBuilder.build("/task-with-lookup/1");
    ResourceGet responseGetResp = new ResourceGet(resourceRegistry, typeParser, documentMapper);
    Map<String, Set<String>> queryParams = new HashMap<>();
    queryParams.put(RestrictedQueryParamsMembers.include.name() + "[task-with-lookup]", new HashSet<>(Arrays.asList("project", "projectNull", "projectOverridden", "projectOverriddenNull")));
    QueryParams queryParamsObject = new QueryParamsBuilder(new DefaultQueryParamsParser()).buildQueryParams(queryParams);
    // WHEN
    Response response = responseGetResp.handle(jsonPath, new QueryParamsAdapter(resourceRegistry.getEntry(TaskWithLookup.class).getResourceInformation(), queryParamsObject, moduleRegistry), null, null);
    // THEN
    Assert.assertNotNull(response);
    assertThat(response.getDocument().getData().get()).isExactlyInstanceOf(Resource.class);
    assertThat(response.getDocument().getSingleData().get().getType()).isEqualTo("task-with-lookup");
    Resource responseData = response.getDocument().getSingleData().get();
    assertThat(responseData.getRelationships().get("project").getSingleData().get().getId()).isEqualTo("42");
    assertThat(responseData.getRelationships().get("projectNull").getSingleData().get().getId()).isEqualTo("1");
    assertThat(responseData.getRelationships().get("projectOverridden").getSingleData().get().getId()).isEqualTo("1");
    assertThat(responseData.getRelationships().get("projectOverriddenNull").getSingleData().get().getId()).isEqualTo("1");
}
Also used : TaskWithLookup(io.crnk.core.mock.models.TaskWithLookup) Resource(io.crnk.core.engine.document.Resource) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) DefaultQueryParamsParser(io.crnk.legacy.queryParams.DefaultQueryParamsParser) Response(io.crnk.core.engine.dispatcher.Response) ResourceGet(io.crnk.core.engine.internal.dispatcher.controller.ResourceGet) QueryParamsBuilder(io.crnk.legacy.queryParams.QueryParamsBuilder) QueryParams(io.crnk.legacy.queryParams.QueryParams) QueryParamsAdapter(io.crnk.legacy.internal.QueryParamsAdapter) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 2 with ResourceGet

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

the class ResourceGetTest method onGivenRequestCollectionGetShouldDenyIt.

@Test
public void onGivenRequestCollectionGetShouldDenyIt() {
    // GIVEN
    JsonPath jsonPath = pathBuilder.build("/tasks/");
    ResourceGet sut = new ResourceGet(resourceRegistry, typeParser, documentMapper);
    // WHEN
    boolean result = sut.isAcceptable(jsonPath, REQUEST_TYPE);
    // THEN
    Assert.assertEquals(result, false);
}
Also used : ResourceGet(io.crnk.core.engine.internal.dispatcher.controller.ResourceGet) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 3 with ResourceGet

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

the class ResourceGetTest 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, new ArrayList<ResourceModificationFilter>());
    Response taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
    assertThat(taskResponse.getDocument().getData().get()).isExactlyInstanceOf(Resource.class);
    String taskId = ((Resource) taskResponse.getDocument().getData().get()).getId();
    assertThat(taskId).isNotNull();
    // GIVEN
    JsonPath jsonPath = pathBuilder.build("/tasks/" + taskId);
    ResourceGet sut = new ResourceGet(resourceRegistry, typeParser, documentMapper);
    // WHEN
    Response response = sut.handle(jsonPath, emptyTaskQuery, null, null);
    // THEN
    Assert.assertNotNull(response);
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) ResourceGet(io.crnk.core.engine.internal.dispatcher.controller.ResourceGet) Resource(io.crnk.core.engine.document.Resource) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter) 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) RelationshipsResourcePost(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 4 with ResourceGet

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

the class ResourceGetTest method onGivenRequestResourceShouldNotLoadAutoIncludeFields.

@Test
public void onGivenRequestResourceShouldNotLoadAutoIncludeFields() throws Exception {
    // GIVEN
    Document newTaskBody = new Document();
    Resource data = createTask();
    newTaskBody.setData(Nullable.of((Object) data));
    JsonPath taskPath = pathBuilder.build("/tasks");
    ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, new ArrayList<ResourceModificationFilter>());
    // WHEN -- adding a task
    Response taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
    // THEN
    assertThat(taskResponse.getDocument().getSingleData().get()).isExactlyInstanceOf(Resource.class);
    assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
    assertThat(taskResponse.getDocument().getSingleData().get().getId()).isNotNull();
    /* ------- */
    // GIVEN
    Document newProjectBody = new Document();
    newProjectBody.setData(Nullable.of((Object) createProject()));
    JsonPath projectPath = pathBuilder.build("/projects");
    // WHEN -- adding a project
    Response projectResponse = resourcePost.handle(projectPath, emptyProjectQuery, null, newProjectBody);
    // THEN
    assertThat(projectResponse.getDocument().getSingleData().get()).isExactlyInstanceOf(Resource.class);
    assertThat(projectResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects");
    assertThat(projectResponse.getDocument().getSingleData().get().getId()).isNotNull();
    assertThat(projectResponse.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("sample project");
    /* ------- */
    // GIVEN
    Document newTaskToProjectBody = new Document();
    ResourceIdentifier reldata = new ResourceIdentifier();
    newTaskToProjectBody.setData(Nullable.of((Object) data));
    data.setType("projects");
    data.setId("2");
    JsonPath savedTaskPath = pathBuilder.build("/tasks/" + TASK_ID + "/relationships/project");
    RelationshipsResourcePost sut = new RelationshipsResourcePost(resourceRegistry, typeParser, new ArrayList<ResourceModificationFilter>());
    // WHEN -- adding a relation between task and project
    Response projectRelationshipResponse = sut.handle(savedTaskPath, emptyProjectQuery, null, newTaskToProjectBody);
    assertThat(projectRelationshipResponse).isNotNull();
    // THEN
    TaskToProjectRepository taskToProjectRepository = new TaskToProjectRepository();
    Project project = taskToProjectRepository.findOneTarget(TASK_ID, "project", new QueryParams());
    assertThat(project.getId()).isEqualTo(PROJECT_ID);
    // Given
    JsonPath jsonPath = pathBuilder.build("/tasks/" + TASK_ID);
    ResourceGet responseGetResp = new ResourceGet(resourceRegistry, typeParser, documentMapper);
    Map<String, Set<String>> queryParams = new HashMap<>();
    queryParams.put(RestrictedQueryParamsMembers.include.name() + "[tasks]", Collections.singleton("[\"project\"]"));
    QueryParams requestParams = new QueryParamsBuilder(new DefaultQueryParamsParser()).buildQueryParams(queryParams);
    // WHEN
    Response response = responseGetResp.handle(jsonPath, new QueryParamsAdapter(resourceRegistry.getEntry(Task.class).getResourceInformation(), requestParams, moduleRegistry), null, null);
    // THEN
    Assert.assertNotNull(response);
    assertThat(response.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
    assertThat(taskResponse.getDocument().getSingleData().get().getRelationships().get("project").getData().get()).isNull();
}
Also used : Task(io.crnk.core.mock.models.Task) Resource(io.crnk.core.engine.document.Resource) RelationshipsResourcePost(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost) 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) RelationshipsResourcePost(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost) DefaultQueryParamsParser(io.crnk.legacy.queryParams.DefaultQueryParamsParser) Response(io.crnk.core.engine.dispatcher.Response) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Project(io.crnk.core.mock.models.Project) TaskToProjectRepository(io.crnk.core.mock.repository.TaskToProjectRepository) ResourceGet(io.crnk.core.engine.internal.dispatcher.controller.ResourceGet) QueryParamsBuilder(io.crnk.legacy.queryParams.QueryParamsBuilder) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter) QueryParams(io.crnk.legacy.queryParams.QueryParams) QueryParamsAdapter(io.crnk.legacy.internal.QueryParamsAdapter) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 5 with ResourceGet

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

Aggregations

BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)8 ResourceGet (io.crnk.core.engine.internal.dispatcher.controller.ResourceGet)8 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)8 Test (org.junit.Test)8 Response (io.crnk.core.engine.dispatcher.Response)5 Resource (io.crnk.core.engine.document.Resource)4 Document (io.crnk.core.engine.document.Document)3 ResourceModificationFilter (io.crnk.core.engine.filter.ResourceModificationFilter)2 RelationshipsResourcePost (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost)2 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)2 QueryParamsAdapter (io.crnk.legacy.internal.QueryParamsAdapter)2 DefaultQueryParamsParser (io.crnk.legacy.queryParams.DefaultQueryParamsParser)2 QueryParams (io.crnk.legacy.queryParams.QueryParams)2 QueryParamsBuilder (io.crnk.legacy.queryParams.QueryParamsBuilder)2 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)1 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)1 Project (io.crnk.core.mock.models.Project)1 Task (io.crnk.core.mock.models.Task)1 TaskWithLookup (io.crnk.core.mock.models.TaskWithLookup)1 TaskToProjectRepository (io.crnk.core.mock.repository.TaskToProjectRepository)1