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.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;
57 class ParticipantCommTest {
60 void participantListenerTest() {
61 var participantHandler = mock(ParticipantHandler.class);
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());
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());
75 var participantDeregisterAckListener = new ParticipantDeregisterAckListener(participantHandler);
76 assertEquals(ParticipantMessageType.PARTICIPANT_DEREGISTER_ACK.name(),
77 participantDeregisterAckListener.getType());
79 var participantPrimeListener = new ParticipantPrimeListener(participantHandler);
80 assertEquals(ParticipantMessageType.PARTICIPANT_PRIME.name(), participantPrimeListener.getType());
81 assertTrue(participantPrimeListener.isDefaultTopic());
83 var acPropertyUpdateListener = new AcPropertyUpdateListener(participantHandler);
84 assertEquals(ParticipantMessageType.PROPERTIES_UPDATE.name(), acPropertyUpdateListener.getType());
86 var automationCompositionUpdateListener = new AutomationCompositionDeployListener(participantHandler);
87 assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY.name(),
88 automationCompositionUpdateListener.getType());
90 var automationCompositionStateChangeListener = new AutomationCompositionStateChangeListener(participantHandler);
91 assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE.name(),
92 automationCompositionStateChangeListener.getType());
94 var participantSyncListener = new ParticipantSyncListener(participantHandler);
95 assertEquals(ParticipantMessageType.PARTICIPANT_SYNC_MSG.name(),
96 participantSyncListener.getType());
97 assertFalse(participantSyncListener.isDefaultTopic());
99 var acMigrationListener = new AutomationCompositionMigrationListener(participantHandler);
100 assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_MIGRATION.name(), acMigrationListener.getType());
102 var acPrepareListener = new AcPrepareListener(participantHandler);
103 assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_PREPARE.name(), acPrepareListener.getType());
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));
113 var participantStatus = new ParticipantStatus();
114 assertDoesNotThrow(() -> publisher.sendParticipantStatus(participantStatus));
115 verify(mockTopicSink).send(coder.encode(participantStatus));
117 var participantRegister = new ParticipantRegister();
118 assertDoesNotThrow(() -> publisher.sendParticipantRegister(participantRegister));
119 verify(mockTopicSink).send(coder.encode(participantRegister));
121 var participantDeregister = new ParticipantDeregister();
122 assertDoesNotThrow(() -> publisher.sendParticipantDeregister(participantDeregister));
123 verify(mockTopicSink).send(coder.encode(participantDeregister));
125 var participantPrimeAck = new ParticipantPrimeAck();
126 assertDoesNotThrow(() -> publisher.sendParticipantPrimeAck(participantPrimeAck));
127 verify(mockTopicSink).send(coder.encode(participantPrimeAck));
129 var automationCompositionAck =
130 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY);
131 assertDoesNotThrow(() -> publisher.sendAutomationCompositionAck(automationCompositionAck));
132 verify(mockTopicSink).send(coder.encode(automationCompositionAck));
134 var participantReqSync = new ParticipantReqSync();
135 assertDoesNotThrow(() -> publisher.sendParticipantReqSync(participantReqSync));
136 verify(mockTopicSink).send(coder.encode(participantReqSync));
140 void participantMessagePublisherExceptionsTest() {
141 var publisher = new ParticipantMessagePublisher();
143 var participantStatus = new ParticipantStatus();
144 assertThrows(AutomationCompositionRuntimeException.class,
145 () -> publisher.sendParticipantStatus(participantStatus));
147 var participantRegister = new ParticipantRegister();
148 assertThrows(AutomationCompositionRuntimeException.class,
149 () -> publisher.sendParticipantRegister(participantRegister));
151 var participantDeregister = new ParticipantDeregister();
152 assertThrows(AutomationCompositionRuntimeException.class,
153 () -> publisher.sendParticipantDeregister(participantDeregister));
155 var automationCompositionAck = mock(AutomationCompositionDeployAck.class);
156 assertThrows(AutomationCompositionRuntimeException.class,
157 () -> publisher.sendAutomationCompositionAck(automationCompositionAck));
159 List<TopicSink> emptyList = Collections.emptyList();
160 assertThrows(IllegalArgumentException.class, () -> publisher.active(emptyList));
166 void messageSenderTest() {
167 var participantHandler = mock(ParticipantHandler.class);
168 var participantParameters = CommonTestData.getParticipantParameters();
169 var messageSender = new MessageSender(participantHandler, participantParameters);
170 messageSender.handleContextRefreshEvent(null);
172 assertFalse(messageSender.makeTimerPool().isTerminated());
173 messageSender.close();
177 void testOnTopicEvent() {
178 ParticipantHandler handler = Mockito.mock(ParticipantHandler.class);
179 Consumer<ParticipantMessage> consumer = Mockito.mock(Consumer.class);
180 ParticipantMessage message = Mockito.mock(ParticipantMessage.class);
182 Mockito.when(handler.appliesTo(message)).thenReturn(true);
184 ParticipantListener<ParticipantMessage> listener =
185 new ParticipantListener<>(ParticipantMessage.class, handler, consumer) {
187 public String getType() {
191 assertNotNull(listener);
192 listener.onTopicEvent(Mockito.mock(Topic.CommInfrastructure.class),
193 "topic", Mockito.mock(StandardCoderObject.class), message);
194 Mockito.verify(handler).appliesTo(message);