You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2018/12/12 09:23:53 UTC

[sling-org-apache-sling-capabilities] branch master updated: SLING-8172 - move SlingServletsSource to test code

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

bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-capabilities.git


The following commit(s) were added to refs/heads/master by this push:
     new df1e5bc  SLING-8172 - move SlingServletsSource to test code
df1e5bc is described below

commit df1e5bc7ebe68882c9164040f735b831436c9669
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Wed Dec 12 10:23:11 2018 +0100

    SLING-8172 - move SlingServletsSource to test code
---
 README.md                                          | 56 ++--------------------
 .../defaultsources/SlingServletsSource.java        | 10 ++--
 .../defaultsources/SlingServletsSourceTest.java    | 34 ++++++++-----
 3 files changed, 31 insertions(+), 69 deletions(-)

diff --git a/README.md b/README.md
index 4eee0a6..3a374f4 100644
--- 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 \
diff --git a/src/main/java/org/apache/sling/capabilities/defaultsources/SlingServletsSource.java b/src/test/java/org/apache/sling/capabilities/defaultsources/SlingServletsSource.java
similarity index 97%
rename from src/main/java/org/apache/sling/capabilities/defaultsources/SlingServletsSource.java
rename to src/test/java/org/apache/sling/capabilities/defaultsources/SlingServletsSource.java
index 0de2c01..18b807c 100644
--- a/src/main/java/org/apache/sling/capabilities/defaultsources/SlingServletsSource.java
+++ b/src/test/java/org/apache/sling/capabilities/defaultsources/SlingServletsSource.java
@@ -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() {
diff --git a/src/test/java/org/apache/sling/capabilities/defaultsources/SlingServletsSourceTest.java b/src/test/java/org/apache/sling/capabilities/defaultsources/SlingServletsSourceTest.java
index e95562a..f5c43fe 100644
--- a/src/test/java/org/apache/sling/capabilities/defaultsources/SlingServletsSourceTest.java
+++ b/src/test/java/org/apache/sling/capabilities/defaultsources/SlingServletsSourceTest.java
@@ -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" };