Search in sources :

Example 6 with PropertiesProvider

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

the class FallbackServiceDiscoveryFactoryTest method fallbackToEmptyDiscoveryIfNoPackageAndDefaultAvailable.

@Test
public void fallbackToEmptyDiscoveryIfNoPackageAndDefaultAvailable() {
    PropertiesProvider propertiesProvider = Mockito.mock(PropertiesProvider.class);
    ServiceDiscoveryFactory defaultFactory = new ServiceDiscoveryFactory() {

        @Override
        public ServiceDiscovery getInstance() {
            return null;
        }
    };
    FallbackServiceDiscoveryFactory fallbackFactory = new FallbackServiceDiscoveryFactory(defaultFactory, new SampleJsonServiceLocator(), propertiesProvider);
    Assert.assertTrue(fallbackFactory.getInstance() instanceof EmptyServiceDiscovery);
}
Also used : PropertiesProvider(io.crnk.core.engine.properties.PropertiesProvider) NullPropertiesProvider(io.crnk.core.engine.properties.NullPropertiesProvider) SampleJsonServiceLocator(io.crnk.legacy.locator.SampleJsonServiceLocator) Test(org.junit.Test)

Example 7 with PropertiesProvider

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

the class FallbackServiceDiscoveryFactoryTest method fallbackToReflectionsIfNoDefaultAvailable.

@Test
public void fallbackToReflectionsIfNoDefaultAvailable() {
    PropertiesProvider propertiesProvider = Mockito.mock(PropertiesProvider.class);
    Mockito.when(propertiesProvider.getProperty(Mockito.eq(CrnkProperties.RESOURCE_SEARCH_PACKAGE))).thenReturn("a.b.c");
    ServiceDiscoveryFactory defaultFactory = new ServiceDiscoveryFactory() {

        @Override
        public ServiceDiscovery getInstance() {
            return null;
        }
    };
    FallbackServiceDiscoveryFactory fallbackFactory = new FallbackServiceDiscoveryFactory(defaultFactory, new SampleJsonServiceLocator(), propertiesProvider);
    Assert.assertTrue(fallbackFactory.getInstance() instanceof ReflectionsServiceDiscovery);
}
Also used : PropertiesProvider(io.crnk.core.engine.properties.PropertiesProvider) NullPropertiesProvider(io.crnk.core.engine.properties.NullPropertiesProvider) SampleJsonServiceLocator(io.crnk.legacy.locator.SampleJsonServiceLocator) Test(org.junit.Test)

Example 8 with PropertiesProvider

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

the class CrnkConfigV3Test method checkProperties.

@Test
public void checkProperties() {
    ApplicationContext applicationContext = Mockito.mock(ApplicationContext.class);
    Mockito.when(applicationContext.getEnvironment()).thenReturn(Mockito.mock(Environment.class));
    CrnkSpringBootProperties properties = new CrnkSpringBootProperties();
    properties.setDomainName("testDomain");
    properties.setDefaultPageLimit(12L);
    properties.setMaxPageLimit(20L);
    properties.setPathPrefix("prefix");
    properties.setAllowUnknownAttributes(true);
    properties.setReturn404OnNull(true);
    properties.setResourcePackage("ch.something");
    ObjectMapper objectMapper = new ObjectMapper();
    CrnkConfigV3 config = new CrnkConfigV3(properties, objectMapper);
    config.setApplicationContext(applicationContext);
    SpringServiceDiscovery serviceDiscovery = Mockito.mock(SpringServiceDiscovery.class);
    CrnkBoot boot = config.crnkBoot(serviceDiscovery);
    PropertiesProvider propertiesProvider = boot.getPropertiesProvider();
    Assert.assertEquals("testDomain", propertiesProvider.getProperty(CrnkProperties.RESOURCE_DEFAULT_DOMAIN));
    Assert.assertEquals("ch.something", propertiesProvider.getProperty(CrnkProperties.RESOURCE_SEARCH_PACKAGE));
    Assert.assertEquals("prefix", propertiesProvider.getProperty(CrnkProperties.WEB_PATH_PREFIX));
    Assert.assertEquals("true", propertiesProvider.getProperty(CrnkProperties.ALLOW_UNKNOWN_ATTRIBUTES));
    Assert.assertEquals("true", propertiesProvider.getProperty(CrnkProperties.RETURN_404_ON_NULL));
    DefaultQuerySpecDeserializer deserializer = (DefaultQuerySpecDeserializer) boot.getQuerySpecDeserializer();
    Assert.assertTrue(deserializer.getAllowUnknownAttributes());
    ConstantServiceUrlProvider constantServiceUrlProvider = (ConstantServiceUrlProvider) boot.getServiceUrlProvider();
    Assert.assertEquals("testDomainprefix", constantServiceUrlProvider.getUrl());
    Assert.assertSame(objectMapper, boot.getObjectMapper());
    Assert.assertNotNull(boot.getModuleRegistry().getSecurityProvider());
}
Also used : PropertiesProvider(io.crnk.core.engine.properties.PropertiesProvider) ApplicationContext(org.springframework.context.ApplicationContext) CrnkBoot(io.crnk.core.boot.CrnkBoot) DefaultQuerySpecDeserializer(io.crnk.core.queryspec.DefaultQuerySpecDeserializer) Environment(org.springframework.core.env.Environment) ConstantServiceUrlProvider(io.crnk.core.engine.url.ConstantServiceUrlProvider) CrnkSpringBootProperties(io.crnk.spring.boot.CrnkSpringBootProperties) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) CrnkConfigV3(io.crnk.spring.boot.v3.CrnkConfigV3) SpringServiceDiscovery(io.crnk.spring.internal.SpringServiceDiscovery) Test(org.junit.Test)

