You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:40:06 UTC

[sling-org-apache-sling-hc-support] 19/31: SLING-3034 - SlingRequestStatusHealthCheckTest added

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

rombert pushed a commit to annotated tag org.apache.sling.hc.support-1.0.4
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-hc-support.git

commit f445623626b07373d1517f968de85a87a115189d
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Tue Sep 3 13:36:03 2013 +0000

    SLING-3034 - SlingRequestStatusHealthCheckTest added
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/healthcheck/support@1519678 13f79535-47bb-0310-9956-ffa450edef68
---
 .../impl/SlingRequestStatusHealthCheck.java        |  6 +-
 .../healthchecks/DefaultLoginsHealthCheckTest.java | 11 +--
 .../org/apache/sling/hc/healthchecks/SetField.java | 30 +++++++
 .../SlingRequestStatusHealthCheckTest.java         | 96 ++++++++++++++++++++++
 4 files changed, 131 insertions(+), 12 deletions(-)

diff --git a/src/main/java/org/apache/sling/hc/support/impl/SlingRequestStatusHealthCheck.java b/src/main/java/org/apache/sling/hc/support/impl/SlingRequestStatusHealthCheck.java
index b982a9e..968174e 100644
--- a/src/main/java/org/apache/sling/hc/support/impl/SlingRequestStatusHealthCheck.java
+++ b/src/main/java/org/apache/sling/hc/support/impl/SlingRequestStatusHealthCheck.java
@@ -18,6 +18,7 @@
 package org.apache.sling.hc.support.impl;
 
 import java.util.Arrays;
+import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
 
@@ -36,7 +37,6 @@ import org.apache.sling.engine.SlingRequestProcessor;
 import org.apache.sling.hc.api.HealthCheck;
 import org.apache.sling.hc.api.Result;
 import org.apache.sling.hc.util.FormattingResultLog;
