Search in sources :

Example 46 with JsonPath

use of io.crnk.core.engine.internal.dispatcher.path.JsonPath in project crnk-framework by crnk-project.

the class ResourcePostTest method onResourceWithCustomNamesShouldSaveParametersCorrectly.

@Test
// TODO support inhertiance
@Ignore
public void onResourceWithCustomNamesShouldSaveParametersCorrectly() throws Exception {
    // GIVEN - creating sample project id
    Document newProjectBody = new Document();
    Resource data = createProject();
    newProjectBody.setData(Nullable.of((Object) data));
    JsonPath projectPath = pathBuilder.build("/projects");
    ResourcePost sut = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response projectResponse = sut.handle(projectPath, emptyProjectQuery, null, newProjectBody);
    // THEN
    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");
    Long projectId = Long.parseLong(projectResponse.getDocument().getSingleData().get().getId());
    /* ------- */
    // GIVEN
    Document pojoBody = new Document();
    Resource pojoData = new Resource();
    pojoBody.setData(Nullable.of((Object) pojoData));
    pojoData.setType("pojo");
    JsonNode put = objectMapper.createObjectNode().put("value", "hello");
    pojoData.setAttribute("other-pojo", put);
    pojoData.getRelationships().put("some-project", new Relationship(new ResourceIdentifier(Long.toString(projectId), "projects")));
    pojoData.getRelationships().put("some-projects", new Relationship(Arrays.asList(new ResourceIdentifier(Long.toString(projectId), "projects"))));
    JsonPath pojoPath = pathBuilder.build("/pojo");
    // WHEN
    QueryParamsBuilder queryParamsBuilder = new QueryParamsBuilder(new DefaultQueryParamsParser());
    QueryParams queryParams = queryParamsBuilder.buildQueryParams(Collections.singletonMap("include[pojo]", Collections.singleton("projects")));
    Response pojoResponse = sut.handle(pojoPath, toQueryAdapter(queryParams, Pojo.class), null, pojoBody);
    // THEN
    assertThat(pojoResponse.getDocument().getSingleData().get().getType()).isEqualTo("pojo");
    Resource persistedPojo = pojoResponse.getDocument().getSingleData().get();
    assertThat(persistedPojo.getId()).isNotNull();
    assertThat(persistedPojo.getAttributes().get("other-pojo").get("value").asText()).isEqualTo("hello");
    assertThat(persistedPojo.getRelationships().get("some-project").getSingleData().get()).isNotNull();
    assertThat(persistedPojo.getRelationships().get("some-project").getSingleData().get().getId()).isEqualTo(projectId.toString());
    Relationship persistedProjectsRelationship = persistedPojo.getRelationships().get("some-projects");
    assertThat(persistedProjectsRelationship).isNotNull();
    // check lazy loaded relation
    PojoRepository repo = (PojoRepository) resourceRegistry.getEntry(Pojo.class).getResourceRepository(null).getResourceRepository();
    Pojo pojo = repo.findOne(null, null);
    assertThat(pojo.getProjects()).hasSize(1);
    assertThat(pojo.getProjects().get(0).getId()).isEqualTo(projectId);
}
Also used : PojoRepository(io.crnk.core.mock.repository.PojoRepository) Pojo(io.crnk.core.mock.models.Pojo) Resource(io.crnk.core.engine.document.Resource) JsonNode(com.fasterxml.jackson.databind.JsonNode) 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) DefaultQueryParamsParser(io.crnk.legacy.queryParams.DefaultQueryParamsParser) Response(io.crnk.core.engine.dispatcher.Response) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Relationship(io.crnk.core.engine.document.Relationship) QueryParamsBuilder(io.crnk.legacy.queryParams.QueryParamsBuilder) QueryParams(io.crnk.legacy.queryParams.QueryParams) Ignore(org.junit.Ignore) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 47 with JsonPath

use of io.crnk.core.engine.internal.dispatcher.path.JsonPath in project crnk-framework by crnk-project.

the class ResourcePostTest method onGivenRequestResourceGetShouldAcceptIt.

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

Example 48 with JsonPath

use of io.crnk.core.engine.internal.dispatcher.path.JsonPath in project crnk-framework by crnk-project.

