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