use of org.apache.sling.validation.impl.model.ValidationModelBuilder in project sling by apache.
the class ValidationServiceImplTest method setUp.
@Before
public void setUp() throws LoginException, PersistenceException, RepositoryException {
validationService = new ValidationServiceImpl();
validationService.searchPaths = Arrays.asList(context.resourceResolver().getSearchPath());
validationService.configuration = configuration;
Mockito.doReturn(20).when(configuration).defaultSeverity();
validationService.resourceBundleProviders = Collections.singletonList(resourceBundleProvider);
Mockito.doReturn(defaultResourceBundle).when(resourceBundleProvider).getResourceBundle(Mockito.anyObject());
modelBuilder = new ValidationModelBuilder();
propertyBuilder = new ResourcePropertyBuilder();
dateValidator = new DateValidator();
Mockito.doReturn(1l).when(providingBundle).getBundleId();
Mockito.doReturn(providingBundle).when(validatorServiceReference).getBundle();
Mockito.doReturn(providingBundle).when(newValidatorServiceReference).getBundle();
validationService.validatorMap.put(DATE_VALIDATOR_ID, dateValidator, validatorServiceReference, 10);
validationService.validatorMap.put(REGEX_VALIDATOR_ID, new RegexValidator(), validatorServiceReference, 10);
validationService.modelRetriever = modelRetriever;
}
use of org.apache.sling.validation.impl.model.ValidationModelBuilder 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.impl.model.ValidationModelBuilder in project sling by apache.
the class ResourceValidationModelProviderImpl method doGetModels.
/** Searches for validation models bound to a specific resource type through a search query.
*
* @param relativeResourceType the resource type to look for
* @return a List of {@link ValidationModel}s. Never {@code null}, but might be empty collection in case no model for the given resource
* type could be found. Returns the models below "/apps" before the models below "/libs".
* @throws IllegalStateException in case a validation model is found but it is invalid */
@Nonnull
private List<ValidationModel> doGetModels(@Nonnull String relativeResourceType) {
List<ValidationModel> validationModels = new ArrayList<ValidationModel>();
ResourceResolver resourceResolver = null;
try {
resourceResolver = rrf.getServiceResourceResolver(null);
String[] searchPaths = resourceResolver.getSearchPath();
for (String searchPath : searchPaths) {
final String queryString = String.format(MODEL_XPATH_QUERY, searchPath, relativeResourceType);
LOG.debug("Looking for validation models with query '{}'", queryString);
Iterator<Resource> models = resourceResolver.findResources(queryString, "xpath");
while (models.hasNext()) {
Resource model = models.next();
LOG.debug("Found validation model resource {}.", model.getPath());
String resourcePath = model.getPath();
try {
ValidationModelBuilder modelBuilder = new ValidationModelBuilder();
ValueMap validationModelProperties = model.getValueMap();
modelBuilder.addApplicablePaths(validationModelProperties.get(ResourceValidationModelProviderImpl.APPLICABLE_PATHS, new String[] {}));
Resource propertiesResource = model.getChild(ResourceValidationModelProviderImpl.PROPERTIES);
modelBuilder.resourceProperties(buildProperties(propertiesResource));
modelBuilder.childResources(buildChildren(model, model));
ValidationModel vm = modelBuilder.build(relativeResourceType, resourcePath);
validationModels.add(vm);
} catch (IllegalArgumentException e) {
throw new IllegalStateException("Found invalid validation model in '" + resourcePath + "': " + e.getMessage(), e);
}
}
if (!validationModels.isEmpty()) {
// the applicable content paths do not matter here!
break;
}
}
return validationModels;
} catch (LoginException e) {
throw new IllegalStateException("Could not get service resource resolver", e);
} finally {
if (resourceResolver != null) {
resourceResolver.close();
}
}
}
Aggregations