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