/*
* ============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));
}
}
/*
* ============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);
+ }
}