Search in sources :

Example 6 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.

the class ResourceAccessSecurityImplTests method testCannotUpdateUsingReadableResourceIfCannotRead.

@Test
public void testCannotUpdateUsingReadableResourceIfCannotRead() {
    initMocks("/content", new String[] { "read", "update" });
    Resource resource = mock(Resource.class);
    when(resource.getPath()).thenReturn("/content");
    ModifiableValueMap valueMap = mock(ModifiableValueMap.class);
    when(resource.adaptTo(ModifiableValueMap.class)).thenReturn(valueMap);
    when(resourceAccessGate.canRead(resource)).thenReturn(ResourceAccessGate.GateResult.DENIED);
    when(resourceAccessGate.canUpdate(resource)).thenReturn(ResourceAccessGate.GateResult.GRANTED);
    Resource readableResource = resourceAccessSecurity.getReadableResource(resource);
    assertNull(readableResource);
}
Also used : Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Example 7 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.

the class JcrModifiableValueMapTest method testSerializable.

public void testSerializable() throws Exception {
    this.rootNode.getSession().refresh(false);
    final ModifiableValueMap pvm = new JcrModifiableValueMap(this.rootNode, getHelperData());
    assertContains(pvm, initialSet());
    assertNull(pvm.get("something"));
    // now put a serializable object
    final List<String> strings = new ArrayList<String>();
    strings.add("a");
    strings.add("b");
    pvm.put("something", strings);
    // check if we get the list again
    @SuppressWarnings("unchecked") final List<String> strings2 = (List<String>) pvm.get("something");
    assertEquals(strings, strings2);
    getSession().save();
    final ModifiableValueMap pvm2 = new JcrModifiableValueMap(this.rootNode, getHelperData());
    // check if we get the list again
    @SuppressWarnings("unchecked") final List<String> strings3 = (List<String>) pvm2.get("something", Serializable.class);
    assertEquals(strings, strings3);
}
Also used : Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 8 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.

the class JcrModifiableValueMapTest method testMixins.

public void testMixins() throws Exception {
    this.rootNode.getSession().refresh(false);
    final Node testNode = this.rootNode.addNode("testMixins" + System.currentTimeMillis());
    testNode.getSession().save();
    final ModifiableValueMap pvm = new JcrModifiableValueMap(testNode, getHelperData());
    final String[] types = pvm.get("jcr:mixinTypes", String[].class);
    final Set<String> exNodeTypes = getMixinNodeTypes(testNode);
    assertEquals(exNodeTypes.size(), (types == null ? 0 : types.length));
    if (types != null) {
        for (final String name : types) {
            assertTrue(exNodeTypes.contains(name));
        }
        String[] newTypes = new String[types.length + 1];
        System.arraycopy(types, 0, newTypes, 0, types.length);
        newTypes[types.length] = "mix:referenceable";
        pvm.put("jcr:mixinTypes", newTypes);
    } else {
        pvm.put("jcr:mixinTypes", "mix:referenceable");
    }
    getSession().save();
    final Set<String> newNodeTypes = getMixinNodeTypes(testNode);
    assertEquals(newNodeTypes.size(), exNodeTypes.size() + 1);
}
Also used : Node(javax.jcr.Node) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 9 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.

the class JcrModifiableValueMapTest method testRemove.

public void testRemove() throws Exception {
    getSession().refresh(false);
    final ModifiableValueMap pvm = new JcrModifiableValueMap(this.rootNode, getHelperData());
    final String key = "removeMe";
    final Long longValue = 5L;
    pvm.put(key, longValue);
    final Object removedValue = pvm.remove(key);
    assertTrue(removedValue instanceof Long);
    assertTrue(removedValue == longValue);
    assertFalse(pvm.containsKey(key));
}
Also used : ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 10 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.

the class DefaultConfigurationPersistenceStrategy method replaceProperties.

private void replaceProperties(Resource resource, Map<String, Object> properties) {
    if (log.isTraceEnabled()) {
        log.trace("! Store properties for resource {}: {}", resource.getPath(), MapUtil.traceOutput(properties));
    }
    ModifiableValueMap modValueMap = resource.adaptTo(ModifiableValueMap.class);
    if (modValueMap == null) {
        throw new ConfigurationPersistenceAccessDeniedException("No write access: Unable to store configuration data to " + resource.getPath() + ".");
    }
    // remove all existing properties that are not filterd
    Set<String> propertyNamesToRemove = new HashSet<>(modValueMap.keySet());
    PropertiesFilterUtil.removeIgnoredProperties(propertyNamesToRemove, configurationManagementSettings);
    for (String propertyName : propertyNamesToRemove) {
        modValueMap.remove(propertyName);
    }
    modValueMap.putAll(properties);
}
Also used : ConfigurationPersistenceAccessDeniedException(org.apache.sling.caconfig.spi.ConfigurationPersistenceAccessDeniedException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) HashSet(java.util.HashSet)

Aggregations

ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)111 Resource (org.apache.sling.api.resource.Resource)74 PersistenceException (org.apache.sling.api.resource.PersistenceException)32 Test (org.junit.Test)28 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)26 HashMap (java.util.HashMap)22 ValueMap (org.apache.sling.api.resource.ValueMap)22 Calendar (java.util.Calendar)13 LoginException (org.apache.sling.api.resource.LoginException)9 ChildResource (org.apache.sling.validation.model.ChildResource)9 Date (java.util.Date)8 HashSet (java.util.HashSet)8 Map (java.util.Map)8 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 IOException (java.io.IOException)7 ValidationModel (org.apache.sling.validation.model.ValidationModel)7 InputStream (java.io.InputStream)6 Node (javax.jcr.Node)6 RepositoryException (javax.jcr.RepositoryException)6