Search in sources :

Example 11 with PropertiesProvider

use of io.crnk.core.engine.properties.PropertiesProvider in project crnk-framework by crnk-project.

the class ResourcePostTest method onPostingReadOnlyFieldShouldIgnoreWithIgnoreBehavior.

@Test
public void onPostingReadOnlyFieldShouldIgnoreWithIgnoreBehavior() throws Exception {
    // GIVEN
    Document requestDocument = new Document();
    Resource data = createTask();
    data.getAttributes().put("readOnlyValue", objectMapper.readTree("\"newValue\""));
    requestDocument.setData(Nullable.of((Object) data));
    JsonPath path = pathBuilder.build("/tasks");
    PropertiesProvider propertiesProvider = new PropertiesProvider() {

        @Override
        public String getProperty(String key) {
            if (CrnkProperties.RESOURCE_FIELD_IMMUTABLE_WRITE_BEHAVIOR.equals(key)) {
                return ResourceFieldImmutableWriteBehavior.IGNORE.toString();
            }
            return null;
        }
    };
    ResourcePost sut = new ResourcePost(resourceRegistry, propertiesProvider, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response response = sut.handle(path, emptyTaskQuery, null, requestDocument);
    String persistedValue = response.getDocument().getSingleData().get().getAttributes().get("readOnlyValue").asText();
    Assert.assertEquals("someReadOnlyValue", persistedValue);
}
Also used : PropertiesProvider(io.crnk.core.engine.properties.PropertiesProvider) Response(io.crnk.core.engine.dispatcher.Response) Resource(io.crnk.core.engine.document.Resource) 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) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 12 with PropertiesProvider

use of io.crnk.core.engine.properties.PropertiesProvider in project crnk-framework by crnk-project.

the class ResourcePostTest method onPostingReadOnlyFieldReturnBadRequestWithFailBehavior.

@Test
public void onPostingReadOnlyFieldReturnBadRequestWithFailBehavior() throws Exception {
    // GIVEN
    Document requestDocument = new Document();
    Resource data = createTask();
    data.getAttributes().put("readOnlyValue", objectMapper.readTree("\"newValue\""));
    requestDocument.setData(Nullable.of((Object) data));
    JsonPath path = pathBuilder.build("/tasks");
    PropertiesProvider propertiesProvider = new PropertiesProvider() {

        @Override
        public String getProperty(String key) {
            if (CrnkProperties.RESOURCE_FIELD_IMMUTABLE_WRITE_BEHAVIOR.equals(key)) {
                return ResourceFieldImmutableWriteBehavior.FAIL.toString();
            }
            return null;
        }
    };
    ResourcePost sut = new ResourcePost(resourceRegistry, propertiesProvider, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    try {
        sut.handle(path, emptyTaskQuery, null, requestDocument);
        Assert.fail("should not be allowed to update read-only field");
    } catch (ForbiddenException e) {
        Assert.assertEquals("field 'readOnlyValue' cannot be modified", e.getMessage());
    }
}
Also used : PropertiesProvider(io.crnk.core.engine.properties.PropertiesProvider) Resource(io.crnk.core.engine.document.Resource) 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) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 13 with PropertiesProvider

use of io.crnk.core.engine.properties.PropertiesProvider in project crnk-framework by crnk-project.

the class ControllerRegistryBuilderTest method onBuildShouldAddAllControllers.

@Test
public void onBuildShouldAddAllControllers() throws Exception {
    // GIVEN
    PropertiesProvider propertiesProvider = Mockito.mock(PropertiesProvider.class);
    ResourceFilterDirectory resourceFilterDirectory = Mockito.mock(ResourceFilterDirectory.class);
    ControllerRegistryBuilder sut = new ControllerRegistryBuilder(null, null, null, propertiesProvider, resourceFilterDirectory, (List) Collections.emptyList());
    // WHEN
    ControllerRegistry result = sut.build();
    // THEN
    result.getController(new ResourcePath("path"), "GET");
}
Also used : PropertiesProvider(io.crnk.core.engine.properties.PropertiesProvider) ControllerRegistry(io.crnk.core.engine.internal.dispatcher.ControllerRegistry) ResourcePath(io.crnk.core.engine.internal.dispatcher.path.ResourcePath) ResourceFilterDirectory(io.crnk.core.engine.filter.ResourceFilterDirectory) ControllerRegistryBuilder(io.crnk.core.engine.internal.dispatcher.ControllerRegistryBuilder) Test(org.junit.Test)

Example 14 with PropertiesProvider

use of io.crnk.core.engine.properties.PropertiesProvider in project crnk-framework by crnk-project.

the class IncludeLookupSetterBaseTest method includePropertiesProviderAllTrueRelationshipLookup.

