Search in sources :

Example 26 with JsonPath

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

the class FieldResourceGetTest method onGivenIncludeRequestFieldResourcesGetShouldHandleIt.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void onGivenIncludeRequestFieldResourcesGetShouldHandleIt() throws Exception {
    // get repositories
    ResourceRepositoryAdapter userRepo = resourceRegistry.getEntry(User.class).getResourceRepository(null);
    ResourceRepositoryAdapter projectRepo = resourceRegistry.getEntry(Project.class).getResourceRepository(null);
    ResourceRepositoryAdapter taskRepo = resourceRegistry.getEntry(Task.class).getResourceRepository(null);
    RelationshipRepositoryAdapter relRepositoryUserToProject = resourceRegistry.getEntry(User.class).getRelationshipRepository("assignedProjects", null);
    RelationshipRepositoryAdapter relRepositoryProjectToTask = resourceRegistry.getEntry(Project.class).getRelationshipRepository("tasks", null);
    ResourceInformation userInfo = resourceRegistry.getEntry(User.class).getResourceInformation();
    ResourceInformation projectInfo = resourceRegistry.getEntry(Project.class).getResourceInformation();
    ResourceField includedTaskField = projectInfo.findRelationshipFieldByName("includedTask");
    ResourceField assignedProjectsField = userInfo.findRelationshipFieldByName("assignedProjects");
    // setup test data
    User user = new User();
    user.setId(1L);
    userRepo.create(user, emptyUserQuery);
    Project project = new Project();
    project.setId(2L);
    projectRepo.create(project, emptyProjectQuery);
    Task task = new Task();
    task.setId(3L);
    taskRepo.create(task, emptyTaskQuery);
    relRepositoryUserToProject.setRelations(user, Collections.singletonList(project.getId()), assignedProjectsField, emptyProjectQuery);
    relRepositoryProjectToTask.setRelation(project, task.getId(), includedTaskField, emptyTaskQuery);
    Map<String, Set<String>> params = new HashMap<String, Set<String>>();
    addParams(params, "include[projects]", "includedTask");
    QueryParams queryParams = queryParamsBuilder.buildQueryParams(params);
    QueryAdapter queryAdapter = new QueryParamsAdapter(projectInfo, queryParams, moduleRegistry);
    JsonPath jsonPath = pathBuilder.build("/users/1/assignedProjects");
    FieldResourceGet sut = new FieldResourceGet(resourceRegistry, typeParser, documentMapper);
    Response response = sut.handle(jsonPath, queryAdapter, null, null);
    // THEN
    Assert.assertNotNull(response);
    Assert.assertNotNull(response.getDocument().getData());
    List<Resource> entityList = ((List<Resource>) response.getDocument().getData().get());
    Assert.assertEquals(true, entityList.size() > 0);
    Assert.assertEquals("projects", entityList.get(0).getType());
    Resource returnedProject = entityList.get(0);
    Assert.assertEquals(project.getId().toString(), returnedProject.getId());
    Relationship relationship = returnedProject.getRelationships().get("includedTask");
    Assert.assertNotNull(relationship);
    Assert.assertEquals(task.getId().toString(), relationship.getSingleData().get().getId());
}
Also used : Task(io.crnk.core.mock.models.Task) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) User(io.crnk.core.mock.models.User) FieldResourceGet(io.crnk.core.engine.internal.dispatcher.controller.FieldResourceGet) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) Resource(io.crnk.core.engine.document.Resource) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter) 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) Relationship(io.crnk.core.engine.document.Relationship) 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 27 with JsonPath

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

the class FieldResourceGetTest method onGivenRequestFieldResourcesGetShouldHandleIt.

@Test
public void onGivenRequestFieldResourcesGetShouldHandleIt() throws Exception {
    // GIVEN
    JsonPath jsonPath = pathBuilder.build("/users/1/assignedProjects");
    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)

Example 28 with JsonPath

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

the class FieldResourceGetTest method onValidRequestShouldAcceptIt.

@Test
public void onValidRequestShouldAcceptIt() {
    // GIVEN
    JsonPath jsonPath = pathBuilder.build("tasks/1/project");
    ResourceRegistry resourceRegistry = mock(ResourceRegistry.class);
    FieldResourceGet sut = new FieldResourceGet(resourceRegistry, typeParser, documentMapper);
    // WHEN
    boolean result = sut.isAcceptable(jsonPath, REQUEST_TYPE);
    // THEN
    assertThat(result).isTrue();
}
Also used : FieldResourceGet(io.crnk.core.engine.internal.dispatcher.controller.FieldResourceGet) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 29 with JsonPath

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

the class ControllerRegistryTest method onUnsupportedRequestRegisterShouldThrowError.

@Test
public void onUnsupportedRequestRegisterShouldThrowError() {
    // GIVEN
    PathBuilder pathBuilder = new PathBuilder(resourceRegistry);
    JsonPath jsonPath = pathBuilder.build("/tasks/");
    String requestType = "PATCH";
    ControllerRegistry sut = new ControllerRegistry(null);
    // THEN
    expectedException.expect(MethodNotFoundException.class);
    // WHEN
    sut.getController(jsonPath, requestType);
}
Also used : ControllerRegistry(io.crnk.core.engine.internal.dispatcher.ControllerRegistry) PathBuilder(io.crnk.core.engine.internal.dispatcher.path.PathBuilder) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Example 30 with JsonPath

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

the class OperationsModule method enrichTypeIdInformation.

private void enrichTypeIdInformation(List<Operation> operations) {
    ResourceRegistry resourceRegistry = moduleContext.getResourceRegistry();
    for (Operation operation : operations) {
        if (operation.getOp().equalsIgnoreCase(HttpMethod.DELETE.toString())) {
            String path = OperationParameterUtils.parsePath(operation.getPath());
            JsonPath jsonPath = (new PathBuilder(resourceRegistry)).build(path);
            Resource resource = new Resource();
            resource.setType(jsonPath.getResourceType());
            resource.setId(jsonPath.getIds().getIds().get(0));
            operation.setValue(resource);
        }
    }
}
Also used : PathBuilder(io.crnk.core.engine.internal.dispatcher.path.PathBuilder) Resource(io.crnk.core.engine.document.Resource) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) OrderedOperation(io.crnk.operations.server.order.OrderedOperation) Operation(io.crnk.operations.Operation) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath)

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