Search in sources :

Example 76 with ModifiableValueMap

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

the class ValidationServiceImplTest method testResourceWithMissingGrandChildProperty.

@Test
public void testResourceWithMissingGrandChildProperty() throws Exception {
    // accept any digits
    propertyBuilder.validator(REGEX_VALIDATOR_ID, 0, RegexValidator.REGEX_PARAM, "\\d");
    ResourceProperty property = propertyBuilder.build("field1");
    modelBuilder.resourceProperty(property);
    ChildResource modelGrandChild = new ChildResourceImpl("grandchild", null, true, Collections.singletonList(property), Collections.<ChildResource>emptyList());
    ChildResource modelChild = new ChildResourceImpl("child", null, true, Collections.singletonList(property), Collections.singletonList(modelGrandChild));
    modelBuilder.childResource(modelChild);
    ValidationModel vm = modelBuilder.build("sometype", "some source");
    // create a resource
    ResourceResolver rr = context.resourceResolver();
    Resource testResource = ResourceUtil.getOrCreateResource(rr, "/content/validation/1/resource", JcrConstants.NT_UNSTRUCTURED, JcrConstants.NT_UNSTRUCTURED, true);
    ModifiableValueMap mvm = testResource.adaptTo(ModifiableValueMap.class);
    mvm.put("field1", "1");
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("field1", "1");
    Resource resourceChild = rr.create(testResource, "child", properties);
    // resourceGrandChild is missing the mandatory field1 property
    rr.create(resourceChild, "grandchild", null);
    ValidationResult vr = validationService.validate(testResource, vm);
    Assert.assertFalse("resource should have been considered invalid", vr.isValid());
    Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(new DefaultValidationFailure("child/grandchild", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME, "field1")));
}
Also used : HashMap(java.util.HashMap) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) ChildResource(org.apache.sling.validation.model.ChildResource) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) ResourceProperty(org.apache.sling.validation.model.ResourceProperty) ValidationModel(org.apache.sling.validation.model.ValidationModel) ChildResourceImpl(org.apache.sling.validation.impl.model.ChildResourceImpl) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) DefaultValidationFailure(org.apache.sling.validation.spi.support.DefaultValidationFailure) Test(org.junit.Test)

Example 77 with ModifiableValueMap

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

the class ResourceValidationModelProviderImplTest method testGetValidationModelsWithInvalidValidatorArguments2.