the class ResourcePostTest method onResourceWithInvalidRelationshipNameShouldThrowException.

@Test
public void onResourceWithInvalidRelationshipNameShouldThrowException() throws Exception {
    // GIVEN - creating sample project id
    Document newProjectBody = new Document();
    Resource data = createProject();
    newProjectBody.setData(Nullable.of((Object) data));
    JsonPath projectPath = pathBuilder.build("/projects");
    ResourcePost sut = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response projectResponse = sut.handle(projectPath, emptyTaskQuery, null, newProjectBody);
    // THEN
    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");
    Long projectId = Long.parseLong(projectResponse.getDocument().getSingleData().get().getId());
    /* ------- */
    // GIVEN
    Document pojoBody = new Document();
    Resource pojoData = new Resource();
    pojoBody.setData(Nullable.of((Object) pojoData));
    pojoData.setType("pojo");
    JsonNode put = objectMapper.createObjectNode().put("value", "hello");
    pojoData.setAttribute("other-pojo", objectMapper.readTree("null"));
    String invalidRelationshipName = "invalid-relationship";
    pojoData.getRelationships().put(invalidRelationshipName, new Relationship(new ResourceIdentifier(Long.toString(projectId), "projects")));
    JsonPath pojoPath = pathBuilder.build("/pojo");
    // THEN
    expectedException.expect(ResourceException.class);
    expectedException.expectMessage(String.format("Invalid relationship name: %s", invalidRelationshipName));
    // WHEN
    sut.handle(pojoPath, new QuerySpecAdapter(new QuerySpec(Pojo.class), resourceRegistry), null, pojoBody);
}
Also used : Resource(io.crnk.core.engine.document.Resource) JsonNode(com.fasterxml.jackson.databind.JsonNode) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) Response(io.crnk.core.engine.dispatcher.Response) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Relationship(io.crnk.core.engine.document.Relationship) QuerySpec(io.crnk.core.queryspec.QuerySpec) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 49 with JsonPath

use of io.crnk.core.engine.internal.dispatcher.path.JsonPath in project crnk-framework by crnk-project.

the class JsonApiRequestProcessor method process.

@Override
public void process(HttpRequestContext requestContext) throws IOException {
    if (isJsonApiRequest(requestContext, isAcceptingPlainJson())) {
        ResourceRegistry resourceRegistry = moduleContext.getResourceRegistry();
        RequestDispatcher requestDispatcher = moduleContext.getRequestDispatcher();
        String path = requestContext.getPath();
        JsonPath jsonPath = new PathBuilder(resourceRegistry).build(path);
        Map<String, Set<String>> parameters = requestContext.getRequestParameters();
        String method = requestContext.getMethod();
        if (jsonPath instanceof ActionPath) {
            // inital implementation, has to improve
            requestDispatcher.dispatchAction(path, method, parameters);
        } else if (jsonPath != null) {
            byte[] requestBody = requestContext.getRequestBody();
            Document document = null;
            if (requestBody != null && requestBody.length > 0) {
                ObjectMapper objectMapper = moduleContext.getObjectMapper();
                try {
                    document = objectMapper.readerFor(Document.class).readValue(requestBody);
                } catch (JsonProcessingException e) {
                    final String message = "Json Parsing failed";
                    setResponse(requestContext, buildBadRequestResponse(message, e.getMessage()));
                    LOGGER.error(message, e);
                    return;
                }
            }
            RepositoryMethodParameterProvider parameterProvider = requestContext.getRequestParameterProvider();
            Response crnkResponse = requestDispatcher.dispatchRequest(path, method, parameters, parameterProvider, document);
            setResponse(requestContext, crnkResponse);
        } else {
        // no repositories invoked, we do nothing
        }
    }
}
Also used : Set(java.util.Set) ActionPath(io.crnk.core.engine.internal.dispatcher.path.ActionPath) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) Document(io.crnk.core.engine.document.Document) RequestDispatcher(io.crnk.core.engine.dispatcher.RequestDispatcher) Response(io.crnk.core.engine.dispatcher.Response) PathBuilder(io.crnk.core.engine.internal.dispatcher.path.PathBuilder) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) RepositoryMethodParameterProvider(io.crnk.legacy.internal.RepositoryMethodParameterProvider) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 50 with JsonPath

