Author: bdelacretaz
Date: Mon Feb 29 13:45:52 2016
New Revision: 1732895

URL: http://svn.apache.org/viewvc?rev=1732895&view=rev
Log:
SLING-5569 - test the ExtendedHealthCheckExecutor interface

Added:
    
sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/core/it/ExtendedHealthCheckExecutorIT.java

Added: 
sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/core/it/ExtendedHealthCheckExecutorIT.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/core/it/ExtendedHealthCheckExecutorIT.java?rev=1732895&view=auto
==============================================================================
--- 
sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/core/it/ExtendedHealthCheckExecutorIT.java
 (added)
+++ 
sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/core/it/ExtendedHealthCheckExecutorIT.java
 Mon Feb 29 13:45:52 2016
@@ -0,0 +1,112 @@
+/*
+ * 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.core.it;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.UUID;
+
+import javax.inject.Inject;
+
+import org.apache.sling.hc.api.HealthCheck;
+import org.apache.sling.hc.api.Result;
+import org.apache.sling.hc.api.execution.HealthCheckExecutionResult;
+import org.apache.sling.hc.api.execution.HealthCheckExecutor;
+import org.apache.sling.hc.util.HealthCheckFilter;
+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.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+
+/** Additional executor tests */
+@RunWith(PaxExam.class)
+public class ExtendedHealthCheckExecutorIT {
+
+    @Inject
+    private HealthCheckExecutor executor;
+    
+    @Inject
+    private BundleContext bundleContext;
+    
+    @SuppressWarnings("rawtypes")
+    private List<ServiceRegistration> regs = new 
ArrayList<ServiceRegistration>();
+    
+    private String testTag;
+    private final Result.Status testResult = Result.Status.OK;
+
+    @Configuration
+    public Option[] config() {
+        return U.config();
+    }
+    
+    private void registerHC(final String ... tags) {
+        final HealthCheck hc = new HealthCheck() {
+            @Override
+            public Result execute() {
+                return new Result(testResult, "Returning " + testResult + " 
for " + tags[0]);
+            }
+            
+        };
+        
+        final Dictionary<String, Object> props = new Hashtable<String, 
Object>();
+        props.put(HealthCheck.NAME, "name_" + tags[0]);
+        props.put(HealthCheck.TAGS, tags);
+        
+        regs.add(bundleContext.registerService(HealthCheck.class.getName(), 
hc, props));
+    }
+    
+    @Before
+    public void setup() {
+        testTag = "TEST_" + UUID.randomUUID().toString();
+        registerHC(testTag);
+        U.expectHealthChecks(1, executor, testTag);
+    }
+    
+    @After
+    public void cleanup() {
+        for(ServiceRegistration reg : regs) {
+            reg.unregister();
+        }
+    }
+    
+    @Test
+    public void testSingleExecution() throws Exception {
+        final HealthCheckFilter filter = new HealthCheckFilter(bundleContext);
+        final ServiceReference [] refs = 
filter.getTaggedHealthCheckServiceReferences(testTag);
+        assertNotNull(refs);
+        assertEquals(1, refs.length);
+        
+        // The ExtendedHealthCheckExecutor interface is not public, so we cheat
+        // to be able to test its implementation
+        final Method m = executor.getClass().getMethod("execute", 
ServiceReference.class);
+        final HealthCheckExecutionResult result = 
(HealthCheckExecutionResult)m.invoke(executor, refs[0]); 
+        assertEquals(testResult, result.getHealthCheckResult().getStatus());
+    }
+}
\ No newline at end of file


Reply via email to