4e51cc518cc860365c997c5a82816c53ff23bb9a
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 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.spy;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
34
35 import java.util.List;
36 import org.junit.jupiter.api.Disabled;
37 import org.junit.jupiter.api.Test;
38 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantStatusReqListener;
39 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
40 import org.onap.policy.clamp.acm.participant.intermediary.parameters.ParticipantParameters;
41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatusReq;
42 import org.onap.policy.common.utils.coder.Coder;
43 import org.onap.policy.common.utils.coder.StandardCoder;
44 import org.onap.policy.common.utils.coder.StandardCoderObject;
45 import org.springframework.context.event.ContextClosedEvent;
46 import org.springframework.context.event.ContextRefreshedEvent;
47
48 class IntermediaryActivatorTest {
49     private static final Coder CODER = new StandardCoder();
50
51     private static final String TOPIC_FIRST = "TOPIC1";
52     private static final String TOPIC_SECOND = "TOPIC2";
53
54     @Disabled
55     @Test
56     void testStartAndStop() throws Exception {
57         ParticipantParameters parameters = CommonTestData.getParticipantParameters();
58
59         var publisherFirst = spy(mock(Publisher.class));
60         var publisherSecond = spy(mock(Publisher.class));
61         var publishers = List.of(publisherFirst, publisherSecond);
62
63         var listenerFirst = spy(mock(ParticipantStatusReqListener.class));
64         when(listenerFirst.getType()).thenReturn(TOPIC_FIRST);
65         when(listenerFirst.getScoListener()).thenReturn(listenerFirst);
66
67         var listenerSecond = spy(mock(ParticipantStatusReqListener.class));
68         when(listenerSecond.getType()).thenReturn(TOPIC_SECOND);
69         when(listenerSecond.getScoListener()).thenReturn(listenerSecond);
70
71         List<Listener<ParticipantStatusReq>> listeners = List.of(listenerFirst, listenerSecond);
72
73         ParticipantHandler handler = mock(ParticipantHandler.class);
74         try (var activator = new IntermediaryActivator(parameters, handler, publishers, listeners)) {
75
76             assertFalse(activator.isAlive());
77             activator.start();
78             assertTrue(activator.isAlive());
79
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());
85
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());
89
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());
93
94             activator.close();
95             assertFalse(activator.isAlive());
96
97             // repeat stop - should throw an exception
98             assertThatIllegalStateException().isThrownBy(activator::stop);
99             assertFalse(activator.isAlive());
100
101             assertDoesNotThrow(() -> activator.handleContextRefreshEvent(mock(ContextRefreshedEvent.class)));
102             assertDoesNotThrow(() -> activator.handleContextClosedEvent(mock(ContextClosedEvent.class)));
103         }
104     }
105 }