2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2024 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
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.assertThrows;
27 import static org.mockito.Mockito.mock;
29 import java.util.Collections;
30 import java.util.List;
31 import org.junit.jupiter.api.Test;
32 import org.mockito.Mockito;
33 import org.onap.policy.clamp.acm.participant.intermediary.handler.ParticipantHandler;
34 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
35 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionRuntimeException;
36 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionDeployAck;
37 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregister;
38 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessageType;
39 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrimeAck;
40 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegister;
41 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegisterAck;
42 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatus;
43 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatusReq;
44 import org.onap.policy.common.endpoints.event.comm.TopicSink;
46 class ParticipantCommTest {
49 void participantListenerTest() {
50 var participantHandler = mock(ParticipantHandler.class);
52 var participantRegisterAckListener = new ParticipantRegisterAckListener(participantHandler);
53 participantRegisterAckListener.onTopicEvent(null, null, null, new ParticipantRegisterAck());
54 assertEquals(ParticipantMessageType.PARTICIPANT_REGISTER_ACK.name(), participantRegisterAckListener.getType());
55 assertEquals(participantRegisterAckListener, participantRegisterAckListener.getScoListener());
57 var participantStatusReqListener = new ParticipantStatusReqListener(participantHandler);
58 participantStatusReqListener.onTopicEvent(null, null, null, new ParticipantStatusReq());
59 assertEquals(ParticipantMessageType.PARTICIPANT_STATUS_REQ.name(), participantStatusReqListener.getType());
60 assertEquals(participantStatusReqListener, participantStatusReqListener.getScoListener());
62 var participantDeregisterAckListener = new ParticipantDeregisterAckListener(participantHandler);
63 assertEquals(ParticipantMessageType.PARTICIPANT_DEREGISTER_ACK.name(),
64 participantDeregisterAckListener.getType());
66 var participantPrimeListener = new ParticipantPrimeListener(participantHandler);
67 assertEquals(ParticipantMessageType.PARTICIPANT_PRIME.name(), participantPrimeListener.getType());
69 var acPropertyUpdateListener = new AcPropertyUpdateListener(participantHandler);
70 assertEquals(ParticipantMessageType.PROPERTIES_UPDATE.name(), acPropertyUpdateListener.getType());
72 var automationCompositionUpdateListener = new AutomationCompositionDeployListener(participantHandler);
73 assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY.name(),
74 automationCompositionUpdateListener.getType());
76 var automationCompositionStateChangeListener = new AutomationCompositionStateChangeListener(participantHandler);
77 assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE.name(),
78 automationCompositionStateChangeListener.getType());
80 var participantRestartListener = new ParticipantRestartListener(participantHandler);
81 assertEquals(ParticipantMessageType.PARTICIPANT_RESTART.name(),
82 participantRestartListener.getType());
84 var participantSyncListener = new ParticipantSyncListener(participantHandler);
85 assertEquals(ParticipantMessageType.PARTICIPANT_SYNC_MSG.name(),
86 participantSyncListener.getType());
88 var acMigrationListener = new AutomationCompositionMigrationListener(participantHandler);
89 assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_MIGRATION.name(), acMigrationListener.getType());
93 void participantMessagePublisherTest() {
94 var publisher = new ParticipantMessagePublisher();
95 publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
96 var participantStatus = new ParticipantStatus();
97 assertDoesNotThrow(() -> publisher.sendParticipantStatus(participantStatus));
99 var participantRegister = new ParticipantRegister();
100 assertDoesNotThrow(() -> publisher.sendParticipantRegister(participantRegister));
102 var participantDeregister = new ParticipantDeregister();
103 assertDoesNotThrow(() -> publisher.sendParticipantDeregister(participantDeregister));
105 var participantPrimeAck = new ParticipantPrimeAck();
106 assertDoesNotThrow(() -> publisher.sendParticipantPrimeAck(participantPrimeAck));
108 var automationCompositionAck = mock(AutomationCompositionDeployAck.class);
109 assertDoesNotThrow(() -> publisher.sendAutomationCompositionAck(automationCompositionAck));
113 void participantMessagePublisherExceptionsTest() {
114 var publisher = new ParticipantMessagePublisher();
116 var participantStatus = new ParticipantStatus();
117 assertThrows(AutomationCompositionRuntimeException.class,
118 () -> publisher.sendParticipantStatus(participantStatus));
120 var participantRegister = new ParticipantRegister();
121 assertThrows(AutomationCompositionRuntimeException.class,
122 () -> publisher.sendParticipantRegister(participantRegister));
124 var participantDeregister = new ParticipantDeregister();
125 assertThrows(AutomationCompositionRuntimeException.class,
126 () -> publisher.sendParticipantDeregister(participantDeregister));
128 var automationCompositionAck = mock(AutomationCompositionDeployAck.class);
129 assertThrows(AutomationCompositionRuntimeException.class,
130 () -> publisher.sendAutomationCompositionAck(automationCompositionAck));
132 List<TopicSink> emptyList = Collections.emptyList();
133 assertThrows(IllegalArgumentException.class, () -> publisher.active(emptyList));
139 void messageSenderTest() {
140 var participantHandler = mock(ParticipantHandler.class);
141 var participantParameters = CommonTestData.getParticipantParameters();
142 var messageSender = new MessageSender(participantHandler, participantParameters);
143 messageSender.handleContextRefreshEvent(null);
145 assertFalse(messageSender.makeTimerPool().isTerminated());
146 messageSender.close();