2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2025 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.participant.intermediary.handler;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.times;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
28 import java.util.List;
29 import org.junit.jupiter.api.Test;
30 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantStatusReqListener;
31 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
32 import org.onap.policy.common.message.bus.event.Topic;
33 import org.onap.policy.common.message.bus.healthcheck.TopicHealthCheck;
34 import org.onap.policy.common.parameters.topic.TopicParameters;
35 import org.springframework.context.event.ContextClosedEvent;
36 import org.springframework.context.event.ContextRefreshedEvent;
38 class BrokerStarterTest {
41 void testWithClampAdminTopicsNull() {
42 var parameters = CommonTestData.getParticipantParameters();
43 parameters.getIntermediaryParameters().setClampAdminTopics(null);
44 var publishers = List.of(mock(Publisher.class));
45 var listeners = List.of(mock(ParticipantStatusReqListener.class));
46 var activator = mock(IntermediaryActivator.class);
47 var participantHandler = mock(ParticipantHandler.class);
48 var brokerStarter = new BrokerStarter(parameters, publishers, listeners, activator, participantHandler);
49 when(activator.isAlive()).thenReturn(false);
51 brokerStarter.handleContextRefreshEvent(mock(ContextRefreshedEvent.class));
52 verify(activator).start();
54 brokerStarter.handleContextClosedEvent(mock(ContextClosedEvent.class));
55 verify(participantHandler, times(0)).sendParticipantDeregister();
56 verify(activator, times(0)).stop();
60 void testAlreadyAlive() {
61 var parameters = CommonTestData.getParticipantParameters();
62 var topic = new TopicParameters();
63 topic.setTopicCommInfrastructure(Topic.CommInfrastructure.NOOP.name());
64 parameters.getIntermediaryParameters().setClampAdminTopics(topic);
65 var publishers = List.of(mock(Publisher.class));
66 var listeners = List.of(mock(ParticipantStatusReqListener.class));
67 var activator = mock(IntermediaryActivator.class);
68 var participantHandler = mock(ParticipantHandler.class);
69 var brokerStarter = new BrokerStarter(parameters, publishers, listeners, activator, participantHandler);
71 when(activator.isAlive()).thenReturn(true);
72 brokerStarter.handleContextRefreshEvent(mock(ContextRefreshedEvent.class));
73 verify(activator, times(0)).start();
75 brokerStarter.handleContextClosedEvent(mock(ContextClosedEvent.class));
76 verify(activator).stop();
77 verify(participantHandler).sendParticipantDeregister();
80 private static class DummyTopicHealthCheck implements TopicHealthCheck {
84 // first call is false, next will be true
86 public boolean healthCheck(List<String> list) {
92 void testWithClampAdminTopics() {
93 var parameters = CommonTestData.getParticipantParameters();
94 var topic = new TopicParameters();
95 topic.setTopicCommInfrastructure(Topic.CommInfrastructure.NOOP.name());
96 parameters.getIntermediaryParameters().setClampAdminTopics(topic);
97 var publishers = List.of(mock(Publisher.class));
98 var listeners = List.of(mock(ParticipantStatusReqListener.class));
99 var activator = mock(IntermediaryActivator.class);
100 var participantHandler = mock(ParticipantHandler.class);
101 var topicHealthCheck = new DummyTopicHealthCheck();
102 var brokerStarter = new BrokerStarter(parameters, publishers, listeners, activator, participantHandler) {
104 protected TopicHealthCheck createTopicHealthCheck(TopicParameters topic) {
105 return topicHealthCheck;
109 when(activator.isAlive()).thenReturn(false);
110 brokerStarter.handleContextRefreshEvent(mock(ContextRefreshedEvent.class));
111 verify(activator).start();