461c8b558be606b45a4f309611bc03e09a645edd
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.controlloop.runtime.config.messaging;
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.spy;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
36 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantStatusListener;
37 import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData;
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 /**
43  * Class to perform unit test of {@link MessageDispatcherActivator}}.
44  *
45  */
46 class MessageDispatcherActivatorTest {
47
48     private static final Coder CODER = new StandardCoder();
49
50     private static final String TOPIC_FIRST = "TOPIC1";
51     private static final String TOPIC_SECOND = "TOPIC2";
52
53     @Test
54     void testStartAndStop() throws Exception {
55         ClRuntimeParameterGroup parameterGroup = CommonTestData.geParameterGroup("dbtest");
56
57         var publisherFirst = spy(mock(Publisher.class));
58         var publisherSecond = spy(mock(Publisher.class));
59         var publishers = new Publisher[] {publisherFirst, publisherSecond};
60
61         var listenerFirst = spy(mock(ParticipantStatusListener.class));
62         when(listenerFirst.getType()).thenReturn(TOPIC_FIRST);
63         when(listenerFirst.getScoListener()).thenReturn(listenerFirst);
64
65         var listenerSecond = spy(mock(ParticipantStatusListener.class));
66         when(listenerSecond.getType()).thenReturn(TOPIC_SECOND);
67         when(listenerSecond.getScoListener()).thenReturn(listenerSecond);
68
69         var listeners = new Listener[] {listenerFirst, listenerSecond};
70
71         try (var activator = new MessageDispatcherActivator(parameterGroup, publishers, listeners)) {
72
73             assertFalse(activator.isAlive());
74             activator.start();
75             assertTrue(activator.isAlive());
76
77             // repeat start - should throw an exception
78             assertThatIllegalStateException().isThrownBy(() -> activator.start());
79             assertTrue(activator.isAlive());
80             verify(publisherFirst, times(1)).active(anyList());
81             verify(publisherSecond, times(1)).active(anyList());
82
83             StandardCoderObject sco = CODER.decode("{messageType:" + TOPIC_FIRST + "}", StandardCoderObject.class);
84             activator.getMsgDispatcher().onTopicEvent(null, "msg", sco);
85             verify(listenerFirst, times(1)).onTopicEvent(any(), any(), any());
86
87             sco = CODER.decode("{messageType:" + TOPIC_SECOND + "}", StandardCoderObject.class);
88             activator.getMsgDispatcher().onTopicEvent(null, "msg", sco);
89             verify(listenerSecond, times(1)).onTopicEvent(any(), any(), any());
90
91             activator.stop();
92             assertFalse(activator.isAlive());
93
94             // repeat stop - should throw an exception
95             assertThatIllegalStateException().isThrownBy(() -> activator.stop());
96             assertFalse(activator.isAlive());
97         }
98     }
99 }