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