Search in sources :

Example 11 with ResourceModificationFilter

use of io.crnk.core.engine.filter.ResourceModificationFilter in project crnk-framework by crnk-project.

the class ResourceDeleteTest method onGivenRequestResourceGetShouldHandleIt.

@Test
public void onGivenRequestResourceGetShouldHandleIt() throws Exception {
    // GIVEN
    JsonPath jsonPath = pathBuilder.build("/tasks/1");
    ResourceDelete sut = new ResourceDelete(resourceRegistry, new ArrayList<ResourceModificationFilter>());
    // WHEN
    Response response = sut.handle(jsonPath, emptyTaskQuery, null, null);
    // THEN
    assertThat(response.getDocument()).isNull();
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter) ResourceDelete(io.crnk.core.engine.internal.dispatcher.controller.ResourceDelete) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 12 with ResourceModificationFilter

use of io.crnk.core.engine.filter.ResourceModificationFilter in project crnk-framework by crnk-project.

the class ResourceDeleteTest method onValidRequestShouldAcceptIt.

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

Example 13 with ResourceModificationFilter

use of io.crnk.core.engine.filter.ResourceModificationFilter in project crnk-framework by crnk-project.

the class ResourceDeleteTest method onNonRelationRequestShouldDenyIt.

@Test
public void onNonRelationRequestShouldDenyIt() {
    // GIVEN
    JsonPath jsonPath = new ResourcePath("tasks/1/relationships/project");
    ResourceRegistry resourceRegistry = mock(ResourceRegistry.class);
    ResourceDelete sut = new ResourceDelete(resourceRegistry, new ArrayList<ResourceModificationFilter>());
    // WHEN
    boolean result = sut.isAcceptable(jsonPath, REQUEST_TYPE);
    // THEN
    assertThat(result).isFalse();
}
Also used : ResourcePath(io.crnk.core.engine.internal.dispatcher.path.ResourcePath) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) ResourceDelete(io.crnk.core.engine.internal.dispatcher.controller.ResourceDelete) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 14 with ResourceModificationFilter

use of io.crnk.core.engine.filter.ResourceModificationFilter in project crnk-framework by crnk-project.

the class ResourceUpsert method setAttribute.

private void setAttribute(ResourceInformation resourceInformation, Object instance, String attributeName, JsonNode valueNode) {
    ResourceField field = resourceInformation.findAttributeFieldByName(attributeName);
    if (canModifyField(resourceInformation, attributeName, field)) {
        try {
            if (field != null) {
                Type valueType = field.getGenericType();
                Object value;
                if (valueNode != null) {
                    JavaType jacksonValueType = objectMapper.getTypeFactory().constructType(valueType);
                    ObjectReader reader = objectMapper.reader().forType(jacksonValueType);
                    value = reader.readValue(valueNode);
                } else {
                    value = null;
                }
                for (ResourceModificationFilter filter : modificationFilters) {
                    value = filter.modifyAttribute(instance, field, attributeName, value);
                }
                field.getAccessor().setValue(instance, value);
            } else if (resourceInformation.getAnyFieldAccessor() != null) {
                AnyResourceFieldAccessor anyFieldAccessor = resourceInformation.getAnyFieldAccessor();
                Object value = objectMapper.reader().forType(Object.class).readValue(valueNode);
                for (ResourceModificationFilter filter : modificationFilters) {
                    value = filter.modifyAttribute(instance, field, attributeName, value);
                }
                anyFieldAccessor.setValue(instance, attributeName, value);
            }
        } catch (IOException e) {
            throw new ResourceException(String.format("Exception while setting %s.%s=%s due to %s", instance, attributeName, valueNode, e.getMessage()), e);
        }
    }
}
Also used : ResourceRelationshipModificationType(io.crnk.core.engine.filter.ResourceRelationshipModificationType) JavaType(com.fasterxml.jackson.databind.JavaType) Type(java.lang.reflect.Type) JavaType(com.fasterxml.jackson.databind.JavaType) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter) ResourceException(io.crnk.core.exception.ResourceException) IOException(java.io.IOException)

Example 15 with ResourceModificationFilter

use of io.crnk.core.engine.filter.ResourceModificationFilter in project crnk-framework by crnk-project.

the class RelationshipsResourcePost method processToOneRelationship.

@Override
protected void processToOneRelationship(Object resource, ResourceInformation targetResourceInformation, ResourceField relationshipField, ResourceIdentifier resourceId, QueryAdapter queryAdapter, RelationshipRepositoryAdapter relationshipRepositoryForClass) {
    for (ResourceModificationFilter filter : modificationFilters) {
        resourceId = filter.modifyOneRelationship(resource, relationshipField, resourceId);
    }
    Serializable parsedId = null;
    if (resourceId != null) {
        parsedId = targetResourceInformation.parseIdString(resourceId.getId());
    }
    // noinspection unchecked
    relationshipRepositoryForClass.setRelation(resource, parsedId, relationshipField, queryAdapter);
}
Also used : Serializable(java.io.Serializable) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter)

Aggregations

ResourceModificationFilter (io.crnk.core.engine.filter.ResourceModificationFilter)19 Serializable (java.io.Serializable)9 Test (org.junit.Test)9 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)8 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)8 Response (io.crnk.core.engine.dispatcher.Response)7 Document (io.crnk.core.engine.document.Document)6 Resource (io.crnk.core.engine.document.Resource)6 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)5 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)4 ArrayList (java.util.ArrayList)4 ResourceDelete (io.crnk.core.engine.internal.dispatcher.controller.ResourceDelete)3 Project (io.crnk.core.mock.models.Project)3 Task (io.crnk.core.mock.models.Task)3 TaskToProjectRepository (io.crnk.core.mock.repository.TaskToProjectRepository)3 QueryParamsAdapter (io.crnk.legacy.internal.QueryParamsAdapter)3 DefaultQueryParamsParser (io.crnk.legacy.queryParams.DefaultQueryParamsParser)3 QueryParams (io.crnk.legacy.queryParams.QueryParams)3 QueryParamsBuilder (io.crnk.legacy.queryParams.QueryParamsBuilder)3 LinkedList (java.util.LinkedList)3