Search in sources :

Example 6 with ConfigurationPersistData

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

the class MockContextAwareConfig method writeConfiguration.

/**
     * Writes configuration parameters using the primary configured persistence
     * provider.
     * @param context Sling context
     * @param contextPath Configuration id
     * @param configName Config name
     * @param values Configuration values
     */
public static void writeConfiguration(SlingContextImpl context, String contextPath, String configName, Map<String, Object> values) {
    ConfigurationManager configManager = context.getService(ConfigurationManager.class);
    Resource contextResource = context.resourceResolver().getResource(contextPath);
    configManager.persistConfiguration(contextResource, configName, new ConfigurationPersistData(values));
}
Also used : ConfigurationPersistData(org.apache.sling.caconfig.spi.ConfigurationPersistData) Resource(org.apache.sling.api.resource.Resource) ConfigurationManager(org.apache.sling.caconfig.management.ConfigurationManager)

Example 7 with ConfigurationPersistData

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

the class MockContextAwareConfig method writeConfigurationCollection.

/**
     * Writes a collection of configuration parameters using the primary
     * configured persistence provider.
     * @param context Sling context
     * @param contextPath Configuration id
     * @param configName Config name
     * @param values Configuration values
     */
public static void writeConfigurationCollection(SlingContextImpl context, String contextPath, String configName, Collection<Map<String, Object>> values) {
    ConfigurationManager configManager = context.getService(ConfigurationManager.class);
    Resource contextResource = context.resourceResolver().getResource(contextPath);
    List<ConfigurationPersistData> items = new ArrayList<>();
    int index = 0;
    for (Map<String, Object> map : values) {
        items.add(new ConfigurationPersistData(map).collectionItemName("item" + (index++)));
    }
    configManager.persistConfigurationCollection(contextResource, configName, new ConfigurationCollectionPersistData(items));
}
Also used : ConfigurationPersistData(org.apache.sling.caconfig.spi.ConfigurationPersistData) ConfigurationCollectionPersistData(org.apache.sling.caconfig.spi.ConfigurationCollectionPersistData) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) ConfigurationManager(org.apache.sling.caconfig.management.ConfigurationManager)

Example 8 with ConfigurationPersistData

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

the class ConfigurationManagerIT method testWriteConfig.

@Test
public void testWriteConfig() throws Exception {
    // write configuration data via configuration manager
    Map<String, Object> values = new HashMap<>();
    values.put("stringParam", "valueA");
    values.put("stringParamDefault", "valueB");
    values.put("intParam", 55);
    values.put("boolParam", true);
    configManager.persistConfiguration(resourcePage1, CONFIG_NAME, new ConfigurationPersistData(values));
    resourceResolver.commit();
    // read config via configuration resolver
    SimpleConfig config = configResolver.get(resourcePage1).as(SimpleConfig.class);
    assertNotNull(config);
    assertEquals("valueA", config.stringParam());
    assertEquals("valueB", config.stringParamDefault());
    assertEquals(55, (int) config.intParam());
    assertEquals(true, config.boolParam());
}
Also used : SimpleConfig(org.apache.sling.caconfig.it.example.SimpleConfig) HashMap(java.util.HashMap) ConfigurationPersistData(org.apache.sling.caconfig.spi.ConfigurationPersistData) Test(org.junit.Test)

Example 9 with ConfigurationPersistData

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

the class ConfigurationPersistenceStrategyMultiplexerImplTest method testWithDefaultStrategy.

@Test
public void testWithDefaultStrategy() {
    context.registerInjectActivateService(new DefaultConfigurationPersistenceStrategy());
    assertSame(resource1, underTest.getResource(resource1));
    assertEquals(resource1.getPath(), underTest.getResourcePath(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 : ConfigurationPersistData(org.apache.sling.caconfig.spi.ConfigurationPersistData) ConfigurationCollectionPersistData(org.apache.sling.caconfig.spi.ConfigurationCollectionPersistData) DefaultConfigurationPersistenceStrategy(org.apache.sling.caconfig.impl.def.DefaultConfigurationPersistenceStrategy) Test(org.junit.Test)

Example 10 with ConfigurationPersistData

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

the class ConfigurationPersistenceStrategyMultiplexerImplTest method testWithNoStrategies.

@Test
public void testWithNoStrategies() {
    assertNull(underTest.getResource(resource1));
    assertNull(underTest.getResourcePath(resource1.getPath()));
    assertFalse(underTest.persistConfiguration(context.resourceResolver(), "/conf/test1", new ConfigurationPersistData(resource1.getValueMap())));
    assertFalse(underTest.persistConfigurationCollection(context.resourceResolver(), "/conf/testCol", new ConfigurationCollectionPersistData(ImmutableList.of(new ConfigurationPersistData(resource1.getValueMap()).collectionItemName(resource1.getName()), new ConfigurationPersistData(resource2.getValueMap()).collectionItemName(resource2.getName())))));
    assertFalse(underTest.deleteConfiguration(context.resourceResolver(), "/conf/test1"));
}
Also used : ConfigurationPersistData(org.apache.sling.caconfig.spi.ConfigurationPersistData) ConfigurationCollectionPersistData(org.apache.sling.caconfig.spi.ConfigurationCollectionPersistData) 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