Search in sources :

Example 21 with ValidationResult

use of org.apache.sling.validation.ValidationResult 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 22 with ValidationResult

use of org.apache.sling.validation.ValidationResult 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 23 with ValidationResult

use of org.apache.sling.validation.ValidationResult in project sling by apache.

the class ValidationServiceImplTest method testResourceWithMissingOptionalChildResource.

@Test
public void testResourceWithMissingOptionalChildResource() throws Exception {
    // accept any digits
    propertyBuilder.validator(REGEX_VALIDATOR_ID, 0, RegexValidator.REGEX_PARAM, "\\d");
    ResourceProperty property = propertyBuilder.build("field1");
    ChildResource child = new ChildResourceImpl("child", null, false, Collections.singletonList(property), Collections.<ChildResource>emptyList());
    modelBuilder.childResource(child);
    ValidationModel vm = modelBuilder.build("type", "some source");
    // create a resource (lacking the optional "child" sub resource)
    ResourceResolver rr = context.resourceResolver();
    Resource testResource = ResourceUtil.getOrCreateResource(rr, "/content/validation/1/resource", JcrConstants.NT_UNSTRUCTURED, JcrConstants.NT_UNSTRUCTURED, true);
    ValidationResult vr = validationService.validate(testResource, vm);
    Assert.assertThat(vr.getFailures(), Matchers.hasSize(0));
    Assert.assertTrue(vr.isValid());
}
Also used : 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) 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) Test(org.junit.Test)

Example 24 with ValidationResult

use of org.apache.sling.validation.ValidationResult in project sling by apache.

the class ValidationServiceImplTest method testResourceWithValidatorLeveragingTheResource.

@Test
public void testResourceWithValidatorLeveragingTheResource() throws Exception {
    Validator<String> extendedValidator = new Validator<String>() {

        @Override
        @Nonnull
        public ValidationResult validate(@Nonnull String data, @Nonnull ValidatorContext context, @Nonnull ValueMap arguments) throws SlingValidationException {
            Resource resource = context.getResource();
            if (resource == null) {
                Assert.fail("Resource must not be null");
            } else {
                Assert.assertThat(resource.getPath(), Matchers.equalTo("/content/validation/1/resource"));
            }
            return DefaultValidationResult.VALID;
        }
    };
    // register validator
    validationService.validatorMap.put("myid", extendedValidator, newValidatorServiceReference, null);
    // accept any digits
    propertyBuilder.validator("myid", null);
    modelBuilder.resourceProperty(propertyBuilder.build("field1"));
    ValidationModel vm = modelBuilder.build("sometype", "some source");
    // create a resource
    ResourceResolver rr = context.resourceResolver();
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("field1", "1");
    Resource testResource = ResourceUtil.getOrCreateResource(rr, "/content/validation/1/resource", properties, JcrConstants.NT_UNSTRUCTURED, true);
    ValidationResult vr = validationService.validate(testResource, vm);
    Assert.assertTrue(vr.isValid());
}
Also used : Nonnull(javax.annotation.Nonnull) HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) 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) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) ValidationModel(org.apache.sling.validation.model.ValidationModel) ValidatorContext(org.apache.sling.validation.spi.ValidatorContext) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) 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 25 with ValidationResult

use of org.apache.sling.validation.ValidationResult 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)

Aggregations

ValidationResult (org.apache.sling.validation.ValidationResult)25 ValidationModel (org.apache.sling.validation.model.ValidationModel)22 Test (org.junit.Test)19 DefaultValidationResult (org.apache.sling.validation.spi.support.DefaultValidationResult)18 Resource (org.apache.sling.api.resource.Resource)14 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)13 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)12 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)12 ChildResource (org.apache.sling.validation.model.ChildResource)12 HashMap (java.util.HashMap)11 DefaultValidationFailure (org.apache.sling.validation.spi.support.DefaultValidationFailure)11 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)8 ChildResourceImpl (org.apache.sling.validation.impl.model.ChildResourceImpl)6 ResourceProperty (org.apache.sling.validation.model.ResourceProperty)6 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)5 Nonnull (javax.annotation.Nonnull)3 ValueMap (org.apache.sling.api.resource.ValueMap)3 Validator (org.apache.sling.validation.spi.Validator)3 ValidatorContext (org.apache.sling.validation.spi.ValidatorContext)3 LinkedHashMap (java.util.LinkedHashMap)2