Example 9 with PropertiesProvider

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

the class DefaultResourceInformationProviderTest method getResourceInformationProviderWithProperty.

private ResourceInformationProvider getResourceInformationProviderWithProperty(String key, String value) {
    PropertiesProvider propertiesProvider = Mockito.mock(PropertiesProvider.class);
    Mockito.when(propertiesProvider.getProperty(Mockito.eq(key))).thenReturn(value);
    ResourceInformationProvider resourceInformationProvider = new DefaultResourceInformationProvider(propertiesProvider, new OffsetLimitPagingBehavior(), new DefaultResourceFieldInformationProvider(), new JacksonResourceFieldInformationProvider());
    resourceInformationProvider.init(context);
    return resourceInformationProvider;
}
Also used : PropertiesProvider(io.crnk.core.engine.properties.PropertiesProvider) NullPropertiesProvider(io.crnk.core.engine.properties.NullPropertiesProvider) OffsetLimitPagingBehavior(io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior) DefaultResourceInformationProvider(io.crnk.core.engine.internal.information.resource.DefaultResourceInformationProvider) ResourceInformationProvider(io.crnk.core.engine.information.resource.ResourceInformationProvider) DefaultResourceInformationProvider(io.crnk.core.engine.internal.information.resource.DefaultResourceInformationProvider) JacksonResourceFieldInformationProvider(io.crnk.core.engine.internal.jackson.JacksonResourceFieldInformationProvider) DefaultResourceFieldInformationProvider(io.crnk.core.engine.internal.information.resource.DefaultResourceFieldInformationProvider)

Example 10 with PropertiesProvider

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

the class ResourcePatchTest method onPatchingReadOnlyFieldReturnBadRequestWithFailBehavior.

@Test
public void onPatchingReadOnlyFieldReturnBadRequestWithFailBehavior() throws Exception {
    // GIVEN
    Document requestDocument = new Document();
    Resource data = createTask();
    requestDocument.setData(Nullable.of((Object) data));
    JsonPath postPath = pathBuilder.build("/tasks");
    ResourcePost post = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    post.handle(postPath, emptyTaskQuery, null, requestDocument);
    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;
        }
    };
    ResourcePatch sut = new ResourcePatch(resourceRegistry, propertiesProvider, typeParser, objectMapper, documentMapper, modificationFilters);
    data.getAttributes().put("readOnlyValue", objectMapper.readTree("\"newValue\""));
    // WHEN
    try {
        JsonPath patchPath = pathBuilder.build("/tasks/" + data.getId());
        sut.handle(patchPath, 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) ForbiddenException(io.crnk.core.exception.ForbiddenException) Resource(io.crnk.core.engine.document.Resource) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) ResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) 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