use of io.crnk.core.engine.dispatcher.Response 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");
}
use of io.crnk.core.engine.dispatcher.Response 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);
}
use of io.crnk.core.engine.dispatcher.Response 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();
}
use of io.crnk.core.engine.dispatcher.Response 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);
}
}
use of io.crnk.core.engine.dispatcher.Response 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");
}
Aggregations