2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.controlloop.runtime.config.messaging;
24 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
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.spy;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
35 import java.util.List;
36 import org.junit.jupiter.api.Test;
37 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
38 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
39 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantStatusListener;
40 import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData;
41 import org.onap.policy.common.utils.coder.Coder;
42 import org.onap.policy.common.utils.coder.StandardCoder;
43 import org.onap.policy.common.utils.coder.StandardCoderObject;
46 * Class to perform unit test of {@link MessageDispatcherActivator}}.
49 class MessageDispatcherActivatorTest {
51 private static final Coder CODER = new StandardCoder();
53 private static final String TOPIC_FIRST = "TOPIC1";
54 private static final String TOPIC_SECOND = "TOPIC2";
57 void testStartAndStop() throws Exception {
58 ClRuntimeParameterGroup parameterGroup = CommonTestData.geParameterGroup("dbtest");
60 var publisherFirst = spy(mock(Publisher.class));
61 var publisherSecond = spy(mock(Publisher.class));
62 var publishers = List.of(publisherFirst, publisherSecond);
64 var listenerFirst = spy(mock(ParticipantStatusListener.class));
65 when(listenerFirst.getType()).thenReturn(TOPIC_FIRST);
66 when(listenerFirst.getScoListener()).thenReturn(listenerFirst);
68 var listenerSecond = spy(mock(ParticipantStatusListener.class));
69 when(listenerSecond.getType()).thenReturn(TOPIC_SECOND);
70 when(listenerSecond.getScoListener()).thenReturn(listenerSecond);
72 List<Listener<ParticipantStatus>> listeners = List.of(listenerFirst, listenerSecond);
74 try (var activator = new MessageDispatcherActivator(parameterGroup, publishers, listeners)) {
76 assertFalse(activator.isAlive());
78 assertTrue(activator.isAlive());
80 // repeat start - should throw an exception
81 assertThatIllegalStateException().isThrownBy(() -> activator.start());
82 assertTrue(activator.isAlive());
83 verify(publisherFirst, times(1)).active(anyList());
84 verify(publisherSecond, times(1)).active(anyList());
86 StandardCoderObject sco = CODER.decode("{messageType:" + TOPIC_FIRST + "}", StandardCoderObject.class);
87 activator.getMsgDispatcher().onTopicEvent(null, "msg", sco);
88 verify(listenerFirst, times(1)).onTopicEvent(any(), any(), any());
90 sco = CODER.decode("{messageType:" + TOPIC_SECOND + "}", StandardCoderObject.class);
91 activator.getMsgDispatcher().onTopicEvent(null, "msg", sco);
92 verify(listenerSecond, times(1)).onTopicEvent(any(), any(), any());
95 assertFalse(activator.isAlive());
97 // repeat stop - should throw an exception
98 assertThatIllegalStateException().isThrownBy(() -> activator.stop());
99 assertFalse(activator.isAlive());