use of org.apache.sling.validation.model.ValidationModel in project sling by apache.
the class ValidationServiceImplTest method testResourceWithNestedChildren.
@Test
public void testResourceWithNestedChildren() throws Exception {
// accept any digits
propertyBuilder.validator(REGEX_VALIDATOR_ID, 0, RegexValidator.REGEX_PARAM, "\\d");
ResourceProperty property = propertyBuilder.build("field1");
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);
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("field1", "1");
Resource resourceChild = rr.create(testResource, "child", properties);
rr.create(resourceChild, "grandchild", properties);
ValidationResult vr = validationService.validate(testResource, vm);
Assert.assertThat(vr.getFailures(), Matchers.hasSize(0));
Assert.assertTrue(vr.isValid());
}
use of org.apache.sling.validation.model.ValidationModel in project sling by apache.
the class ResourceValidationModelProviderImplTest method testGetValidationModelsWithChildren.
@Test
public void testGetValidationModelsWithChildren() throws Exception {
// build two models manually (which are identical except for the applicable path)
ResourcePropertyBuilder resourcePropertyBuilder = new ResourcePropertyBuilder();
resourcePropertyBuilder.multiple();
resourcePropertyBuilder.optional();
ResourceProperty childproperty = resourcePropertyBuilder.build("child1property");
modelBuilder.childResource(new ChildResourceImpl("child1", null, true, Collections.singletonList(childproperty), Collections.<ChildResource>emptyList()));
ValidationModel model1 = modelBuilder.build("sling/validation/test", libsValidatorsRoot.getPath() + "/testValidationModel1");
// build models in JCR
createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", model1);
// compare both models
Collection<ValidationModel> models = modelProvider.getValidationModels("sling/validation/test");
Assert.assertThat(models, Matchers.contains(model1));
}
use of org.apache.sling.validation.model.ValidationModel in project sling by apache.
the class ResourceValidationModelProviderImplTest method testGetValidationModelsWithMissingChildrenAndProperties.
@Test(expected = IllegalStateException.class)
public void testGetValidationModelsWithMissingChildrenAndProperties() throws Exception {
// create a model with properties (otherwise build() will already throw an exception)
modelBuilder = new ValidationModelBuilder();
modelBuilder.resourceProperty(new ResourcePropertyBuilder().build("field1"));
modelBuilder.addApplicablePath("content/site1");
ValidationModel model1 = modelBuilder.build("sling/validation/test", libsValidatorsRoot.getPath() + "/testValidationModel1");
Resource resource = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", model1);
// make created model invalid by removing the properties sub resource
rr.delete(resource.getChild("properties"));
modelProvider.getValidationModels("sling/validation/test");
}
use of org.apache.sling.validation.model.ValidationModel in project sling by apache.
the class ResourceValidationModelProviderImplTest method testGetValidationModelsWithOverlay.
@Test
public void testGetValidationModelsWithOverlay() throws Exception {
// create two models manually (which are identical except for the applicable path)
ValidationModel model1 = modelBuilder.build("sling/validation/test", libsValidatorsRoot.getPath() + "/testValidationModel1");
modelBuilder.setApplicablePath("/content/site2");
ValidationModel model2 = modelBuilder.build("sling/validation/test", appsValidatorsRoot.getPath() + "/testValidationModel1");
// create two models: one in libs and one in apps (distinguishable via applicablePath)
createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", model1);
createValidationModelResource(rr, appsValidatorsRoot.getPath(), "testValidationModel1", model2);
// only the apps model should be returned
Collection<ValidationModel> models = modelProvider.getValidationModels("sling/validation/test");
Assert.assertThat(models, Matchers.contains(model2));
}
use of org.apache.sling.validation.model.ValidationModel in project sling by apache.
the class ResourceValidationModelProviderImplTest method testCachingOfGetValidationModels.
@Test
public void testCachingOfGetValidationModels() throws Exception {
// build one model
ResourcePropertyBuilder resourcePropertyBuilder = new ResourcePropertyBuilder();
ValidationModel model1 = modelBuilder.resourceProperty(resourcePropertyBuilder.build("property1")).build("sling/validation/test", libsValidatorsRoot.getPath() + "/testValidationModel1");
modelBuilder.setApplicablePath("/content/site2");
// build models in JCR
createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", model1);
// check that both models are returned
Collection<ValidationModel> models = modelProvider.getValidationModels("sling/validation/test");
Assert.assertThat(models, Matchers.containsInAnyOrder(model1));
// the 2nd time the same instance should be returned
Collection<ValidationModel> models2 = modelProvider.getValidationModels("sling/validation/test");
Assert.assertEquals("Due to caching both models should be actually the same instance", System.identityHashCode(models), System.identityHashCode(models2));
}
Aggregations