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 2013/09/03 15:36:04 UTC

svn commit: r1519678 - in /sling/trunk/contrib/extensions/healthcheck/support/src: main/java/org/apache/sling/hc/support/impl/ test/java/org/apache/sling/hc/healthchecks/

Author: bdelacretaz
Date: Tue Sep  3 13:36:03 2013
New Revision: 1519678

URL: http://svn.apache.org/r1519678
Log:
SLING-3034 - SlingRequestStatusHealthCheckTest added

Added:
    sling/trunk/contrib/extensions/healthcheck/support/src/test/java/org/apache/sling/hc/healthchecks/SetField.java
    sling/trunk/contrib/extensions/healthcheck/support/src/test/java/org/apache/sling/hc/healthchecks/SlingRequestStatusHealthCheckTest.java
Modified:
    sling/trunk/contrib/extensions/healthcheck/support/src/main/java/org/apache/sling/hc/support/impl/SlingRequestStatusHealthCheck.java
    sling/trunk/contrib/extensions/healthcheck/support/src/test/java/org/apache/sling/hc/healthchecks/DefaultLoginsHealthCheckTest.java

Modified: sling/trunk/contrib/extensions/healthcheck/support/src/main/java/org/apache/sling/hc/support/impl/SlingRequestStatusHealthCheck.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/support/src/main/java/org/apache/sling/hc/support/impl/SlingRequestStatusHealthCheck.java?rev=1519678&r1=1519677&r2=1519678&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/support/src/main/java/org/apache/sling/hc/support/impl/SlingRequestStatusHealthCheck.java (original)
+++ sling/trunk/contrib/extensions/healthcheck/support/src/main/java/org/apache/sling/hc/support/impl/SlingRequestStatusHealthCheck.java Tue Sep  3 13:36:03 2013
@@ -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.SlingRequ
 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 SlingRequestStatusHealthChe
     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));
     }
 

Modified: sling/trunk/contrib/extensions/healthcheck/support/src/test/java/org/apache/sling/hc/healthchecks/DefaultLoginsHealthCheckTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/support/src/test/java/org/apache/sling/hc/healthchecks/DefaultLoginsHealthCheckTest.java?rev=1519678&r1=1519677&r2=1519678&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/support/src/test/java/org/apache/sling/hc/healthchecks/DefaultLoginsHealthCheckTest.java (original)
+++ sling/trunk/contrib/extensions/healthcheck/support/src/test/java/org/apache/sling/hc/healthchecks/DefaultLoginsHealthCheckTest.java Tue Sep  3 13:36:03 2013
@@ -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 DefaultLoginsHealthCheckTes
     
     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 DefaultLoginsHealthCheckTes
         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());

Added: sling/trunk/contrib/extensions/healthcheck/support/src/test/java/org/apache/sling/hc/healthchecks/SetField.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/support/src/test/java/org/apache/sling/hc/healthchecks/SetField.java?rev=1519678&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/support/src/test/java/org/apache/sling/hc/healthchecks/SetField.java (added)
+++ sling/trunk/contrib/extensions/healthcheck/support/src/test/java/org/apache/sling/hc/healthchecks/SetField.java Tue Sep  3 13:36:03 2013
@@ -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

Added: sling/trunk/contrib/extensions/healthcheck/support/src/test/java/org/apache/sling/hc/healthchecks/SlingRequestStatusHealthCheckTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/support/src/test/java/org/apache/sling/hc/healthchecks/SlingRequestStatusHealthCheckTest.java?rev=1519678&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/support/src/test/java/org/apache/sling/hc/healthchecks/SlingRequestStatusHealthCheckTest.java (added)
+++ sling/trunk/contrib/extensions/healthcheck/support/src/test/java/org/apache/sling/hc/healthchecks/SlingRequestStatusHealthCheckTest.java Tue Sep  3 13:36:03 2013
@@ -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());
+    }
+}