Fix noop health check to analise if topics are alive 30/141830/1
authoradheli.tavares <adheli.tavares@est.tech>
Thu, 14 Aug 2025 10:11:44 +0000 (11:11 +0100)
committeradheli.tavares <adheli.tavares@est.tech>
Thu, 14 Aug 2025 10:17:17 +0000 (11:17 +0100)
Issue-ID: POLICY-5445
Change-Id: I4a004ebced4cd4e31e84fbdfae93823e8f986b4b
Signed-off-by: adheli.tavares <adheli.tavares@est.tech>
message-bus/src/main/java/org/onap/policy/common/message/bus/healthcheck/noop/NoopHealthCheck.java
message-bus/src/test/java/org/onap/policy/common/message/bus/healthcheck/noop/NoopHealthCheckTest.java

index 684f7f4..c0006ab 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * ============LICENSE_START=======================================================
- * Copyright (C) 2025 Nordix Foundation.
+ * Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.common.message.bus.healthcheck.noop;
 
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import org.onap.policy.common.message.bus.event.Topic;
+import org.onap.policy.common.message.bus.event.TopicEndpoint;
+import org.onap.policy.common.message.bus.event.TopicEndpointManager;
 import org.onap.policy.common.message.bus.healthcheck.TopicHealthCheck;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class NoopHealthCheck implements TopicHealthCheck {
 
+    private final Logger logger = LoggerFactory.getLogger(NoopHealthCheck.class);
+
+    private final TopicEndpoint topicEndpoint = TopicEndpointManager.getManager();
+
+    private Map<String, Topic> actualTopics;
+
     @Override
     public boolean healthCheck(List<String> topics) {
-        return true;
+        var topicsHealthy = true;
+
+        this.populateActualTopics();
+
+        if (topicEndpoint.isAlive()) {
+            for (String topic : topics) {
+                var actualTopic = actualTopics.get(topic.toLowerCase());
+                if (!actualTopic.isAlive()) {
+                    logger.warn("Topic {} is not alive!", topic);
+                    topicsHealthy = false;
+                    break;
+                }
+            }
+        } else {
+            logger.warn("Topic Endpoint is not alive!");
+            return false;
+        }
+
+        return topicsHealthy;
+    }
+
+    private void populateActualTopics() {
+        actualTopics = new HashMap<>();
+        topicEndpoint.getNoopTopicSinks().forEach(sink -> actualTopics.put(sink.getTopic(), sink));
+        topicEndpoint.getNoopTopicSources().forEach(source -> actualTopics.put(source.getTopic(), source));
     }
 }
index 2eb3604..fdd3e1b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * ============LICENSE_START=======================================================
- * Copyright (C) 2025 Nordix Foundation.
+ * Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.common.message.bus.healthcheck.noop;
 
+import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.util.List;
 import org.junit.jupiter.api.Test;
+import org.onap.policy.common.message.bus.event.CommonTestData;
+import org.onap.policy.common.message.bus.event.TopicEndpoint;
+import org.onap.policy.common.message.bus.event.TopicEndpointManager;
 
 class NoopHealthCheckTest {
 
     @Test
     void testBuild() {
+        TopicEndpoint topicEndpoint = TopicEndpointManager.getManager();
+        topicEndpoint.start();
         var healthCheck = new NoopHealthCheck();
         var result = healthCheck.healthCheck(List.of());
         assertTrue(result);
     }
+
+    @Test
+    void testBuild_Failure() {
+        TopicEndpoint topicEndpoint = TopicEndpointManager.getManager();
+        topicEndpoint.start();
+        var healthCheck = new NoopHealthCheck();
+        topicEndpoint.stop();
+        var result = healthCheck.healthCheck(List.of());
+        assertFalse(result);
+    }
+
+    @Test
+    void test_TopicIsAlive() {
+        TopicEndpoint topicEndpoint = TopicEndpointManager.getManager();
+
+        var topicSource = CommonTestData.getTopicParameters("topicSource", "noop", "localhost");
+        var topicSink = CommonTestData.getTopicParameters("topicSink", "noop", "localhost");
+
+        topicEndpoint.addTopicSources(List.of(topicSource));
+        topicEndpoint.addTopicSinks(List.of(topicSink));
+
+        topicEndpoint.start();
+        var healthCheck = new NoopHealthCheck();
+        var result = healthCheck.healthCheck(List.of("topicSource", "topicSink"));
+        assertTrue(result);
+    }
+
+    @Test
+    void test_TopicIsNotAlive() {
+        TopicEndpoint topicEndpoint = TopicEndpointManager.getManager();
+
+        var topicSource = CommonTestData.getTopicParameters("topicSource", "noop", "localhost");
+        var topicSink = CommonTestData.getTopicParameters("topicSink", "noop", "localhost");
+
+        topicEndpoint.addTopicSources(List.of(topicSource));
+        topicEndpoint.addTopicSinks(List.of(topicSink));
+
+        topicEndpoint.start();
+
+        var topic = topicEndpoint.getNoopTopicSource("topicsource");
+        topic.stop();
+        var healthCheck = new NoopHealthCheck();
+        var result = healthCheck.healthCheck(List.of("topicSource", "topicSink"));
+        assertFalse(result);
+    }
 }