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