Search in sources :

Example 16 with ValueMapDecorator

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

the class ResourceModelClassesTest method testArrayPrimitivesModel.

@Test
public void testArrayPrimitivesModel() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("intArray", new int[] { 1, 2, 9, 8 });
    map.put("secondIntArray", new Integer[] { 1, 2, 9, 8 });
    ValueMap vm = new ValueMapDecorator(map);
    Resource res = mock(Resource.class);
    when(res.adaptTo(ValueMap.class)).thenReturn(vm);
    ArrayPrimitivesModel model = factory.getAdapter(res, ArrayPrimitivesModel.class);
    assertNotNull(model);
    int[] primitiveIntArray = model.getIntArray();
    assertEquals(4, primitiveIntArray.length);
    assertEquals(2, primitiveIntArray[1]);
    int[] secondPrimitiveIntArray = model.getSecondIntArray();
    assertEquals(4, secondPrimitiveIntArray.length);
    assertEquals(2, secondPrimitiveIntArray[1]);
}
Also used : ArrayPrimitivesModel(org.apache.sling.models.testmodels.classes.ArrayPrimitivesModel) HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 17 with ValueMapDecorator

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

the class ValidationServiceImplTest method testValidateNeverCalledWithNullValues.

@Test
public void testValidateNeverCalledWithNullValues() throws Exception {
    Validator<String> myValidator = new Validator<String>() {

        @Override
        @Nonnull
        public ValidationResult validate(@Nonnull String data, @Nonnull ValidatorContext context, @Nonnull ValueMap arguments) throws SlingValidationException {
            Assert.assertNotNull("data parameter for validate should never be null", data);
            Assert.assertNotNull("location of context parameter for validate should never be null", context.getLocation());
            Assert.assertNotNull("valueMap of context parameter for validate should never be null", context.getValueMap());
            Assert.assertNull("resource of context parameter for validate cannot be set if validate was called only with a value map", context.getResource());
            Assert.assertNotNull("arguments parameter for validate should never be null", arguments);
            return DefaultValidationResult.VALID;
        }
    };
    validationService.validatorMap.put("someId", myValidator, validatorServiceReference, 10);
    propertyBuilder.validator("someId", 20);
    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", "1");
    ValidationResult vr = validationService.validate(new ValueMapDecorator(hashMap), vm);
    Assert.assertThat(vr.getFailures(), Matchers.hasSize(0));
    Assert.assertTrue(vr.isValid());
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) Nonnull(javax.annotation.Nonnull) ValidatorContext(org.apache.sling.validation.spi.ValidatorContext) HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) DateValidator(org.apache.sling.validation.impl.util.examplevalidators.DateValidator) RegexValidator(org.apache.sling.validation.impl.validators.RegexValidator) Validator(org.apache.sling.validation.spi.Validator) Test(org.junit.Test)

Example 18 with ValueMapDecorator

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

the class ValidationServiceImplTest method testValidateWithInvalidValidatorId.

@Test(expected = IllegalStateException.class)
public void testValidateWithInvalidValidatorId() throws Exception {
    propertyBuilder.validator("invalidid", 10);
    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", "1");
    validationService.validate(new ValueMapDecorator(hashMap), vm);
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) HashMap(java.util.HashMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Test(org.junit.Test)

Example 19 with ValueMapDecorator

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

the class ValidationServiceImplTest method testValueMapWithMissingOptionalValue.

@Test()
public void testValueMapWithMissingOptionalValue() throws Exception {
    modelBuilder.resourceProperty(propertyBuilder.optional().build("field1"));
    ValidationModel vm = modelBuilder.build("sling/validation/test", "some source");
    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    hashMap.put("field2", "1");
    ValidationResult vr = validationService.validate(new ValueMapDecorator(hashMap), vm);
    Assert.assertThat(vr.getFailures(), Matchers.hasSize(0));
    Assert.assertTrue(vr.isValid());
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) HashMap(java.util.HashMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) Test(org.junit.Test)

Example 20 with ValueMapDecorator

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

the class ValidationServiceImplTest method testValueMapWithCorrectDataType.

@Test
public void testValueMapWithCorrectDataType() throws Exception {
    propertyBuilder.validator(REGEX_VALIDATOR_ID, 0, RegexValidator.REGEX_PARAM, "abc");
    modelBuilder.resourceProperty(propertyBuilder.build("field1"));
    propertyBuilder = new ResourcePropertyBuilder();
    final String TEST_REGEX = "^test$";
    propertyBuilder.validator(REGEX_VALIDATOR_ID, 0, RegexValidator.REGEX_PARAM, TEST_REGEX);
    modelBuilder.resourceProperty(propertyBuilder.build("field2"));
    ValidationModel vm = modelBuilder.build("sling/validation/test", "some source");
    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    hashMap.put("field1", "HelloWorld");
    hashMap.put("field2", "HelloWorld");
    ValidationResult vr = validationService.validate(new ValueMapDecorator(hashMap), vm);
    Assert.assertFalse(vr.isValid());
    Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>hasItem(new DefaultValidationFailure("field2", 0, defaultResourceBundle, RegexValidator.I18N_KEY_PATTERN_DOES_NOT_MATCH, TEST_REGEX)));
}
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) ResourcePropertyBuilder(org.apache.sling.validation.impl.model.ResourcePropertyBuilder) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) Test(org.junit.Test)

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