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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.participant.intermediary.comm;
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;
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;
56 class ParticipantCommTest {
59 void participantListenerTest() {
60 var participantHandler = mock(ParticipantHandler.class);
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());
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());
74 var participantDeregisterAckListener = new ParticipantDeregisterAckListener(participantHandler);
75 assertEquals(ParticipantMessageType.PARTICIPANT_DEREGISTER_ACK.name(),
76 participantDeregisterAckListener.getType());
78 var participantPrimeListener = new ParticipantPrimeListener(participantHandler);
79 assertEquals(ParticipantMessageType.PARTICIPANT_PRIME.name(), participantPrimeListener.getType());
80 assertTrue(participantPrimeListener.isDefaultTopic());
82 var acPropertyUpdateListener = new AcPropertyUpdateListener(participantHandler);
83 assertEquals(ParticipantMessageType.PROPERTIES_UPDATE.name(), acPropertyUpdateListener.getType());
85 var automationCompositionUpdateListener = new AutomationCompositionDeployListener(participantHandler);
86 assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY.name(),
87 automationCompositionUpdateListener.getType());
89 var automationCompositionStateChangeListener = new AutomationCompositionStateChangeListener(participantHandler);
90 assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE.name(),
91 automationCompositionStateChangeListener.getType());
93 var participantSyncListener = new ParticipantSyncListener(participantHandler);
94 assertEquals(ParticipantMessageType.PARTICIPANT_SYNC_MSG.name(),
95 participantSyncListener.getType());
96 assertFalse(participantSyncListener.isDefaultTopic());
98 var acMigrationListener = new AutomationCompositionMigrationListener(participantHandler);
99 assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_MIGRATION.name(), acMigrationListener.getType());
101 var acPrepareListener = new AcPrepareListener(participantHandler);
102 assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_PREPARE.name(), acPrepareListener.getType());
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));
112 var participantStatus = new ParticipantStatus();
113 assertDoesNotThrow(() -> publisher.sendParticipantStatus(participantStatus));
114 verify(mockTopicSink).send(coder.encode(participantStatus));
116 var participantRegister = new ParticipantRegister();
117 assertDoesNotThrow(() -> publisher.sendParticipantRegister(participantRegister));
118 verify(mockTopicSink).send(coder.encode(participantRegister));
120 var participantDeregister = new ParticipantDeregister();
121 assertDoesNotThrow(() -> publisher.sendParticipantDeregister(participantDeregister));
122 verify(mockTopicSink).send(coder.encode(participantDeregister));
124 var participantPrimeAck = new ParticipantPrimeAck();
125 assertDoesNotThrow(() -> publisher.sendParticipantPrimeAck(participantPrimeAck));
126 verify(mockTopicSink).send(coder.encode(participantPrimeAck));
128 var automationCompositionAck =
129 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY);
130 assertDoesNotThrow(() -> publisher.sendAutomationCompositionAck(automationCompositionAck));
131 verify(mockTopicSink).send(coder.encode(automationCompositionAck));
133 var participantReqSync = new ParticipantReqSync();
134 assertDoesNotThrow(() -> publisher.sendParticipantReqSync(participantReqSync));
135 verify(mockTopicSink).send(coder.encode(participantReqSync));
139 void participantMessagePublisherExceptionsTest() {
140 var publisher = new ParticipantMessagePublisher();
142 var participantStatus = new ParticipantStatus();
143 assertThrows(AutomationCompositionRuntimeException.class,
144 () -> publisher.sendParticipantStatus(participantStatus));
146 var participantRegister = new ParticipantRegister();
147 assertThrows(AutomationCompositionRuntimeException.class,
148 () -> publisher.sendParticipantRegister(participantRegister));
150 var participantDeregister = new ParticipantDeregister();
151 assertThrows(AutomationCompositionRuntimeException.class,
152 () -> publisher.sendParticipantDeregister(participantDeregister));
154 var automationCompositionAck = mock(AutomationCompositionDeployAck.class);
155 assertThrows(AutomationCompositionRuntimeException.class,
156 () -> publisher.sendAutomationCompositionAck(automationCompositionAck));
158 List<TopicSink> emptyList = Collections.emptyList();
159 assertThrows(IllegalArgumentException.class, () -> publisher.active(emptyList));
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);
171 assertFalse(messageSender.makeTimerPool().isTerminated());
172 messageSender.close();
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);
181 Mockito.when(handler.appliesTo(message)).thenReturn(true);
183 ParticipantListener<ParticipantMessage> listener =
184 new ParticipantListener<>(ParticipantMessage.class, handler, consumer) {
186 public String getType() {
190 assertNotNull(listener);
191 listener.onTopicEvent(Mockito.mock(Topic.CommInfrastructure.class),
192 "topic", Mockito.mock(StandardCoderObject.class), message);
193 Mockito.verify(handler).appliesTo(message);