Author: bdelacretaz
Date: Mon Feb 29 09:16:10 2016
New Revision: 1732836
URL: http://svn.apache.org/viewvc?rev=1732836&view=rev
Log:
SLING-4941 - tests for JmxAdjustableStatusForTesting, contributed by Georg
Henzler, thanks!
Added:
sling/trunk/bundles/extensions/healthcheck/it/src/test/java/org/apache/sling/hc/it/core/JmxAdjustableStatusForTestingTest.java
Added:
sling/trunk/bundles/extensions/healthcheck/it/src/test/java/org/apache/sling/hc/it/core/JmxAdjustableStatusForTestingTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/it/src/test/java/org/apache/sling/hc/it/core/JmxAdjustableStatusForTestingTest.java?rev=1732836&view=auto
==============================================================================
---
sling/trunk/bundles/extensions/healthcheck/it/src/test/java/org/apache/sling/hc/it/core/JmxAdjustableStatusForTestingTest.java
(added)
+++
sling/trunk/bundles/extensions/healthcheck/it/src/test/java/org/apache/sling/hc/it/core/JmxAdjustableStatusForTestingTest.java
Mon Feb 29 09:16:10 2016
@@ -0,0 +1,153 @@
+/*
+ * 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.util.ArrayList;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+
+import javax.inject.Inject;
+import javax.management.DynamicMBean;
+
+import org.apache.sling.hc.api.HealthCheck;
+import org.apache.sling.hc.api.Result;
+import org.apache.sling.hc.api.ResultLog;
+import org.apache.sling.hc.api.ResultLog.Entry;
+import org.apache.sling.hc.api.execution.HealthCheckExecutionResult;
+import org.apache.sling.hc.api.execution.HealthCheckExecutor;
+import org.apache.sling.hc.util.FormattingResultLog;
+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;
+
+/** Test jmx-adjustable status for testing HC. */
+@RunWith(PaxExam.class)
+public class JmxAdjustableStatusForTestingTest {
+
+ @Inject
+ private HealthCheckExecutor executor;
+
+ @Inject
+ private BundleContext bundleContext;
+
+ private static String testTag = "testTagName";
+
+ @SuppressWarnings("rawtypes")
+ private List<ServiceRegistration> regs = new
ArrayList<ServiceRegistration>();
+
+ @Configuration
+ public Option[] config() {
+ return U.config();
+ }
+
+ @Before
+ public void setup() {
+ U.expectHealthChecks(0, executor, testTag);
+ registerHC(testTag);
+ }
+
+ @After
+ @SuppressWarnings("rawtypes")
+ public void cleanup() {
+ for (ServiceRegistration r : regs) {
+ r.unregister();
+ }
+ regs.clear();
+ U.expectHealthChecks(0, executor, testTag);
+ }
+
+ @Test
+ public void testJmxAdjustableStatusForTesting() throws Exception {
+
+ U.expectHealthChecks(1, executor, testTag);
+
+ Result result = getOverallResult(executor.execute(testTag));
+ assertEquals(Result.Status.OK, result.getStatus());
+
+ invokeMBean("addWarnResultForTags", new Object[] { testTag },
new String[] { String.class.getName() });
+
+ U.expectHealthChecks(2, executor, testTag);
+ result = getOverallResult(executor.execute(testTag));
+ assertEquals(Result.Status.WARN, result.getStatus());
+
+ invokeMBean("reset", new Object[] {}, new String[] {});
+
+ U.expectHealthChecks(1, executor, testTag);
+ result = getOverallResult(executor.execute(testTag));
+ assertEquals(Result.Status.OK, result.getStatus());
+
+ invokeMBean("addCriticalResultForTags", new Object[] {
"anotherTag," + testTag },
+ new String[] { String.class.getName() });
+
+ U.expectHealthChecks(2, executor, testTag);
+ result = getOverallResult(executor.execute(testTag));
+ assertEquals(Result.Status.CRITICAL, result.getStatus());
+
+ invokeMBean("reset", new Object[] {}, new String[] {});
+
+ U.expectHealthChecks(1, executor, testTag);
+ result = getOverallResult(executor.execute(testTag));
+ assertEquals(Result.Status.OK, result.getStatus());
+
+ }
+
+ private void registerHC(final String... tags) {
+ final HealthCheck hc = new HealthCheck() {
+ @Override
+ public Result execute() {
+ return new Result(Result.Status.OK, "All good
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, hc,
props));
+ }
+
+ private void invokeMBean(String operation, Object[] args, String[]
signature) throws Exception {
+
+ ServiceReference<?>[] serviceReference =
bundleContext.getServiceReferences(DynamicMBean.class.getName(),
+
"(jmx.objectname=org.apache.sling.healthcheck:type=AdjustableHealthCheckForTesting)");
+ DynamicMBean mBean = (DynamicMBean)
bundleContext.getService(serviceReference[0]);
+ mBean.invoke(operation, args, signature);
+
+ }
+
+ private Result getOverallResult(List<HealthCheckExecutionResult>
results) {
+ FormattingResultLog resultLog = new FormattingResultLog();
+ for (HealthCheckExecutionResult executionResult : results) {
+ for (Entry entry :
executionResult.getHealthCheckResult()) {
+ resultLog.add(new
ResultLog.Entry(entry.getStatus(), entry.getMessage(), entry.getException()));
+ }
+ }
+ return new Result(resultLog);
+ }
+
+}