Search in sources :

Example 1 with ConfigurationPersistData

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

the class DefaultConfigurationPersistenceStrategy method persistConfigurationCollection.

@Override
public boolean persistConfigurationCollection(ResourceResolver resourceResolver, String configResourceCollectionParentPath, ConfigurationCollectionPersistData data) {
    if (!config.enabled()) {
        return false;
    }
    Resource configResourceParent = getOrCreateResource(resourceResolver, configResourceCollectionParentPath, data.getProperties());
    // delete existing children and create new ones
    deleteChildren(configResourceParent);
    for (ConfigurationPersistData item : data.getItems()) {
        String path = configResourceParent.getPath() + "/" + item.getCollectionItemName();
        getOrCreateResource(resourceResolver, path, item.getProperties());
    }
    commit(resourceResolver, configResourceCollectionParentPath);
    return true;
}
Also used : ConfigurationPersistData(org.apache.sling.caconfig.spi.ConfigurationPersistData) Resource(org.apache.sling.api.resource.Resource)

Example 2 with ConfigurationPersistData

use of org.apache.sling.caconfig.spi.ConfigurationPersistData 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 3 with ConfigurationPersistData

use of org.apache.sling.caconfig.spi.ConfigurationPersistData 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 4 with ConfigurationPersistData

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

the class ConfigurationManagerImplTest method testPersistConfigurationCollection_Nested.

@Test
public void testPersistConfigurationCollection_Nested() throws Exception {
    underTest.persistConfigurationCollection(contextResourceLevel2, getConfigResolvePath(CONFIG_NESTED_NAME) + "/propSubList", new ConfigurationCollectionPersistData(ImmutableList.of(new ConfigurationPersistData(ImmutableMap.<String, Object>of("prop1", "value1_persist")).collectionItemName("item1"), new ConfigurationPersistData(ImmutableMap.<String, Object>of("prop1", "value2_persist")).collectionItemName("item2"), new ConfigurationPersistData(ImmutableMap.<String, Object>of("prop1", "value3_persist")).collectionItemName("item3"))));
    context.resourceResolver().commit();
    ConfigurationData configData = underTest.getConfiguration(contextResourceLevel2, CONFIG_NESTED_NAME);
    ConfigurationData[] subListData = configData.getValues().get("propSubList", ConfigurationData[].class);
    assertEquals(3, subListData.length);
    assertEquals("value1_persist", subListData[0].getValues().get("prop1", String.class));
    assertEquals("value2_persist", subListData[1].getValues().get("prop1", String.class));
    assertEquals("value3_persist", subListData[2].getValues().get("prop1", String.class));
}
Also used : ConfigurationCollectionPersistData(org.apache.sling.caconfig.spi.ConfigurationCollectionPersistData) ConfigurationPersistData(org.apache.sling.caconfig.spi.ConfigurationPersistData) ConfigurationData(org.apache.sling.caconfig.management.ConfigurationData) Test(org.junit.Test)

Example 5 with ConfigurationPersistData

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

the class ConfigurationManagerIT method testWriteConfigCollection.

@Test
public void testWriteConfigCollection() throws Exception {
    // write configuration data via configuration manager
    Map<String, Object> values1 = new HashMap<>();
    values1.put("stringParam", "valueA");
    values1.put("stringParamDefault", "valueB");
    Map<String, Object> values2 = new HashMap<>();
    values2.put("intParam", 55);
    values2.put("boolParam", true);
    List<ConfigurationPersistData> items = new ArrayList<>();
    items.add(new ConfigurationPersistData(values1).collectionItemName("item1"));
    items.add(new ConfigurationPersistData(values2).collectionItemName("item2"));
    configManager.persistConfigurationCollection(resourcePage1, CONFIG_NAME, new ConfigurationCollectionPersistData(items));
    resourceResolver.commit();
    // read config via configuration resolver
    Collection<SimpleConfig> config = configResolver.get(resourcePage1).asCollection(SimpleConfig.class);
    assertEquals(2, config.size());
    Iterator<SimpleConfig> configIterator = config.iterator();
    SimpleConfig config1 = configIterator.next();
    SimpleConfig config2 = configIterator.next();
    assertEquals("valueA", config1.stringParam());
    assertEquals("valueB", config1.stringParamDefault());
    assertEquals(55, (int) config2.intParam());
    assertEquals(true, config2.boolParam());
}
Also used : SimpleConfig(org.apache.sling.caconfig.it.example.SimpleConfig) HashMap(java.util.HashMap) ConfigurationPersistData(org.apache.sling.caconfig.spi.ConfigurationPersistData) ConfigurationCollectionPersistData(org.apache.sling.caconfig.spi.ConfigurationCollectionPersistData) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

ConfigurationPersistData (org.apache.sling.caconfig.spi.ConfigurationPersistData)17 Test (org.junit.Test)12 ConfigurationCollectionPersistData (org.apache.sling.caconfig.spi.ConfigurationCollectionPersistData)9 Resource (org.apache.sling.api.resource.Resource)8 ValueMap (org.apache.sling.api.resource.ValueMap)4 ConfigurationPersistenceStrategy2 (org.apache.sling.caconfig.spi.ConfigurationPersistenceStrategy2)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 SimpleConfig (org.apache.sling.caconfig.it.example.SimpleConfig)2 ConfigurationData (org.apache.sling.caconfig.management.ConfigurationData)2 ConfigurationManager (org.apache.sling.caconfig.management.ConfigurationManager)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 DefaultConfigurationPersistenceStrategy (org.apache.sling.caconfig.impl.def.DefaultConfigurationPersistenceStrategy)1