Search in sources :

Example 11 with ValueMap

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));
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Test(org.junit.Test)

Example 12 with ValueMap

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));
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 13 with ValueMap

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));
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 14 with ValueMap

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"));
}
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 15 with ValueMap

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;
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Event(org.osgi.service.event.Event) List(java.util.List) LoginException(org.apache.sling.api.resource.LoginException)

Aggregations

ValueMap (org.apache.sling.api.resource.ValueMap)278 Resource (org.apache.sling.api.resource.Resource)205 Test (org.junit.Test)160 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)59 HashMap (java.util.HashMap)54 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)44 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)26 Map (java.util.Map)21 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)21 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)20 RewriterResponse (org.apache.sling.security.impl.ContentDispositionFilter.RewriterResponse)20 Expectations (org.jmock.Expectations)20 ArrayList (java.util.ArrayList)18 PersistenceException (org.apache.sling.api.resource.PersistenceException)17 Calendar (java.util.Calendar)16 Date (java.util.Date)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)12 InputStream (java.io.InputStream)11 Node (javax.jcr.Node)11