-import org.osgi.service.component.ComponentContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -89,8 +89,8 @@ public class SlingRequestStatusHealthCheck implements HealthCheck {
     private ResourceResolverFactory resolverFactory;
 
     @Activate
-    public void activate(ComponentContext ctx) {
-        paths = PropertiesUtil.toStringArray(ctx.getProperties().get(PROP_PATH), new String [] {});
+    public void activate(final Map<String, Object> properties) {
+        paths = PropertiesUtil.toStringArray(properties.get(PROP_PATH), new String [] {});
         log.info("Activated, paths={}", Arrays.asList(paths));
     }
 
diff --git a/src/test/java/org/apache/sling/hc/healthchecks/DefaultLoginsHealthCheckTest.java b/src/test/java/org/apache/sling/hc/healthchecks/DefaultLoginsHealthCheckTest.java
index 7ce900b..862b6f8 100644
--- a/src/test/java/org/apache/sling/hc/healthchecks/DefaultLoginsHealthCheckTest.java
+++ b/src/test/java/org/apache/sling/hc/healthchecks/DefaultLoginsHealthCheckTest.java
@@ -20,7 +20,6 @@ package org.apache.sling.hc.healthchecks;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
-import java.lang.reflect.Field;
 import java.util.Arrays;
 
 import javax.jcr.Credentials;
@@ -40,10 +39,10 @@ public class DefaultLoginsHealthCheckTest {
     
     private Result getTestResult(String login) throws Exception {
         final DefaultLoginsHealthCheck c = new DefaultLoginsHealthCheck();
-        setField(c, "logins", Arrays.asList(new String[] { login }));
+        SetField.set(c, "logins", Arrays.asList(new String[] { login }));
         
         final SlingRepository repo = Mockito.mock(SlingRepository.class);
-        setField(c, "repository", repo);
+        SetField.set(c, "repository", repo);
         final Session s = Mockito.mock(Session.class);
         Mockito.when(repo.login(Matchers.any(Credentials.class))).thenAnswer(new Answer<Session>() {
             @Override
@@ -59,12 +58,6 @@ public class DefaultLoginsHealthCheckTest {
         return c.execute();
     }
     
-    private void setField(Object o, String name, Object value) throws Exception {
-        final Field f = o.getClass().getDeclaredField(name);
-        f.setAccessible(true);
-        f.set(o, value);
-    }
-    
     @Test
     public void testHealthCheckFails() throws Exception {
         assertFalse("Expecting failed check", getTestResult("admin:admin").isOk());
diff --git a/src/test/java/org/apache/sling/hc/healthchecks/SetField.java b/src/test/java/org/apache/sling/hc/healthchecks/SetField.java
new file mode 100644
index 0000000..c4577a3
--- /dev/null
+++ b/src/test/java/org/apache/sling/hc/healthchecks/SetField.java
@@ -0,0 +1,30 @@
+/*
+ * 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.healthchecks;
+
+import java.lang.reflect.Field;
+
+public class SetField {
+    
+    public static void set(Object o, String name, Object value) throws Exception {
+        final Field f = o.getClass().getDeclaredField(name);
+        f.setAccessible(true);
+        f.set(o, value);
+    }
+    
+}
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/hc/healthchecks/SlingRequestStatusHealthCheckTest.java b/src/test/java/org/apache/sling/hc/healthchecks/SlingRequestStatusHealthCheckTest.java
new file mode 100644
index 0000000..6731465
--- /dev/null
+++ b/src/test/java/org/apache/sling/hc/healthchecks/SlingRequestStatusHealthCheckTest.java
@@ -0,0 +1,96 @@
+package org.apache.sling.hc.healthchecks;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.engine.SlingRequestProcessor;
+import org.apache.sling.hc.api.Result;
+import org.apache.sling.hc.support.impl.SlingRequestStatusHealthCheck;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+import org.mockito.Matchers;
+import org.mockito.Mockito;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+@RunWith(Parameterized.class)
+public class SlingRequestStatusHealthCheckTest {
+    private SlingRequestStatusHealthCheck hc;
+    private final Result.Status expectedStatus;
+    private final String [] paths;
+    
+    @Parameters(name="{0}")
+    public static List<Object[]> data() {
+            final List<Object[]> result = new ArrayList<Object[]>();
+            result.add(new Object[] { "200.html:200,502.html:1234,436.json:436", Result.Status.WARN });
+            
+            result.add(new Object[] { "", Result.Status.OK }); 
+            result.add(new Object[] { "200.x", Result.Status.OK }); 
+            result.add(new Object[] { "404.html:404", Result.Status.OK }); 
+            result.add(new Object[] { "200.html:200,502.html:502,436.json:436" , Result.Status.OK }); 
+            result.add(new Object[] { "200.html:1234,502.html:502,436.json:436" , Result.Status.WARN }); 
+            result.add(new Object[] { "200.html:200,502.html:1234,436.json:436" , Result.Status.WARN }); 
+            result.add(new Object[] { "200.html:200,502.html:502,436.json:1234" , Result.Status.WARN }); 
+            result.add(new Object[] { "200.html:1234,502.html:1234,436.json:1234" , Result.Status.WARN }); 
+            return result;
+    }
+    
+    @Before
+    public void setup() throws Exception {
+        hc = new SlingRequestStatusHealthCheck();
+        
+        final ResourceResolverFactory rrf = Mockito.mock(ResourceResolverFactory.class);
+        SetField.set(hc, "resolverFactory", rrf);
+        
+        final Answer<Void> a = new Answer<Void> () {
+            @Override
+            public Void answer(InvocationOnMock invocation) {
+                final HttpServletRequest request = (HttpServletRequest)invocation.getArguments()[0];
+                final HttpServletResponse response = (HttpServletResponse)invocation.getArguments()[1];
+                final String path = request.getPathInfo();
+                if(path.length() > 0) {
+                    final String status = path.substring(0, path.indexOf('.'));
+                    response.setStatus(Integer.valueOf(status));
+                }
+                return null;
+            }
+            
+        };
+        
+        final SlingRequestProcessor srp = Mockito.mock(SlingRequestProcessor.class);
+        SetField.set(hc, "requestProcessor", srp);
+        Mockito.doAnswer(a).when(srp).processRequest(
+                Matchers.any(HttpServletRequest.class),  
+                Matchers.any(HttpServletResponse.class), 
+                Matchers.any(ResourceResolver.class));
+        
+        
+        final Map<String, Object> properties = new HashMap<String, Object>();
+        properties.put("path", paths);
+        hc.activate(properties);
+    }
+    
+    public SlingRequestStatusHealthCheckTest(String paths, Result.Status expectedStatus) {
+        this.paths = paths.split(",");
+        this.expectedStatus = expectedStatus;
+        
+    }
+    
+    @Test
+    public void testResult() {
+        assertEquals("Expecting result " + expectedStatus + " for paths=" + paths,
+                expectedStatus, hc.execute().getStatus());
+    }
+}

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