use of io.crnk.core.engine.internal.dispatcher.path.JsonPath in project crnk-framework by crnk-project.

the class CollectionGetTest method onGivenRequestResourceShouldLoadAutoIncludeFields.

@Test
public void onGivenRequestResourceShouldLoadAutoIncludeFields() 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().getType()).isEqualTo("tasks");
    Long taskId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    assertThat(taskId).isNotNull();
    /* ------- */
    // GIVEN
    Document newProjectBody = new Document();
    data = createProject();
    newProjectBody.setData(Nullable.of((Object) data));
    JsonPath projectPath = pathBuilder.build("/projects");
    // WHEN -- adding a project
    Response projectResponse = resourcePost.handle(projectPath, emptyProjectQuery, null, newProjectBody);
    // THEN
    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");
    Long projectId = Long.parseLong(projectResponse.getDocument().getSingleData().get().getId());
    assertThat(projectId).isNotNull();
    /* ------- */
    // GIVEN
    Document newTaskToProjectBody = new Document();
    data = new Resource();
    newTaskToProjectBody.setData(Nullable.of((Object) Collections.singletonList(data)));
    data.setType("projects");
    data.setId(projectId.toString());
    JsonPath savedTaskPath = pathBuilder.build("/tasks/" + taskId + "/relationships/includedProjects");
    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(taskId, "includedProjects", new QueryParams());
    assertThat(project.getId()).isEqualTo(projectId);
    // Given
    JsonPath jsonPath = pathBuilder.build("/tasks/" + taskId);
    ResourceGet responseGetResp = new ResourceGet(resourceRegistry, typeParser, documentMapper);
    Map<String, Set<String>> queryParams = new HashMap<>();
    queryParams.put(RestrictedQueryParamsMembers.include.name() + "[tasks]", Collections.singleton("includedProjects"));
    QueryParams queryParams1 = new QueryParamsBuilder(new DefaultQueryParamsParser()).buildQueryParams(queryParams);
    // WHEN
    Response response = responseGetResp.handle(jsonPath, new QueryParamsAdapter(resourceRegistry.getEntry(Task.class).getResourceInformation(), queryParams1, moduleRegistry), null, null);
    // THEN
    Assert.assertNotNull(response);
    data = response.getDocument().getSingleData().get();
    assertThat(data.getType()).isEqualTo("tasks");
    Relationship relationship = data.getRelationships().get("includedProjects");
    assertThat(relationship.getCollectionData()).isNotNull();
    assertThat(relationship.getCollectionData().get().size()).isEqualTo(1);
    assertThat(relationship.getCollectionData().get().get(0).getId()).isEqualTo(projectId.toString());
}
Also used : Task(io.crnk.core.mock.models.Task) Resource(io.crnk.core.engine.document.Resource) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) DefaultQueryParamsParser(io.crnk.legacy.queryParams.DefaultQueryParamsParser) Response(io.crnk.core.engine.dispatcher.Response) Project(io.crnk.core.mock.models.Project) TaskToProjectRepository(io.crnk.core.mock.repository.TaskToProjectRepository) Relationship(io.crnk.core.engine.document.Relationship) 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) Test(org.junit.Test)

Aggregations

JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)88 Test (org.junit.Test)80 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)72 Response (io.crnk.core.engine.dispatcher.Response)50 Document (io.crnk.core.engine.document.Document)46 Resource (io.crnk.core.engine.document.Resource)41 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)36 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)20 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)16 Project (io.crnk.core.mock.models.Project)14 Relationship (io.crnk.core.engine.document.Relationship)13 RelationshipsResourcePost (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost)12 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)11 ResourcePath (io.crnk.core.engine.internal.dispatcher.path.ResourcePath)10 QuerySpec (io.crnk.core.queryspec.QuerySpec)10 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)10 Task (io.crnk.core.mock.models.Task)9 TaskToProjectRepository (io.crnk.core.mock.repository.TaskToProjectRepository)9 QueryParams (io.crnk.legacy.queryParams.QueryParams)9 ResourceModificationFilter (io.crnk.core.engine.filter.ResourceModificationFilter)8