89bafa1967334f8625a2fa02224a24ae77f38895
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2024 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.assertj.core.api.Assertions.assertThatIllegalStateException;
24 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.anyList;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import java.util.List;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantStatusReqListener;
37 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
38 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatusReq;
39 import org.onap.policy.common.utils.coder.Coder;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.onap.policy.common.utils.coder.StandardCoderObject;
42 import org.springframework.context.event.ContextClosedEvent;
43 import org.springframework.context.event.ContextRefreshedEvent;
44
45 class IntermediaryActivatorTest {
46     private static final Coder CODER = new StandardCoder();
47
48     private static final String TOPIC_FIRST = "TOPIC1";
49     private static final String TOPIC_SECOND = "TOPIC2";
50
51     @Test
52     void testStartAndStop() throws Exception {
53         var parameters = CommonTestData.getParticipantParameters();
54
55         var publisherFirst = mock(Publisher.class);
56         var publisherSecond = mock(Publisher.class);
57         var publishers = List.of(publisherFirst, publisherSecond);
58
59         var listenerFirst = mock(ParticipantStatusReqListener.class);
60         when(listenerFirst.getType()).thenReturn(TOPIC_FIRST);
61         when(listenerFirst.getScoListener()).thenReturn(listenerFirst);
62         when(listenerFirst.isDefaultTopic()).thenReturn(true);
63
64         var listenerSecond = mock(ParticipantStatusReqListener.class);
65         when(listenerSecond.getType()).thenReturn(TOPIC_SECOND);
66         when(listenerSecond.getScoListener()).thenReturn(listenerSecond);
67         when(listenerSecond.isDefaultTopic()).thenReturn(false);
68
69         List<Listener<ParticipantStatusReq>> listeners = List.of(listenerFirst, listenerSecond);
70
71         var handler = mock(ParticipantHandler.class);
72         try (var activator = new IntermediaryActivator(parameters, handler, publishers, listeners)) {
73
74             assertFalse(activator.isAlive());
75             activator.start();
76             assertTrue(activator.isAlive());
77
78             // repeat start - should throw an exception
79             assertThatIllegalStateException().isThrownBy(activator::start);
80             assertTrue(activator.isAlive());
81             verify(publisherFirst, times(1)).active(anyList());
82             verify(publisherSecond, times(1)).active(anyList());
83
84             var sco = CODER.decode("{messageType:" + TOPIC_FIRST + "}", StandardCoderObject.class);
85             activator.getMsgDispatcher().onTopicEvent(null, "msg", sco);
86             verify(listenerFirst, times(1)).onTopicEvent(any(), any(), any());
87
88             sco = CODER.decode("{messageType:" + TOPIC_SECOND + "}", StandardCoderObject.class);
89             activator.getSyncMsgDispatcher().onTopicEvent(null, "msg", sco);
90             verify(listenerSecond, times(1)).onTopicEvent(any(), any(), any());
91
92             activator.close();
93             assertFalse(activator.isAlive());
94
95             // repeat stop - should throw an exception
96             assertThatIllegalStateException().isThrownBy(activator::stop);
97             assertFalse(activator.isAlive());
98
99             assertDoesNotThrow(() -> activator.handleContextRefreshEvent(mock(ContextRefreshedEvent.class)));
100             assertDoesNotThrow(() -> activator.handleContextClosedEvent(mock(ContextClosedEvent.class)));
101         }
102     }
103 }