Search in sources :

Example 1 with MockQueryResult

use of org.apache.sling.testing.mock.jcr.MockQueryResult 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);
}
Also used : Matcher(java.util.regex.Matcher) ResourcePropertyBuilder(org.apache.sling.validation.impl.model.ResourcePropertyBuilder) MockQueryResultHandler(org.apache.sling.testing.mock.jcr.MockQueryResultHandler) ResourceProperty(org.apache.sling.validation.model.ResourceProperty) MockQueryResult(org.apache.sling.testing.mock.jcr.MockQueryResult) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ArrayList(java.util.ArrayList) List(java.util.List) MockQuery(org.apache.sling.testing.mock.jcr.MockQuery) ValueMap(org.apache.sling.api.resource.ValueMap) HashMap(java.util.HashMap) Map(java.util.Map) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) ValidationModelBuilder(org.apache.sling.validation.impl.model.ValidationModelBuilder) Session(javax.jcr.Session) Before(org.junit.Before)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Session (javax.jcr.Session)1 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 ValueMap (org.apache.sling.api.resource.ValueMap)1 MockQuery (org.apache.sling.testing.mock.jcr.MockQuery)1 MockQueryResult (org.apache.sling.testing.mock.jcr.MockQueryResult)1 MockQueryResultHandler (org.apache.sling.testing.mock.jcr.MockQueryResultHandler)1 ResourcePropertyBuilder (org.apache.sling.validation.impl.model.ResourcePropertyBuilder)1 ValidationModelBuilder (org.apache.sling.validation.impl.model.ValidationModelBuilder)1 ResourceProperty (org.apache.sling.validation.model.ResourceProperty)1 Before (org.junit.Before)1