093ae9e90487c2145a434ad5d59b7cbdab0e9f9e
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.participant.intermediary.handler;
22
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;
27
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;
37
38 class BrokerStarterTest {
39
40     @Test
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);
50
51         brokerStarter.handleContextRefreshEvent(mock(ContextRefreshedEvent.class));
52         verify(activator).start();
53
54         brokerStarter.handleContextClosedEvent(mock(ContextClosedEvent.class));
55         verify(participantHandler, times(0)).sendParticipantDeregister();
56         verify(activator, times(0)).stop();
57     }
58
59     @Test
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);
70
71         when(activator.isAlive()).thenReturn(true);
72         brokerStarter.handleContextRefreshEvent(mock(ContextRefreshedEvent.class));
73         verify(activator, times(0)).start();
74
75         brokerStarter.handleContextClosedEvent(mock(ContextClosedEvent.class));
76         verify(activator).stop();
77         verify(participantHandler).sendParticipantDeregister();
78     }
79
80     private static class DummyTopicHealthCheck implements TopicHealthCheck {
81
82         int count = 0;
83
84         // first call is false, next will be true
85         @Override
86         public boolean healthCheck(List<String> list) {
87             return (count++) > 0;
88         }
89     }
90
91     @Test
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) {
103             @Override
104             protected TopicHealthCheck createTopicHealthCheck(TopicParameters topic) {
105                 return topicHealthCheck;
106             }
107         };
108
109         when(activator.isAlive()).thenReturn(false);
110         brokerStarter.handleContextRefreshEvent(mock(ContextRefreshedEvent.class));
111         verify(activator).start();
112     }
113 }