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