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));
}
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));
}
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"));
}
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"));
}
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);
}
Aggregations