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