Search in sources :

Example 1 with ConfigurationPersistenceStrategy2

use of org.apache.sling.caconfig.spi.ConfigurationPersistenceStrategy2 in project sling by apache.

the class DefaultConfigurationPersistenceStrategyTest method testGetConfigName.

@Test
public void testGetConfigName() throws Exception {
    ConfigurationPersistenceStrategy2 underTest = context.registerInjectActivateService(new DefaultConfigurationPersistenceStrategy());
    String path = "test";
    assertEquals(path, underTest.getConfigName(path, null));
    assertEquals(path, underTest.getCollectionParentConfigName(path, null));
    assertEquals(path, underTest.getCollectionItemConfigName(path, null));
}
Also used : ConfigurationPersistenceStrategy2(org.apache.sling.caconfig.spi.ConfigurationPersistenceStrategy2) Test(org.junit.Test)

Example 2 with ConfigurationPersistenceStrategy2

use of org.apache.sling.caconfig.spi.ConfigurationPersistenceStrategy2 in project sling by apache.

the class DefaultConfigurationPersistenceStrategyTest method testGetResourcePath.

@Test
public void testGetResourcePath() {
    ConfigurationPersistenceStrategy2 underTest = context.registerInjectActivateService(new DefaultConfigurationPersistenceStrategy());
    String path = "/conf/test";
    assertEquals(path, underTest.getResourcePath(path));
    assertEquals(path, underTest.getCollectionParentResourcePath(path));
    assertEquals(path, underTest.getCollectionItemResourcePath(path));
}
Also used : ConfigurationPersistenceStrategy2(org.apache.sling.caconfig.spi.ConfigurationPersistenceStrategy2) Test(org.junit.Test)

Example 3 with ConfigurationPersistenceStrategy2

use of org.apache.sling.caconfig.spi.ConfigurationPersistenceStrategy2 in project sling by apache.

the class DefaultConfigurationPersistenceStrategyTest method testPersistConfigurationCollection.

@Test
public void testPersistConfigurationCollection() throws Exception {
    ConfigurationPersistenceStrategy2 underTest = context.registerInjectActivateService(new DefaultConfigurationPersistenceStrategy());
    // store new config collection items
    assertTrue(underTest.persistConfigurationCollection(context.resourceResolver(), "/conf/test", new ConfigurationCollectionPersistData(ImmutableList.of(new ConfigurationPersistData(ImmutableMap.<String, Object>of("prop1", "value1")).collectionItemName("0"), new ConfigurationPersistData(ImmutableMap.<String, Object>of("prop2", 5)).collectionItemName("1"))).properties(ImmutableMap.<String, Object>of("prop1", "abc", "sling:resourceType", "/a/b/c"))));
    context.resourceResolver().commit();
    Resource resource = context.resourceResolver().getResource("/conf/test");
    assertEquals(2, ImmutableList.copyOf(resource.getChildren()).size());
    ValueMap props0 = context.resourceResolver().getResource("/conf/test/0").getValueMap();
    assertEquals("value1", props0.get("prop1", String.class));
    ValueMap props1 = context.resourceResolver().getResource("/conf/test/1").getValueMap();
    assertEquals((Integer) 5, props1.get("prop2", Integer.class));
    assertThat(resource, ResourceMatchers.props("prop1", "abc", "sling:resourceType", "/a/b/c"));
    // remove config collection items
    assertTrue(underTest.persistConfigurationCollection(context.resourceResolver(), "/conf/test", new ConfigurationCollectionPersistData(ImmutableList.<ConfigurationPersistData>of())));
    context.resourceResolver().commit();
    resource = context.resourceResolver().getResource("/conf/test");
    assertEquals(0, ImmutableList.copyOf(resource.getChildren()).size());
    underTest.deleteConfiguration(context.resourceResolver(), "/conf/test");
    assertNull(context.resourceResolver().getResource("/conf/test"));
}
Also used : ConfigurationPersistenceStrategy2(org.apache.sling.caconfig.spi.ConfigurationPersistenceStrategy2) ConfigurationCollectionPersistData(org.apache.sling.caconfig.spi.ConfigurationCollectionPersistData) ConfigurationPersistData(org.apache.sling.caconfig.spi.ConfigurationPersistData) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 4 with ConfigurationPersistenceStrategy2

use of org.apache.sling.caconfig.spi.ConfigurationPersistenceStrategy2 in project sling by apache.

the class ConfigurationPersistenceStrategyMultiplexerImplTest method testMultipleStrategies.

