From: adheli.tavares Date: Thu, 14 Aug 2025 10:11:44 +0000 (+0100) Subject: Fix noop health check to analise if topics are alive X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F30%2F141830%2F1;p=policy%2Fcommon.git Fix noop health check to analise if topics are alive Issue-ID: POLICY-5445 Change-Id: I4a004ebced4cd4e31e84fbdfae93823e8f986b4b Signed-off-by: adheli.tavares --- diff --git a/message-bus/src/main/java/org/onap/policy/common/message/bus/healthcheck/noop/NoopHealthCheck.java b/message-bus/src/main/java/org/onap/policy/common/message/bus/healthcheck/noop/NoopHealthCheck.java index 684f7f41..c0006abf 100644 --- a/message-bus/src/main/java/org/onap/policy/common/message/bus/healthcheck/noop/NoopHealthCheck.java +++ b/message-bus/src/main/java/org/onap/policy/common/message/bus/healthcheck/noop/NoopHealthCheck.java @@ -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. @@ -18,13 +18,50 @@ 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 actualTopics; + @Override public boolean healthCheck(List 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)); } } diff --git a/message-bus/src/test/java/org/onap/policy/common/message/bus/healthcheck/noop/NoopHealthCheckTest.java b/message-bus/src/test/java/org/onap/policy/common/message/bus/healthcheck/noop/NoopHealthCheckTest.java index 2eb36040..fdd3e1b9 100644 --- a/message-bus/src/test/java/org/onap/policy/common/message/bus/healthcheck/noop/NoopHealthCheckTest.java +++ b/message-bus/src/test/java/org/onap/policy/common/message/bus/healthcheck/noop/NoopHealthCheckTest.java @@ -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. @@ -18,17 +18,68 @@ 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); + } }