@Test(expected = IllegalStateException.class)
public void testGetValidationModelsWithInvalidValidatorArguments2() throws Exception {
    // create a model with neither children nor properties
    ValidationModel model1 = modelBuilder.build("sling/validation/test", libsValidatorsRoot.getPath() + "/testValidationModel1");
    // create valid model first
    Resource modelResource = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", model1);
    // and make parametrization of validator invalid afterwards
    Resource validatorResource = modelResource.getChild("properties/field1/validators/validatorId");
    ModifiableValueMap validatorArguments = validatorResource.adaptTo(ModifiableValueMap.class);
    // starting with "="
    validatorArguments.put("validatorArguments", "=value2");
    Collection<ValidationModel> models = modelProvider.getValidationModels("sling/validation/test");
    Assert.assertThat(models, Matchers.contains(model1));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Example 78 with ModifiableValueMap

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

the class ResourceValidationModelProviderImplTest method createValidationModelChildResource.

private Resource createValidationModelChildResource(Resource parentResource, ChildResource child) throws PersistenceException {
    ResourceResolver rr = parentResource.getResourceResolver();
    Resource modelChildren = rr.create(parentResource, ResourceValidationModelProviderImpl.CHILDREN, primaryTypeUnstructuredMap);
    Resource modelResource = rr.create(modelChildren, child.getName(), primaryTypeUnstructuredMap);
    ModifiableValueMap mvm = modelResource.adaptTo(ModifiableValueMap.class);
    if (child.getNamePattern() != null) {
        mvm.put(ResourceValidationModelProviderImpl.NAME_REGEX, child.getNamePattern());
    }
    mvm.put(ResourceValidationModelProviderImpl.OPTIONAL, !child.isRequired());
    createValidationModelProperties(modelResource, child.getProperties());
    // recursion for all childs
    for (ChildResource grandChild : child.getChildren()) {
        createValidationModelChildResource(modelResource, grandChild);
    }
    return modelResource;
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) ChildResource(org.apache.sling.validation.model.ChildResource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 79 with ModifiableValueMap

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

the class ResourceConfigurationManager method saveConfig.

@Override
public void saveConfig(ResourceResolver resolver, DistributionConfiguration config) {
    Resource configRoot = resolver.getResource(configRootPath);
    if (configRoot == null) {
        return;
    }
    Resource configResource = configRoot.getChild(config.getName());
    Resource contentResource = (configResource != null) ? configResource.getChild(CONTENT_NODE) : null;
    try {
        if (configResource == null) {
            Map<String, Object> configMap = new HashMap<String, Object>();
            configMap.put("jcr:primaryType", "sling:Folder");
            configResource = resolver.create(configRoot, config.getName(), configMap);
        }
        if (contentResource == null) {
            Map<String, Object> contentMap = new HashMap<String, Object>();
            contentMap.put("jcr:primaryType", "nt:unstructured");
            contentResource = resolver.create(configResource, CONTENT_NODE, contentMap);
        }
        Map<String, Object> properties = config.getProperties();
        properties = filterMap(properties);
        for (Map.Entry<String, String> propDefault : configDefaults.entrySet()) {
            if (!properties.containsKey(propDefault.getKey())) {
                properties.put(propDefault.getKey(), propDefault.getValue());
            }
        }
        ModifiableValueMap valueMap = contentResource.adaptTo(ModifiableValueMap.class);
        valueMap.putAll(properties);
    } catch (PersistenceException e) {
        log.error("cannot save config {}", config.getName(), e);
    }
}
Also used : HashMap(java.util.HashMap) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Map(java.util.Map) HashMap(java.util.HashMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 80 with ModifiableValueMap

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

the class MessageStoreImplAttachmentsTest method assertSaveMessageWithAttachments.

private void assertSaveMessageWithAttachments(Message msg, int num) throws IOException {
    store.save(msg);
    List<BodyPart> attList = new LinkedList<BodyPart>();
    MessageStoreImpl.recursiveMultipartProcessing((Multipart) msg.getBody(), new StringBuilder(), new StringBuilder(), false, attList);
    @SuppressWarnings("unchecked") Queue<BodyPart> attachmentsMsg = (Queue<BodyPart>) attList;
    assertTrue("No attachments found", attachmentsMsg.size() > 0);
    assertEquals("", num, attachmentsMsg.size());
    final Resource r = resolver.getResource(getResourcePath(msg, store));
    assertNotNull("Expecting non-null Resource", r);
    for (Resource aRes : r.getChildren()) {
        final ModifiableValueMap aMap = aRes.adaptTo(ModifiableValueMap.class);
        BodyPart aMsg = attachmentsMsg.poll();
        assertNotNull("JCR contains more attachments", aMsg);
        for (Field f : aMsg.getHeader().getFields()) {
            String name = f.getName();
            assertEquals("Field " + name + " is different", (aMap.get(name, String.class)), f.getBody());
        }
        if (aMsg.getBody() instanceof TextBody) {
            assertEquals("Content is not the same", MessageStoreImpl.getTextPart(aMsg), aMap.get(CONTENT, String.class));
        } else if (aMsg.getBody() instanceof BinaryBody) {
            assertEquals("Content is not the same", getBinPart(aMsg), aMap.get(CONTENT, String.class));
        } else {
            fail("Unknown type of attachment body");
        }
    }
    assertEquals("Message contains more attachments", attachmentsMsg.poll(), null);
}
Also used : BodyPart(org.apache.james.mime4j.message.BodyPart) Resource(org.apache.sling.api.resource.Resource) BinaryBody(org.apache.james.mime4j.dom.BinaryBody) LinkedList(java.util.LinkedList) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) TextBody(org.apache.james.mime4j.dom.TextBody) RawField(org.apache.james.mime4j.stream.RawField) Field(org.apache.james.mime4j.stream.Field) Queue(java.util.Queue)

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