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

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 8aaa0af  CAMEL-15126: camel-health - polished
8aaa0af is described below

commit 8aaa0afa4ee76a529f40389b9db714cac41af490
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Thu May 28 13:37:20 2020 +0200

    CAMEL-15126: camel-health - polished
---
 .../apache/camel/health/HealthCheckRegistry.java   |  3 +-
 .../impl/health/DefaultHealthCheckRegistry.java    | 25 ++++++------
 .../health/DefaultHealthCheckRegistryTest.java     | 46 +++++++++++++++++++++-
 3 files changed, 61 insertions(+), 13 deletions(-)

diff --git 
a/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckRegistry.java 
b/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckRegistry.java
index 6187114..e20b784 100644
--- 
a/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckRegistry.java
+++ 
b/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckRegistry.java
@@ -22,6 +22,7 @@ import java.util.stream.Collectors;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
+import org.apache.camel.StaticService;
 import org.apache.camel.util.ObjectHelper;
 
 /**
@@ -30,7 +31,7 @@ import org.apache.camel.util.ObjectHelper;
  * Note that this registry can be superseded by the future camel context 
internal
  * registry, @see <a 
href="https://issues.apache.org/jira/browse/CAMEL-10792"/>.
  */
-public interface HealthCheckRegistry extends HealthCheckRepository, 
CamelContextAware {
+public interface HealthCheckRegistry extends HealthCheckRepository, 
CamelContextAware, StaticService {
 
     /**
      * Service factory key.
diff --git 
a/core/camel-health/src/main/java/org/apache/camel/impl/health/DefaultHealthCheckRegistry.java
 
b/core/camel-health/src/main/java/org/apache/camel/impl/health/DefaultHealthCheckRegistry.java
index 53cde1e..8a0b92c 100644
--- 
a/core/camel-health/src/main/java/org/apache/camel/impl/health/DefaultHealthCheckRegistry.java
+++ 
b/core/camel-health/src/main/java/org/apache/camel/impl/health/DefaultHealthCheckRegistry.java
@@ -24,7 +24,6 @@ import java.util.stream.Stream;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
-import org.apache.camel.StaticService;
 import org.apache.camel.health.HealthCheck;
 import org.apache.camel.health.HealthCheckRegistry;
 import org.apache.camel.health.HealthCheckRepository;
@@ -37,7 +36,7 @@ import org.slf4j.LoggerFactory;
  * Default {@link HealthCheckRegistry}.
  */
 @JdkService(HealthCheckRegistry.FACTORY)
-public class DefaultHealthCheckRegistry extends ServiceSupport implements 
HealthCheckRegistry, StaticService {
+public class DefaultHealthCheckRegistry extends ServiceSupport implements 
HealthCheckRegistry {
     private static final Logger LOG = 
LoggerFactory.getLogger(DefaultHealthCheckRegistry.class);
 
     private final Set<HealthCheck> checks;
@@ -56,28 +55,32 @@ public class DefaultHealthCheckRegistry extends 
ServiceSupport implements Health
         setCamelContext(camelContext);
     }
 
-    // ************************************
-    // Properties
-    // ************************************
-
     @Override
-    public final void setCamelContext(CamelContext camelContext) {
-        this.camelContext = camelContext;
+    protected void doInit() throws Exception {
+        super.doInit();
 
-        // TODO: Move this to doInit
-        for (HealthCheck check: checks) {
+        for (HealthCheck check : checks) {
             if (check instanceof CamelContextAware) {
                 ((CamelContextAware) check).setCamelContext(camelContext);
             }
         }
 
-        for (HealthCheckRepository repository: repositories) {
+        for (HealthCheckRepository repository : repositories) {
             if (repository instanceof CamelContextAware) {
                 ((CamelContextAware) repository).setCamelContext(camelContext);
             }
         }
     }
 
+    // ************************************
+    // Properties
+    // ************************************
+
+    @Override
+    public final void setCamelContext(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
     @Override
     public final CamelContext getCamelContext() {
         return camelContext;
diff --git 
a/core/camel-health/src/test/java/org/apache/camel/impl/health/DefaultHealthCheckRegistryTest.java
 
b/core/camel-health/src/test/java/org/apache/camel/impl/health/DefaultHealthCheckRegistryTest.java
index a8fe62f..6e3cab5 100644
--- 
a/core/camel-health/src/test/java/org/apache/camel/impl/health/DefaultHealthCheckRegistryTest.java
+++ 
b/core/camel-health/src/test/java/org/apache/camel/impl/health/DefaultHealthCheckRegistryTest.java
@@ -21,9 +21,12 @@ import java.util.Map;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
 import org.apache.camel.health.HealthCheck;
 import org.apache.camel.health.HealthCheckRegistry;
 import org.apache.camel.health.HealthCheckResultBuilder;
+import org.apache.camel.impl.DefaultCamelContext;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -82,7 +85,38 @@ public class DefaultHealthCheckRegistryTest {
         }
     }
 
-    private class MyHealthCheck extends AbstractHealthCheck {
+    @Test
+    public void testInjectCamelContext() throws Exception {
+        CamelContext context = new DefaultCamelContext();
+
+        HealthCheckRegistry registry = new DefaultHealthCheckRegistry();
+        registry.setCamelContext(context);
+
+        registry.register(new MyHealthCheck("G1", "1"));
+        registry.register(new MyHealthCheck("G1", "1"));
+        registry.register(new MyHealthCheck("G1", "2"));
+        registry.register(new MyHealthCheck("G2", "3"));
+
+        context.start();
+        registry.start();
+
+        List<HealthCheck> checks = 
registry.stream().collect(Collectors.toList());
+        Assert.assertEquals(3, checks.size());
+
+        for (HealthCheck check : checks) {
+            HealthCheck.Result response = check.call();
+
+            Assert.assertEquals(HealthCheck.State.UP, response.getState());
+            Assert.assertFalse(response.getMessage().isPresent());
+            Assert.assertFalse(response.getError().isPresent());
+            Assert.assertSame(context, ((CamelContextAware) 
check).getCamelContext());
+        }
+    }
+
+    private class MyHealthCheck extends AbstractHealthCheck implements 
CamelContextAware {
+
+        private CamelContext context;
+
         protected MyHealthCheck(String group, String id) {
             super(group, id);
             getConfiguration().setEnabled(true);
@@ -92,5 +126,15 @@ public class DefaultHealthCheckRegistryTest {
         public void doCall(HealthCheckResultBuilder builder, Map<String, 
Object> options) {
             builder.up();
         }
+
+        @Override
+        public void setCamelContext(CamelContext camelContext) {
+            this.context = camelContext;
+        }
+
+        @Override
+        public CamelContext getCamelContext() {
+            return context;
+        }
     }
 }

Reply via email to