use of org.apache.sling.api.resource.ValueMap in project sling by apache.
the class ConfigurationResolverValueMapTest method testConfig.
@Test
public void testConfig() {
context.build().resource("/conf/content/site1/sling:configs/sampleName", "stringParam", "configValue1", "intParam", 111, "boolParam", true);
ValueMap props = underTest.get(site1Page1).name("sampleName").asValueMap();
assertEquals("configValue1", props.get("stringParam", String.class));
assertEquals(111, (int) props.get("intParam", 0));
assertEquals(true, props.get("boolParam", false));
}
use of org.apache.sling.api.resource.ValueMap in project sling by apache.
the class DefaultConfigurationInheritanceStrategyTest method testWithPropertyMerging.
@Test
public void testWithPropertyMerging() {
underTest = context.registerInjectActivateService(new DefaultConfigurationInheritanceStrategy(), "configPropertyInheritancePropertyNames", PROPERTY_CONFIG_PROPERTY_INHERIT_CUSTOM);
Iterator<Resource> resources = ImmutableList.of(context.create().resource("/conf/resource1", "prop1", "value1a", "prop2", "value2a", PROPERTY_CONFIG_PROPERTY_INHERIT, true), context.create().resource("/conf/resource2", "prop2", "value2b", "prop3", "value3b", PROPERTY_CONFIG_PROPERTY_INHERIT_CUSTOM, true), context.create().resource("/conf/resource3", "prop4", "value4b")).iterator();
Resource inherited = underTest.getResource(resources);
ValueMap props = inherited.getValueMap();
assertEquals("value1a", props.get("prop1", String.class));
assertEquals("value2a", props.get("prop2", String.class));
assertEquals("value3b", props.get("prop3", String.class));
assertEquals("value4b", props.get("prop4", String.class));
}
use of org.apache.sling.api.resource.ValueMap in project sling by apache.
the class DefaultConfigurationInheritanceStrategyTest method testWithoutPropertyMerging.
@Test
public void testWithoutPropertyMerging() {
underTest = context.registerInjectActivateService(new DefaultConfigurationInheritanceStrategy());
Iterator<Resource> resources = ImmutableList.of(context.create().resource("/conf/resource1", "prop1", "value1a", "prop2", "value2a"), context.create().resource("/conf/resource2", "prop2", "value2b", "prop3", "value3b"), context.create().resource("/conf/resource3", "prop4", "value4b")).iterator();
Resource inherited = underTest.getResource(resources);
ValueMap props = inherited.getValueMap();
assertEquals("value1a", props.get("prop1", String.class));
assertEquals("value2a", props.get("prop2", String.class));
assertNull(props.get("prop3", String.class));
assertNull(props.get("prop4", String.class));
}
use of org.apache.sling.api.resource.ValueMap 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.api.resource.ValueMap in project sling by apache.
the class DistributedEventSender method readEvent.
/**
* Read an event from the resource
* @return The event object or <code>null</code>
*/
private Event readEvent(final Resource eventResource) {
try {
final ValueMap vm = ResourceHelper.getValueMap(eventResource);
final String topic = vm.get(EventConstants.EVENT_TOPIC, String.class);
if (topic == null) {
// no topic should never happen as we check the resource type before
logger.error("Unable to read distributed event from " + eventResource.getPath() + " : no topic property available.");
} else {
final Map<String, Object> properties = ResourceHelper.cloneValueMap(vm);
// only send event if there are no read errors, otherwise discard it
@SuppressWarnings("unchecked") final List<Exception> readErrorList = (List<Exception>) properties.remove(ResourceHelper.PROPERTY_MARKER_READ_ERROR_LIST);
if (readErrorList == null) {
properties.remove(EventConstants.EVENT_TOPIC);
properties.remove(DEAConstants.PROPERTY_DISTRIBUTE);
final Object oldRT = properties.remove("event.dea." + ResourceResolver.PROPERTY_RESOURCE_TYPE);
if (oldRT != null) {
properties.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, oldRT);
} else {
properties.remove(ResourceResolver.PROPERTY_RESOURCE_TYPE);
}
try {
final Event event = new Event(topic, properties);
return event;
} catch (final IllegalArgumentException iae) {
// this exception occurs if the topic is not correct (it should never happen,
// but you never know)
logger.error("Unable to read event: " + iae.getMessage(), iae);
}
} else {
for (final Exception e : readErrorList) {
logger.warn("Unable to read distributed event from " + eventResource.getPath(), e);
}
}
}
} catch (final InstantiationException ie) {
// something happened with the resource in the meantime
this.ignoreException(ie);
}
return null;
}
Aggregations