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