@Test
public void includePropertiesProviderAllTrueRelationshipLookup() throws Exception {
    PropertiesProvider propertiesProvider = new PropertiesProvider() {

        @Override
        public String getProperty(String key) {
            if (key.equalsIgnoreCase(CrnkProperties.INCLUDE_AUTOMATICALLY_OVERWRITE)) {
                return "true";
            }
            return null;
        }
    };
    mapper = new DocumentMapper(resourceRegistry, objectMapper, propertiesProvider, resourceFilterDirectory);
    QuerySpec querySpec = new QuerySpec(Task.class);
    querySpec.includeRelation(Arrays.asList("project"));
    Task task = new Task();
    task.setId(1L);
    Document document = mapper.toDocument(toResponse(task), toAdapter(querySpec));
    Resource taskResource = document.getSingleData().get();
    assertNotNull(taskResource.getRelationships().get("project"));
    assertNotNull(taskResource.getRelationships().get("project").getData());
}
Also used : PropertiesProvider(io.crnk.core.engine.properties.PropertiesProvider) EmptyPropertiesProvider(io.crnk.core.engine.properties.EmptyPropertiesProvider) HierarchicalTask(io.crnk.core.mock.models.HierarchicalTask) Task(io.crnk.core.mock.models.Task) DocumentMapper(io.crnk.core.engine.internal.document.mapper.DocumentMapper) Resource(io.crnk.core.engine.document.Resource) QuerySpec(io.crnk.core.queryspec.QuerySpec) Document(io.crnk.core.engine.document.Document) AbstractDocumentMapperTest(io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest) Test(org.junit.Test)

Example 15 with PropertiesProvider

use of io.crnk.core.engine.properties.PropertiesProvider in project crnk-framework by crnk-project.

the class IncludeLookupUtilTest method checkDefaultLookupIncludeBehavior.

@Test
public void checkDefaultLookupIncludeBehavior() {
    Assert.assertEquals(LookupIncludeBehavior.DEFAULT, IncludeLookupUtil.getGlolbalLookupIncludeBehavior(null));
    PropertiesProvider propertiesProvider = Mockito.mock(PropertiesProvider.class);
    Assert.assertEquals(LookupIncludeBehavior.DEFAULT, IncludeLookupUtil.getGlolbalLookupIncludeBehavior(propertiesProvider));
    Mockito.when(propertiesProvider.getProperty(CrnkProperties.INCLUDE_AUTOMATICALLY)).thenReturn("true");
    Assert.assertEquals(LookupIncludeBehavior.AUTOMATICALLY_WHEN_NULL, IncludeLookupUtil.getGlolbalLookupIncludeBehavior(propertiesProvider));
    Mockito.when(propertiesProvider.getProperty(CrnkProperties.INCLUDE_AUTOMATICALLY)).thenReturn("false");
    Assert.assertEquals(LookupIncludeBehavior.DEFAULT, IncludeLookupUtil.getGlolbalLookupIncludeBehavior(propertiesProvider));
    Mockito.when(propertiesProvider.getProperty(CrnkProperties.INCLUDE_AUTOMATICALLY_OVERWRITE)).thenReturn("true");
    Assert.assertEquals(LookupIncludeBehavior.AUTOMATICALLY_ALWAYS, IncludeLookupUtil.getGlolbalLookupIncludeBehavior(propertiesProvider));
    Mockito.when(propertiesProvider.getProperty(CrnkProperties.INCLUDE_AUTOMATICALLY_OVERWRITE)).thenReturn("false");
    Assert.assertEquals(LookupIncludeBehavior.DEFAULT, IncludeLookupUtil.getGlolbalLookupIncludeBehavior(propertiesProvider));
}
Also used : PropertiesProvider(io.crnk.core.engine.properties.PropertiesProvider) AbstractDocumentMapperTest(io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest) Test(org.junit.Test)

Aggregations

PropertiesProvider (io.crnk.core.engine.properties.PropertiesProvider)17 Test (org.junit.Test)13 Document (io.crnk.core.engine.document.Document)5 Resource (io.crnk.core.engine.document.Resource)5 NullPropertiesProvider (io.crnk.core.engine.properties.NullPropertiesProvider)5 CrnkBoot (io.crnk.core.boot.CrnkBoot)4 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)3 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)3 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)3 AbstractDocumentMapperTest (io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest)3 ConstantServiceUrlProvider (io.crnk.core.engine.url.ConstantServiceUrlProvider)3 SampleJsonServiceLocator (io.crnk.legacy.locator.SampleJsonServiceLocator)3 DocumentMapper (io.crnk.core.engine.internal.document.mapper.DocumentMapper)2 EmptyPropertiesProvider (io.crnk.core.engine.properties.EmptyPropertiesProvider)2 HierarchicalTask (io.crnk.core.mock.models.HierarchicalTask)2 Task (io.crnk.core.mock.models.Task)2 ReflectionsServiceDiscovery (io.crnk.core.module.discovery.ReflectionsServiceDiscovery)2 QuerySpec (io.crnk.core.queryspec.QuerySpec)2 Before (org.junit.Before)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1