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 bc27423294075d41f94207b0a760ae9ac7e8923b Author: Bertrand Delacretaz <[email protected]> AuthorDate: Mon Feb 29 09:16:10 2016 +0000 SLING-4941 - tests for JmxAdjustableStatusForTesting, contributed by Georg Henzler, thanks! git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1732836 13f79535-47bb-0310-9956-ffa450edef68 --- .../it/core/JmxAdjustableStatusForTestingTest.java | 153 +++++++++++++++++++++ 1 file changed, 153 insertions(+) diff --git a/src/test/java/org/apache/sling/hc/it/core/JmxAdjustableStatusForTestingTest.java b/src/test/java/org/apache/sling/hc/it/core/JmxAdjustableStatusForTestingTest.java new file mode 100644 index 0000000..96ed2fa --- /dev/null +++ b/src/test/java/org/apache/sling/hc/it/core/JmxAdjustableStatusForTestingTest.java @@ -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); + } + +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
