You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2022/07/13 12:04:57 UTC

[sling-org-apache-sling-testing-resourceresolver-mock] 03/03: resource provider impl

This is an automated email from the ASF dual-hosted git repository.

sseifert pushed a commit to branch experimental/resource-provider
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-resourceresolver-mock.git

commit fc72ebe5d0c8db415bf60cda41fca67b2950a6ac
Author: Stefan Seifert <st...@users.noreply.github.com>
AuthorDate: Wed Jul 13 13:53:08 2022 +0200

    resource provider impl
---
 pom.xml                                            |  12 +++
 .../resourceresolver/MockResourceProvider.java     | 119 +++++++++++++++++++++
 2 files changed, 131 insertions(+)

diff --git a/pom.xml b/pom.xml
index 1f2225b..b6e074c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -108,6 +108,18 @@
         </dependency>
 
         <!-- testing -->
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.testing.sling-mock.junit4</artifactId>
+            <version>3.3.2</version>
+            <scope>test</scope>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.apache.sling</groupId>
+                    <artifactId>org.apache.sling.testing.resourceresolver-mock</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceProvider.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceProvider.java
new file mode 100644
index 0000000..199fbbb
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceProvider.java
@@ -0,0 +1,119 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.testing.resourceresolver;
+
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.sling.api.resource.LoginException;
+import org.apache.sling.api.resource.PersistenceException;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.resource.ResourceUtil;
+import org.apache.sling.spi.resource.provider.ResolveContext;
+import org.apache.sling.spi.resource.provider.ResourceContext;
+import org.apache.sling.spi.resource.provider.ResourceProvider;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.osgi.service.event.EventAdmin;
+
+/**
+ * This is a wrapper around {@link MockResourceResolver} to act as resource provider.
+ */
+@Component(service = ResourceProvider.class, property = {
+        ResourceProvider.PROPERTY_NAME + "=MockResourceProvider",
+        ResourceProvider.PROPERTY_ROOT + "=/",
+        ResourceProvider.PROPERTY_MODIFIABLE + ":Boolean=true",
+        // although we do not really support authentication, it's required for a modifiable resource provider
+        ResourceProvider.PROPERTY_AUTHENTICATE + "=" + ResourceProvider.AUTHENTICATE_REQUIRED
+})
+public final class MockResourceProvider extends ResourceProvider<Void> {
+
+    @Reference(cardinality = ReferenceCardinality.OPTIONAL)
+    private EventAdmin eventAdmin;
+
+    private ResourceResolver resourceResolver;
+
+    @Activate
+    private void activate() {
+        MockResourceResolverFactoryOptions options = new MockResourceResolverFactoryOptions();
+        options.setMangleNamespacePrefixes(true);
+        options.setEventAdmin(eventAdmin);
+        ResourceResolverFactory resourceResolverFactory = new MockResourceResolverFactory(options);
+        try {
+            this.resourceResolver = resourceResolverFactory.getResourceResolver(null);
+        }
+        catch (LoginException ex) {
+            throw new RuntimeException(ex);
+        }
+    }
+
+    @Override
+    public @Nullable Resource getResource(@NotNull ResolveContext<Void> ctx,
+            @NotNull String path, @NotNull ResourceContext resourceContext,
+            @Nullable Resource parent) {
+        return resourceResolver.getResource(path);
+    }
+
+    @Override
+    public @Nullable Iterator<Resource> listChildren(
+            @NotNull ResolveContext<Void> ctx, @NotNull Resource parent) {
+        return resourceResolver.listChildren(parent);
+    }
+
+    @Override
+    public @NotNull Resource create(@NotNull ResolveContext<Void> ctx, String path,
+            Map<String, Object> properties) throws PersistenceException {
+        String parentPath = ResourceUtil.getParent(path);
+        if (parentPath == null) {
+            throw new PersistenceException("Invalid path: " + path);
+        }
+        Resource parent = resourceResolver.getResource(parentPath);
+        if (parent == null) {
+            throw new PersistenceException("Parent does not exist: " + parentPath);
+        }
+        return resourceResolver.create(parent, path, properties);
+    }
+
+    @Override
+    public void delete(@NotNull ResolveContext<Void> ctx, @NotNull Resource resource) throws PersistenceException {
+        resourceResolver.delete(resource);
+    }
+
+    @Override
+    public void revert(@NotNull ResolveContext<Void> ctx) {
+        resourceResolver.revert();
+    }
+
+    @Override
+    public void commit(@NotNull ResolveContext<Void> ctx) throws PersistenceException {
+        resourceResolver.commit();
+    }
+
+    @Override
+    public boolean hasChanges(@NotNull ResolveContext<Void> ctx) {
+        return resourceResolver.hasChanges();
+    }
+
+}