63baef24dbd0265e1a4fa4d3cddff0b9ea4d6686
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2025 OpenInfra Foundation Europe. All rights reserved.
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.comm;
22
23 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.assertThrows;
28 import static org.junit.jupiter.api.Assertions.assertTrue;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.verify;
31
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.function.Consumer;
35 import org.junit.jupiter.api.Test;
36 import org.mockito.Mockito;
37 import org.onap.policy.clamp.acm.participant.intermediary.handler.ParticipantHandler;
38 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
39 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionRuntimeException;
40 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionDeployAck;
41 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregister;
42 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessage;
43 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessageType;
44 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrimeAck;
45 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegister;
46 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegisterAck;
47 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantReqSync;
48 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatus;
49 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatusReq;
50 import org.onap.policy.common.message.bus.event.Topic;
51 import org.onap.policy.common.message.bus.event.TopicSink;
52 import org.onap.policy.common.utils.coder.Coder;
53 import org.onap.policy.common.utils.coder.CoderException;
54 import org.onap.policy.common.utils.coder.StandardCoder;
55 import org.onap.policy.common.utils.coder.StandardCoderObject;
56
57 class ParticipantCommTest {
58
59     @Test
60     void participantListenerTest() {
61         var participantHandler = mock(ParticipantHandler.class);
62
63         var participantRegisterAckListener = new ParticipantRegisterAckListener(participantHandler);
64         participantRegisterAckListener.onTopicEvent(null, null, null, new ParticipantRegisterAck());
65         assertEquals(ParticipantMessageType.PARTICIPANT_REGISTER_ACK.name(), participantRegisterAckListener.getType());
66         assertFalse(participantRegisterAckListener.isDefaultTopic());
67         assertEquals(participantRegisterAckListener, participantRegisterAckListener.getScoListener());
68
69         var participantStatusReqListener = new ParticipantStatusReqListener(participantHandler);
70         participantStatusReqListener.onTopicEvent(null, null, null, new ParticipantStatusReq());
71         assertEquals(ParticipantMessageType.PARTICIPANT_STATUS_REQ.name(), participantStatusReqListener.getType());
72         assertEquals(participantStatusReqListener, participantStatusReqListener.getScoListener());
73         assertFalse(participantStatusReqListener.isDefaultTopic());
74
75         var participantDeregisterAckListener = new ParticipantDeregisterAckListener(participantHandler);
76         assertEquals(ParticipantMessageType.PARTICIPANT_DEREGISTER_ACK.name(),
77                 participantDeregisterAckListener.getType());
78
79         var participantPrimeListener = new ParticipantPrimeListener(participantHandler);
80         assertEquals(ParticipantMessageType.PARTICIPANT_PRIME.name(), participantPrimeListener.getType());
81         assertTrue(participantPrimeListener.isDefaultTopic());
82
83         var acPropertyUpdateListener = new AcPropertyUpdateListener(participantHandler);
84         assertEquals(ParticipantMessageType.PROPERTIES_UPDATE.name(), acPropertyUpdateListener.getType());
85
86         var automationCompositionUpdateListener = new AutomationCompositionDeployListener(participantHandler);
87         assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY.name(),
88                 automationCompositionUpdateListener.getType());
89
90         var automationCompositionStateChangeListener = new AutomationCompositionStateChangeListener(participantHandler);
91         assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE.name(),
92                 automationCompositionStateChangeListener.getType());
93
94         var participantSyncListener = new ParticipantSyncListener(participantHandler);
95         assertEquals(ParticipantMessageType.PARTICIPANT_SYNC_MSG.name(),
96                 participantSyncListener.getType());
97         assertFalse(participantSyncListener.isDefaultTopic());
98
99         var acMigrationListener = new AutomationCompositionMigrationListener(participantHandler);
100         assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_MIGRATION.name(), acMigrationListener.getType());
101
102         var acPrepareListener = new AcPrepareListener(participantHandler);
103         assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_PREPARE.name(), acPrepareListener.getType());
104     }
105
106     @Test
107     void participantMessagePublisherTest() throws CoderException {
108         var coder = new StandardCoder();
109         var mockTopicSink = mock(TopicSink.class);
110         var publisher = new ParticipantMessagePublisher();
111         publisher.active(Collections.singletonList(mockTopicSink));
112
113         var participantStatus = new ParticipantStatus();
114         assertDoesNotThrow(() -> publisher.sendParticipantStatus(participantStatus));
115         verify(mockTopicSink).send(coder.encode(participantStatus));
116
117         var participantRegister = new ParticipantRegister();
118         assertDoesNotThrow(() -> publisher.sendParticipantRegister(participantRegister));
119         verify(mockTopicSink).send(coder.encode(participantRegister));
120
121         var participantDeregister = new ParticipantDeregister();
122         assertDoesNotThrow(() -> publisher.sendParticipantDeregister(participantDeregister));
123         verify(mockTopicSink).send(coder.encode(participantDeregister));
124
125         var participantPrimeAck = new ParticipantPrimeAck();
126         assertDoesNotThrow(() -> publisher.sendParticipantPrimeAck(participantPrimeAck));
127         verify(mockTopicSink).send(coder.encode(participantPrimeAck));
128
129         var automationCompositionAck = new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY);
130         assertDoesNotThrow(() -> publisher.sendAutomationCompositionAck(automationCompositionAck));
131         verify(mockTopicSink).send(coder.encode(automationCompositionAck));
132
133         var participantReqSync = new ParticipantReqSync();
134         assertDoesNotThrow(() -> publisher.sendParticipantReqSync(participantReqSync));
135         verify(mockTopicSink).send(coder.encode(participantReqSync));
136     }
137
138     @Test
139     void participantMessagePublisherExceptionsTest() {
140         var publisher = new ParticipantMessagePublisher();
141
142         var participantStatus = new ParticipantStatus();
143         assertThrows(AutomationCompositionRuntimeException.class,
144                 () -> publisher.sendParticipantStatus(participantStatus));
145
146         var participantRegister = new ParticipantRegister();
147         assertThrows(AutomationCompositionRuntimeException.class,
148                 () -> publisher.sendParticipantRegister(participantRegister));
149
150         var participantDeregister = new ParticipantDeregister();
151         assertThrows(AutomationCompositionRuntimeException.class,
152                 () -> publisher.sendParticipantDeregister(participantDeregister));
153
154         var automationCompositionAck = mock(AutomationCompositionDeployAck.class);
155         assertThrows(AutomationCompositionRuntimeException.class,
156                 () -> publisher.sendAutomationCompositionAck(automationCompositionAck));
157
158         List<TopicSink> emptyList = Collections.emptyList();
159         assertThrows(IllegalArgumentException.class, () -> publisher.active(emptyList));
160
161         publisher.stop();
162     }
163
164     @Test
165     void messageSenderTest() {
166         var participantHandler = mock(ParticipantHandler.class);
167         var participantParameters = CommonTestData.getParticipantParameters();
168         var messageSender = new MessageSender(participantHandler, participantParameters);
169         messageSender.handleContextRefreshEvent(null);
170         messageSender.run();
171         assertFalse(messageSender.makeTimerPool().isTerminated());
172         messageSender.close();
173     }
174
175     @Test
176     void testOnTopicEvent() {
177         ParticipantHandler handler = Mockito.mock(ParticipantHandler.class);
178         Consumer<ParticipantMessage> consumer = Mockito.mock(Consumer.class);
179         ParticipantMessage message = Mockito.mock(ParticipantMessage.class);
180
181         Mockito.when(handler.appliesTo(message)).thenReturn(true);
182
183         ParticipantListener<ParticipantMessage> listener =
184                 new ParticipantListener<>(ParticipantMessage.class, handler, consumer) {
185                 @Override
186                 public String getType() {
187                     return "";
188                 }
189             };
190         assertNotNull(listener);
191         listener.onTopicEvent(Mockito.mock(Topic.CommInfrastructure.class),
192                 "topic", Mockito.mock(StandardCoderObject.class), message);
193         Mockito.verify(handler).appliesTo(message);
194     }
195 }