07c20ee9e5de106e68778d5eec99beab23304acf
[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.CoderException;
53 import org.onap.policy.common.utils.coder.StandardCoder;
54 import org.onap.policy.common.utils.coder.StandardCoderObject;
55
56 class ParticipantCommTest {
57
58     @Test
59     void participantListenerTest() {
60         var participantHandler = mock(ParticipantHandler.class);
61
62         var participantRegisterAckListener = new ParticipantRegisterAckListener(participantHandler);
63         participantRegisterAckListener.onTopicEvent(null, null, null, new ParticipantRegisterAck());
64         assertEquals(ParticipantMessageType.PARTICIPANT_REGISTER_ACK.name(), participantRegisterAckListener.getType());
65         assertFalse(participantRegisterAckListener.isDefaultTopic());
66         assertEquals(participantRegisterAckListener, participantRegisterAckListener.getScoListener());
67
68         var participantStatusReqListener = new ParticipantStatusReqListener(participantHandler);
69         participantStatusReqListener.onTopicEvent(null, null, null, new ParticipantStatusReq());
70         assertEquals(ParticipantMessageType.PARTICIPANT_STATUS_REQ.name(), participantStatusReqListener.getType());
71         assertEquals(participantStatusReqListener, participantStatusReqListener.getScoListener());
72         assertFalse(participantStatusReqListener.isDefaultTopic());
73
74         var participantDeregisterAckListener = new ParticipantDeregisterAckListener(participantHandler);
75         assertEquals(ParticipantMessageType.PARTICIPANT_DEREGISTER_ACK.name(),
76                 participantDeregisterAckListener.getType());
77
78         var participantPrimeListener = new ParticipantPrimeListener(participantHandler);
79         assertEquals(ParticipantMessageType.PARTICIPANT_PRIME.name(), participantPrimeListener.getType());
80         assertTrue(participantPrimeListener.isDefaultTopic());
81
82         var acPropertyUpdateListener = new AcPropertyUpdateListener(participantHandler);
83         assertEquals(ParticipantMessageType.PROPERTIES_UPDATE.name(), acPropertyUpdateListener.getType());
84
85         var automationCompositionUpdateListener = new AutomationCompositionDeployListener(participantHandler);
86         assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY.name(),
87                 automationCompositionUpdateListener.getType());
88
89         var automationCompositionStateChangeListener = new AutomationCompositionStateChangeListener(participantHandler);
90         assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE.name(),
91                 automationCompositionStateChangeListener.getType());
92
93         var participantSyncListener = new ParticipantSyncListener(participantHandler);
94         assertEquals(ParticipantMessageType.PARTICIPANT_SYNC_MSG.name(),
95                 participantSyncListener.getType());
96         assertFalse(participantSyncListener.isDefaultTopic());
97
98         var acMigrationListener = new AutomationCompositionMigrationListener(participantHandler);
99         assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_MIGRATION.name(), acMigrationListener.getType());
100
101         var acPrepareListener = new AcPrepareListener(participantHandler);
102         assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_PREPARE.name(), acPrepareListener.getType());
103     }
104
105     @Test
106     void participantMessagePublisherTest() throws CoderException {
107         var coder = new StandardCoder();
108         var mockTopicSink = mock(TopicSink.class);
109         var publisher = new ParticipantMessagePublisher();
110         publisher.active(Collections.singletonList(mockTopicSink));
111
112         var participantStatus = new ParticipantStatus();
113         assertDoesNotThrow(() -> publisher.sendParticipantStatus(participantStatus));
114         verify(mockTopicSink).send(coder.encode(participantStatus));
115
116         var participantRegister = new ParticipantRegister();
117         assertDoesNotThrow(() -> publisher.sendParticipantRegister(participantRegister));
118         verify(mockTopicSink).send(coder.encode(participantRegister));
119
120         var participantDeregister = new ParticipantDeregister();
121         assertDoesNotThrow(() -> publisher.sendParticipantDeregister(participantDeregister));
122         verify(mockTopicSink).send(coder.encode(participantDeregister));
123
124         var participantPrimeAck = new ParticipantPrimeAck();
125         assertDoesNotThrow(() -> publisher.sendParticipantPrimeAck(participantPrimeAck));
126         verify(mockTopicSink).send(coder.encode(participantPrimeAck));
127
128         var automationCompositionAck =
129                 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 }