use of org.apache.sling.testing.mock.jcr.MockQuery in project sling by apache.
the class ResourceValidationModelProviderImplTest method setUp.
@Before
public void setUp() throws LoginException, PersistenceException, RepositoryException {
primaryTypeUnstructuredMap = new HashMap<String, Object>();
primaryTypeUnstructuredMap.put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
modelProvider = new ResourceValidationModelProviderImpl();
// one default model
modelBuilder = new ValidationModelBuilder();
modelBuilder.setApplicablePath("/content/site1");
ResourcePropertyBuilder propertyBuilder = new ResourcePropertyBuilder();
propertyBuilder.validator("validatorId", 10, RegexValidator.REGEX_PARAM, "prefix.*");
ResourceProperty property = propertyBuilder.build("field1");
modelBuilder.resourceProperty(property);
prefixBasedResultHandler = new MockQueryResultHandler() {
@Override
public MockQueryResult executeQuery(MockQuery query) {
if (!"xpath".equals(query.getLanguage())) {
return null;
}
String statement = query.getStatement();
// @validatingResourceType="<some-resource-type>"]
if (statement.startsWith("/jcr:root/")) {
statement = statement.substring("/jcr:root/".length() - 1);
}
// extract the prefix from the statement
String prefix = Text.getAbsoluteParent(statement, 0);
// extract the resource type from the statement
Matcher matcher = RESOURCE_TYPE_PATTERN.matcher(statement);
if (!matcher.matches()) {
throw new IllegalArgumentException("Can only process query statements which contain a validatedResourceType but the statement is: " + statement);
}
String resourceType = matcher.group(1);
PrefixAndResourceType prefixAndResourceType = new PrefixAndResourceType(prefix, resourceType);
if (validatorModelNodesPerPrefixAndResourceType.keySet().contains(prefixAndResourceType)) {
return new MockQueryResult(validatorModelNodesPerPrefixAndResourceType.get(prefixAndResourceType));
}
return null;
}
};
rr = context.resourceResolver();
modelProvider.rrf = resourceResolverFactory;
// create a wrapper resource resolver, which cannot be closed (as the SlingContext will take care of that)
ResourceResolver nonClosableResourceResolverWrapper = Mockito.spy(rr);
// intercept all close calls
Mockito.doNothing().when(nonClosableResourceResolverWrapper).close();
// always use the context's resource resolver (because we never commit)
Mockito.when(resourceResolverFactory.getServiceResourceResolver(Mockito.anyObject())).thenReturn(nonClosableResourceResolverWrapper);
MockJcr.addQueryResultHandler(rr.adaptTo(Session.class), prefixBasedResultHandler);
validatorModelNodesPerPrefixAndResourceType = new HashMap<PrefixAndResourceType, List<Node>>();
appsValidatorsRoot = ResourceUtil.getOrCreateResource(rr, APPS + "/" + VALIDATION_MODELS_RELATIVE_PATH, (Map<String, Object>) null, "sling:Folder", true);
libsValidatorsRoot = ResourceUtil.getOrCreateResource(rr, LIBS + "/" + VALIDATION_MODELS_RELATIVE_PATH, (Map<String, Object>) null, "sling:Folder", true);
}
Aggregations