sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From romb...@apache.org
Subject [sling-org-apache-sling-hc-it] 36/49: SLING-4862 - add test for HC servlet activation
Date Wed, 18 Oct 2017 23:20:46 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit ed635e0b50996760ec457fd4930cfc753d53bd9f
Author: Bertrand Delacretaz <bdelacretaz@apache.org>
AuthorDate: Fri Aug 14 11:31:39 2015 +0000

    SLING-4862 - add test for HC servlet activation
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1695866 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  13 ++-
 .../sling/hc/it/core/HealthCheckServletTest.java   | 121 +++++++++++++++++++++
 .../apache/sling/hc/it/core/MockHttpService.java   |  55 ++++++++++
 src/test/java/org/apache/sling/hc/it/core/U.java   |   4 +-
 4 files changed, 191 insertions(+), 2 deletions(-)

diff --git a/pom.xml b/pom.xml
index b0da36e..eb7b03f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -121,5 +121,16 @@
             <version>4.2.1</version>
             <scope>test</scope>
         </dependency>
-     </dependencies>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.http.servlet-api</artifactId>
+            <version>1.1.0</version>
+            <scope>test</scope>
+        </dependency>   
+    </dependencies>
 </project>
diff --git a/src/test/java/org/apache/sling/hc/it/core/HealthCheckServletTest.java b/src/test/java/org/apache/sling/hc/it/core/HealthCheckServletTest.java
new file mode 100644
index 0000000..b62ca67
--- /dev/null
+++ b/src/test/java/org/apache/sling/hc/it/core/HealthCheckServletTest.java
@@ -0,0 +1,121 @@
+/*
+ * 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 SF 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.hc.it.core;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.UUID;
+
+import javax.inject.Inject;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.http.HttpService;
+
+/** Verify that the HealthCheckExecutorServlet becomes available
+ *  after creating the corresponding config
+ */
+@RunWith(PaxExam.class)
+public class HealthCheckServletTest {
+
+    @Inject
+    private ConfigurationAdmin configAdmin;
+    
+    @Inject
+    private BundleContext bundleContext;
+    
+    private MockHttpService httpService;
+    private ServiceRegistration reg;
+
+    @Configuration
+    public Option[] config() {
+        return U.config();
+    }
+    
+    private int countServletServices(String packageNamePrefix) throws InvalidSyntaxException
{
+        final ServiceReference<?> [] refs = bundleContext.getServiceReferences("javax.servlet.Servlet",
null);
+        int count = 0;
+        if(refs != null) {
+            for(ServiceReference ref : refs) {
+                final Object o = bundleContext.getService(ref);
+                if(o.getClass().getName().startsWith(packageNamePrefix)) {
+                    count++;
+                }
+                bundleContext.ungetService(ref);
+            }
+        }
+        return count;
+    }
+    
+    @Before
+    public void setup() {
+        httpService = new MockHttpService();
+        reg = bundleContext.registerService(HttpService.class, httpService, null);
+    }
+
+    @After
+    public void cleanup() {
+        reg.unregister();
+        reg = null;
+        httpService = null;
+    }
+
+    @Test
+    public void testServletBecomesActive() throws InvalidSyntaxException, IOException, InterruptedException
{
+        final String property = "servletPath";
+        final String path = "/test/" + UUID.randomUUID();
+        final String packagePrefix = "org.apache.sling.hc";
+        assertEquals("Initially expecting no servlet from " + packagePrefix, 0, countServletServices(packagePrefix));
+        final int pathsBefore = httpService.getPaths().size();
+        
+        // Activate servlet and wait for it to show up
+        final String pid = "org.apache.sling.hc.core.impl.servlet.HealthCheckExecutorServlet";
+        final org.osgi.service.cm.Configuration cfg = configAdmin.getConfiguration(pid, null);
+        final Dictionary<String, Object> props = new Hashtable<String, Object>();
+        props.put(property, path);
+        cfg.update(props);
+        
+        final long timeoutMsec = 5000L;
+        final long endTime = System.currentTimeMillis() + timeoutMsec;
+        while(System.currentTimeMillis() < endTime) {
+            if(countServletServices(packagePrefix) > 0) {
+                break;
+            }
+            Thread.sleep(50L);
+        }
+        
+        assertEquals("After adding configuration, expecting one servlet from " +  packagePrefix,
1, countServletServices(packagePrefix));
+        final List<String> paths = httpService.getPaths();
+        assertEquals("Expecting one new servlet registration", pathsBefore + 1, paths.size());
+        assertEquals("Expecting the HC servlet to be registered at " + path, path, paths.get(paths.size()
- 1));
+    }
+}
diff --git a/src/test/java/org/apache/sling/hc/it/core/MockHttpService.java b/src/test/java/org/apache/sling/hc/it/core/MockHttpService.java
new file mode 100644
index 0000000..13d6548
--- /dev/null
+++ b/src/test/java/org/apache/sling/hc/it/core/MockHttpService.java
@@ -0,0 +1,55 @@
+/*
+ * 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 SF 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.hc.it.core;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.List;
+
+import javax.servlet.Servlet;
+
+import org.osgi.service.http.HttpContext;
+import org.osgi.service.http.HttpService;
+
+class MockHttpService implements HttpService {
+
+    private List<String> paths = new ArrayList<String>();
+            
+    @Override
+    public void registerResources(String alias, String name, HttpContext context) {
+    }
+
+    @Override
+    public void registerServlet(String alias, Servlet servlet, Dictionary initparams, HttpContext
context) {
+        paths.add(alias);
+    }
+
+    public void unregister(String alias) {
+        paths.remove(alias);
+    }
+
+    @Override
+    public HttpContext createDefaultHttpContext() {
+        return null;
+    }
+    
+    List<String> getPaths() {
+        return Collections.unmodifiableList(paths);
+    }
+}
diff --git a/src/test/java/org/apache/sling/hc/it/core/U.java b/src/test/java/org/apache/sling/hc/it/core/U.java
index 53aaaa0..38b3f91 100644
--- a/src/test/java/org/apache/sling/hc/it/core/U.java
+++ b/src/test/java/org/apache/sling/hc/it/core/U.java
@@ -75,11 +75,13 @@ public class U {
                     provision(
                             mavenBundle("org.apache.felix", "org.apache.felix.gogo.shell",
"0.10.0"),
                             mavenBundle("org.apache.felix", "org.apache.felix.gogo.runtime",
"0.10.0"),
-                            mavenBundle("org.apache.felix", "org.apache.felix.gogo.command",
"0.12.0")
+                            mavenBundle("org.apache.felix", "org.apache.felix.gogo.command",
"0.12.0"),
+                            mavenBundle("org.apache.felix", "org.apache.felix.shell.remote",
"1.1.2")
                     )
             ),
             provision(
                     mavenBundle("org.apache.felix", "org.apache.felix.scr", "1.6.2"),
+                    mavenBundle("org.apache.felix", "org.apache.felix.configadmin", "1.8.8"),
                     mavenBundle("org.apache.felix", "org.apache.felix.http.servlet-api",
"1.1.0"),
                     mavenBundle("org.apache.sling", "org.apache.sling.hc.core", coreVersion),
                     mavenBundle("org.apache.sling", "org.apache.sling.hc.samples", samplesVersion),

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <commits@sling.apache.org>.

Mime
View raw message