Search in sources :

Example 56 with ValueMapDecorator

use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.

the class ValidationServiceImplTest method testValueMapWithEmptyOptionalValue.

@Test()
public void testValueMapWithEmptyOptionalValue() throws Exception {
    propertyBuilder.optional();
    propertyBuilder.validator(REGEX_VALIDATOR_ID, null, RegexValidator.REGEX_PARAM, "abc");
    modelBuilder.resourceProperty(propertyBuilder.build("field1"));
    ValidationModel vm = modelBuilder.build("sling/validation/test", "some source");
    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    hashMap.put("field1", "");
    ValidationResult vr = validationService.validate(new ValueMapDecorator(hashMap), vm);
    Assert.assertFalse(vr.isValid());
    Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(new DefaultValidationFailure("field1", 10, defaultResourceBundle, RegexValidator.I18N_KEY_PATTERN_DOES_NOT_MATCH, "abc")));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) HashMap(java.util.HashMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) DefaultValidationFailure(org.apache.sling.validation.spi.support.DefaultValidationFailure) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) Test(org.junit.Test)

Example 57 with ValueMapDecorator

use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.

the class ValidationServiceImplTest method testValueMapWithMissingField.

@Test()
public void testValueMapWithMissingField() throws Exception {
    modelBuilder.resourceProperty(propertyBuilder.build("field1"));
    modelBuilder.resourceProperty(propertyBuilder.build("field2"));
    modelBuilder.resourceProperty(propertyBuilder.build("field3"));
    modelBuilder.resourceProperty(propertyBuilder.build("field4"));
    ValidationModel vm = modelBuilder.build("sling/validation/test", "some source");
    // this should not be detected as missing property
    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    hashMap.put("field1", new String[] {});
    hashMap.put("field2", new String[] { "null" });
    hashMap.put("field3", "");
    ValidationResult vr = validationService.validate(new ValueMapDecorator(hashMap), vm);
    Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(new DefaultValidationFailure("", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME, "field4")));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) HashMap(java.util.HashMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) DefaultValidationFailure(org.apache.sling.validation.spi.support.DefaultValidationFailure) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) Test(org.junit.Test)

Example 58 with ValueMapDecorator

use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.

the class MergedResourceTest method testResourceTypeMixed.

@Test
public void testResourceTypeMixed() throws Exception {
    final ValueMap vm1 = new ValueMapDecorator(Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object) "vma"));
    final MockResource r1 = new MockResource("/a", vm1, null) {

        @Override
        public String getResourceType() {
            return "a";
        }
    };
    final ValueMap vm2 = new ValueMapDecorator(Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object) "vmb"));
    final MockResource r2 = new MockResource("/b", vm2, null) {

        @Override
        public String getResourceType() {
            return "b";
        }
    };
    final ValueMap vm3 = new ValueMapDecorator(Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object) "vmc"));
    final MockResource r3 = new MockResource("/c", vm3, null) {

        @Override
        public String getResourceType() {
            return "c";
        }
    };
    final List<Resource> resources = new ArrayList<Resource>();
    resources.add(r1);
    resources.add(r2);
    resources.add(r3);
    final List<ValueMap> valueMaps = new ArrayList<ValueMap>();
    valueMaps.add(vm1);
    valueMaps.add(vm2);
    valueMaps.add(vm3);
    final MergedResource mr1 = new MergedResource(null, "/a", "a", resources, valueMaps);
    assertEquals("c", mr1.getResourceType());
    assertEquals("vmc", mr1.getResourceSuperType());
    final MergedResource mr2 = new MergedResource(null, "/a", "a", resources, Collections.singletonList(vm2));
    assertEquals("c", mr2.getResourceType());
    assertEquals("vmb", mr2.getResourceSuperType());
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) MockResource(org.apache.sling.testing.resourceresolver.MockResource) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) MockResource(org.apache.sling.testing.resourceresolver.MockResource) Test(org.junit.Test)

Example 59 with ValueMapDecorator

use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.

the class MergedResourceTest method testToString.

@Test
public void testToString() throws Exception {
    final ValueMap vm = new ValueMapDecorator(Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object) "vm"));
    final Resource r = new MockResource("/innerResourcePath", vm, null) {

        @Override
        public String getResourceType() {
            return "innerResource";
        }
    };
    final Resource mr = new MergedResource(null, "/merged", "merged", Collections.singletonList(r), Collections.singletonList(vm));
    assertTrue(mr.toString().contains("/innerResourcePath"));
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) MockResource(org.apache.sling.testing.resourceresolver.MockResource) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Resource(org.apache.sling.api.resource.Resource) MockResource(org.apache.sling.testing.resourceresolver.MockResource) Test(org.junit.Test)

Example 60 with ValueMapDecorator

use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.

the class ConfigurationDataImpl method getValues.

@Override
public ValueMap getValues() {
    if (valuesCache == null) {
        Map<String, Object> props = new HashMap<>();
        if (writebackConfigurationResource != null) {
            props.putAll(ResourceUtil.getValueMap(writebackConfigurationResource));
        }
        PropertiesFilterUtil.removeIgnoredProperties(props, configurationManagementSettings);
        resolveNestedConfigs(props);
        valuesCache = new ValueMapDecorator(props);
    }
    return valuesCache;
}
Also used : HashMap(java.util.HashMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator)

Aggregations

ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)63 Test (org.junit.Test)45 ValueMap (org.apache.sling.api.resource.ValueMap)44 HashMap (java.util.HashMap)36 Resource (org.apache.sling.api.resource.Resource)36 ValidationModel (org.apache.sling.validation.model.ValidationModel)9 ValidationResult (org.apache.sling.validation.ValidationResult)8 DefaultValidationResult (org.apache.sling.validation.spi.support.DefaultValidationResult)6 ArrayList (java.util.ArrayList)3 RepositoryException (javax.jcr.RepositoryException)3 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)3 ResourceModelWithRequiredField (org.apache.sling.models.testmodels.classes.ResourceModelWithRequiredField)3 MockResource (org.apache.sling.testing.resourceresolver.MockResource)3 DefaultValidationFailure (org.apache.sling.validation.spi.support.DefaultValidationFailure)3 Calendar (java.util.Calendar)2 Collection (java.util.Collection)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 LinkedHashMap (java.util.LinkedHashMap)2