@SuppressWarnings("deprecation")
@Test
public void testMultipleStrategies() {
    // strategy 1 (using old ConfigurationPersistenceStrategy with bridge to  ConfigurationPersistenceStrategy2)
    context.registerService(org.apache.sling.caconfig.spi.ConfigurationPersistenceStrategy.class, new org.apache.sling.caconfig.spi.ConfigurationPersistenceStrategy() {

        @Override
        public Resource getResource(Resource resource) {
            return resource2;
        }

        @Override
        public String getResourcePath(String resourcePath) {
            return resource2.getPath();
        }

        @Override
        public boolean persistConfiguration(ResourceResolver resourceResolver, String configResourcePath, ConfigurationPersistData data) {
            return true;
        }

        @Override
        public boolean persistConfigurationCollection(ResourceResolver resourceResolver, String configResourceCollectionParentPath, ConfigurationCollectionPersistData data) {
            return false;
        }

        @Override
        public boolean deleteConfiguration(ResourceResolver resourceResolver, String configResourcePath) {
            return false;
        }
    }, Constants.SERVICE_RANKING, 2000);
    // strategy 2
    context.registerService(ConfigurationPersistenceStrategy2.class, new ConfigurationPersistenceStrategy2() {

        @Override
        public Resource getResource(Resource resource) {
            return resource1;
        }

        @Override
        public Resource getCollectionParentResource(Resource resource) {
            return resource1;
        }

        @Override
        public Resource getCollectionItemResource(Resource resource) {
            return resource1;
        }

        @Override
        public String getResourcePath(String resourcePath) {
            return resource1.getPath();
        }

        @Override
        public String getCollectionParentResourcePath(String resourcePath) {
            return resource1.getPath();
        }

        @Override
        public String getCollectionItemResourcePath(String resourcePath) {
            return resource1.getPath();
        }

        @Override
        public String getConfigName(String configName, String relatedConfigPath) {
            return resource1.getPath();
        }

        @Override
        public String getCollectionParentConfigName(String configName, String relatedConfigPath) {
            return resource1.getPath();
        }

        @Override
        public String getCollectionItemConfigName(String configName, String relatedConfigPath) {
            return resource1.getPath();
        }

        @Override
        public boolean persistConfiguration(ResourceResolver resourceResolver, String configResourcePath, ConfigurationPersistData data) {
            return false;
        }

        @Override
        public boolean persistConfigurationCollection(ResourceResolver resourceResolver, String configResourceCollectionParentPath, ConfigurationCollectionPersistData data) {
            return true;
        }

        @Override
        public boolean deleteConfiguration(ResourceResolver resourceResolver, String configResourcePath) {
            return true;
        }
    }, Constants.SERVICE_RANKING, 1000);
    assertSame(resource2, underTest.getResource(resource1));
    assertSame(resource1, underTest.getCollectionParentResource(resource1));
    assertSame(resource2, underTest.getCollectionItemResource(resource1));
    assertEquals(resource2.getPath(), underTest.getResourcePath(resource1.getPath()));
    assertEquals(resource1.getPath(), underTest.getCollectionParentResourcePath(resource1.getPath()));
    assertEquals(resource2.getPath(), underTest.getCollectionItemResourcePath(resource1.getPath()));
    assertEquals(resource2.getPath(), underTest.getConfigName(resource1.getPath(), null));
    assertEquals(resource1.getPath(), underTest.getCollectionParentConfigName(resource1.getPath(), null));
    assertEquals(resource2.getPath(), underTest.getCollectionItemConfigName(resource1.getPath(), null));
    assertEquals(ImmutableList.of(resource2.getPath(), resource1.getPath()), ImmutableList.copyOf(underTest.getAllConfigNames(resource1.getPath())));
    assertEquals(ImmutableList.of(resource1.getPath()), ImmutableList.copyOf(underTest.getAllCollectionParentConfigNames(resource1.getPath())));
    assertEquals(ImmutableList.of(resource2.getPath(), resource1.getPath()), ImmutableList.copyOf(underTest.getAllCollectionItemConfigNames(resource1.getPath())));
    assertTrue(underTest.persistConfiguration(context.resourceResolver(), "/conf/test1", new ConfigurationPersistData(resource1.getValueMap())));
    assertTrue(underTest.persistConfigurationCollection(context.resourceResolver(), "/conf/testCol", new ConfigurationCollectionPersistData(ImmutableList.of(new ConfigurationPersistData(resource1.getValueMap()).collectionItemName(resource1.getName()), new ConfigurationPersistData(resource2.getValueMap()).collectionItemName(resource2.getName())))));
    assertTrue(underTest.deleteConfiguration(context.resourceResolver(), "/conf/test1"));
}
Also used : ConfigurationPersistenceStrategy2(org.apache.sling.caconfig.spi.ConfigurationPersistenceStrategy2) ConfigurationPersistData(org.apache.sling.caconfig.spi.ConfigurationPersistData) ConfigurationCollectionPersistData(org.apache.sling.caconfig.spi.ConfigurationCollectionPersistData) Resource(org.apache.sling.api.resource.Resource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Test(org.junit.Test)

Example 5 with ConfigurationPersistenceStrategy2

use of org.apache.sling.caconfig.spi.ConfigurationPersistenceStrategy2 in project sling by apache.

the class ConfigurationPersistenceStrategyPrinter method printConfiguration.

@Override
public void printConfiguration(PrintWriter pw, ServiceReference<ConfigurationPersistenceStrategy2> serviceReference, BundleContext bundleContext) {
    ConfigurationPersistenceStrategy2 service = bundleContext.getService(serviceReference);
    if (service instanceof ConfigurationPersistenceStrategyBridge.Adapter) {
        ConfigurationPersistenceStrategyBridge.Adapter adapter = (ConfigurationPersistenceStrategyBridge.Adapter) service;
        pw.print(INDENT);
        pw.print(BULLET);
        pw.println("Delegates to " + adapter.getOriginalServiceClass().getName());
    }
    bundleContext.ungetService(serviceReference);
}
Also used : ConfigurationPersistenceStrategyBridge(org.apache.sling.caconfig.impl.ConfigurationPersistenceStrategyBridge) ConfigurationPersistenceStrategy2(org.apache.sling.caconfig.spi.ConfigurationPersistenceStrategy2)

Aggregations

ConfigurationPersistenceStrategy2 (org.apache.sling.caconfig.spi.ConfigurationPersistenceStrategy2)8 Test (org.junit.Test)7 Resource (org.apache.sling.api.resource.Resource)4 ConfigurationPersistData (org.apache.sling.caconfig.spi.ConfigurationPersistData)4 ConfigurationCollectionPersistData (org.apache.sling.caconfig.spi.ConfigurationCollectionPersistData)3 ValueMap (org.apache.sling.api.resource.ValueMap)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 ConfigurationPersistenceStrategyBridge (org.apache.sling.caconfig.impl.ConfigurationPersistenceStrategyBridge)1