SLING-8172 - move SlingServletsSource to test code
authorBertrand Delacretaz <bdelacretaz@apache.org>
Wed, 12 Dec 2018 09:23:11 +0000 (10:23 +0100)
committerBertrand Delacretaz <bdelacretaz@apache.org>
Wed, 12 Dec 2018 09:23:11 +0000 (10:23 +0100)
README.md
src/test/java/org/apache/sling/capabilities/defaultsources/SlingServletsSource.java [moved from src/main/java/org/apache/sling/capabilities/defaultsources/SlingServletsSource.java with 97% similarity]
src/test/java/org/apache/sling/capabilities/defaultsources/SlingServletsSourceTest.java

index 4eee0a6..3a374f4 100644 (file)
--- a/README.md
+++ b/README.md
@@ -44,6 +44,8 @@ categories that can be provided separately (see below):
          */
           Map<String, Object> getCapabilities(ResourceResolver resolver) throws Exception;
     }
+
+This module does not provide any `CapabilitiesSource` service, but an example `SlingServletsSource` is provided in the test code.
     
 The sling/capabilities resource type
 ------------------------------------
@@ -54,59 +56,7 @@ resource type.
 A required property named `namespace_patterns` must be present, containing 1..N Java
 regexp patterns to select which capabilities namespaces are exposed by this resource.
 
-Example configuration
----------------------
-This module does not provide any active `CapabilitiesSource` out of the box, but it provides a
-`SlingServletsSource` that can be used to exposes which Sling servlets are active, including their
-`sling.servlet.*` properties for reference.
-
-With the example configuration below a `sling/capabilities` resource with 
-`namespace_patterns='org\.apache\.sling\.servlets\.test[A|B]'`
-produces the following output at `/var/capabilities/caps.json` with the resource shown 
-below:
-
-    {
-      "org.apache.sling.capabilities": {
-        "data": {
-          "org.apache.sling.servlets.testA": {
-            "GetAclServlet_89b2e8f3": {
-              "sling.servlet.extensions": "json",
-              "sling.servlet.methods": "GET",
-              "sling.servlet.resourceTypes": "sling/servlet/default",
-              "sling.servlet.selectors": [
-                "acl",
-                "tidy.acl"
-              ]
-            }
-          },
-          "org.apache.sling.servlets.testB": {
-            "ChangeUserPasswordServlet_458ad2ce": {
-              "sling.servlet.methods": "POST",
-              "sling.servlet.resourceTypes": "sling/user",
-              "sling.servlet.selectors": "changePassword"
-            }
-          }
-        }
-      }
-    }
-
-The configured `testC` namespace is omitted due to the `namespace_patterns` property of the resource shown below.
-
-Here's the required configuration, excerpted from `/system/console/status-Configurations`:
-
-       Factory PID = org.apache.sling.capabilities.defaultsources.SlingServletsSource
-       capabilitiesNamespaceSuffix = testA
-       servletsLdapFilter = (&(sling.servlet.extensions=json)(sling.servlet.selectors=acl))    
-       
-       Factory PID = org.apache.sling.capabilities.defaultsources.SlingServletsSource
-       capabilitiesNamespaceSuffix = testB
-       servletsLdapFilter = (&(sling.servlet.resourceTypes=sling/user)(sling.servlet.selectors=changePassword))
-
-       Factory PID = org.apache.sling.capabilities.defaultsources.SlingServletsSource
-       capabilitiesNamespaceSuffix = testC
-       servletsLdapFilter = (sling.servlet.extensions=html)
-
-And a resource that then generates the above output can be created with
+As an example, a resource that causes capabilities with the `org\.apache\.sling\.servlets\.test[A|B]` namespace regexp to be output (assuming a `CapabilitiesSource` that provides them is available) can be created as follows:
 
     curl -u admin:admin \
       -Fsling:resourceType=sling/capabilities \
@@ -77,12 +77,16 @@ public class SlingServletsSource implements CapabilitiesSource {
     private static final String SLING_SERVLET_PROPERTY_PREFIX = "sling.servlet.";
     public static final String NAMESPACE_PREFIX = "org.apache.sling.servlets.";
     
-    @Activate
-    public void activate(Config cfg, ComponentContext ctx) {
-        this.bundleContext = ctx.getBundleContext();
+    void configure(Config cfg, BundleContext bctx) {
+        this.bundleContext = bctx;
         this.namespace = NAMESPACE_PREFIX + cfg.capabilitiesNamespaceSuffix();
         this.ldapFilter = cfg.servletsLdapFilter();
     }
+    
+    @Activate
+    public void activate(Config cfg, ComponentContext ctx) {
+        configure(cfg, ctx.getBundleContext());
+    }
 
     @Override
     public String getNamespace() {
index e95562a..f5c43fe 100644 (file)
@@ -19,9 +19,8 @@
 package org.apache.sling.capabilities.defaultsources;
 
 import java.io.IOException;
-import java.util.Dictionary;
+import java.lang.annotation.Annotation;
 import java.util.HashMap;
-import java.util.Hashtable;
 import java.util.Map;
 import java.util.regex.Pattern;
 import javax.servlet.Servlet;
@@ -34,8 +33,6 @@ import static org.junit.Assert.assertTrue;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
-import org.osgi.service.cm.Configuration;
-import org.osgi.service.cm.ConfigurationAdmin;
 
 public class SlingServletsSourceTest {
 
@@ -59,16 +56,27 @@ public class SlingServletsSourceTest {
 
     @Before
     public void setup() throws IOException {
-        // Configure allowed path patterns
-        final ConfigurationAdmin ca = context.getService(ConfigurationAdmin.class);
-        assertNotNull("Expecting a ConfigurationAdmin service", ca);
-        final Configuration cfg = ca.getConfiguration(SlingServletsSource.class.getName());
-        final Dictionary<String, Object> props = new Hashtable<>();
-        props.put("capabilitiesNamespaceSuffix", "TEST_NS");
-        props.put("servletsLdapFilter", "(sling.servlet.extensions=json)");
-        cfg.update(props);
+        
+        final SlingServletsSource.Config cfg = new SlingServletsSource.Config() {
+            @Override
+            public String servletsLdapFilter() {
+                return "(sling.servlet.extensions=json)";
+            }
 
-        context.registerInjectActivateService(new SlingServletsSource());
+            @Override
+            public String capabilitiesNamespaceSuffix() {
+                return "TEST_NS";
+            }
+
+            @Override
+            public Class<? extends Annotation> annotationType() {
+                return SlingServletsSource.Config.class;
+            }
+        };
+
+        final SlingServletsSource src = new SlingServletsSource();
+        src.configure(cfg, context.bundleContext());
+        context.registerService(CapabilitiesSource.class, src);
 
         // Need a few (fake) Sling servlets to test
         final String [] ext = { "json", "txt", "json" };