1540d6e4c46a8fd86654129becc4c06bb8a2b4c4
[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.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26
27 import java.util.Collections;
28 import java.util.List;
29 import org.junit.jupiter.api.Test;
30 import org.mockito.Mockito;
31 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
32 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionRuntimeException;
33 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeployAck;
34 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
35 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
36 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
37 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
38 import org.onap.policy.common.endpoints.event.comm.TopicSink;
39 import org.onap.policy.common.utils.coder.CoderException;
40
41 class ParticipantCommTest {
42
43     private final CommonTestData commonTestData = new CommonTestData();
44
45     @Test
46     void participantReqTest() throws CoderException {
47         var participantHandler = commonTestData.getParticipantHandlerAutomationCompositions();
48
49         var participantRegisterAckListener = new ParticipantRegisterAckListener(participantHandler);
50         assertEquals(ParticipantMessageType.PARTICIPANT_REGISTER_ACK.name(), participantRegisterAckListener.getType());
51
52         var participantStatusReqListener = new ParticipantStatusReqListener(participantHandler);
53         assertEquals(ParticipantMessageType.PARTICIPANT_STATUS_REQ.name(), participantStatusReqListener.getType());
54
55         var participantDeregisterAckListener = new ParticipantDeregisterAckListener(participantHandler);
56         assertEquals(ParticipantMessageType.PARTICIPANT_DEREGISTER_ACK.name(),
57             participantDeregisterAckListener.getType());
58
59         var participantPrimeListener = new ParticipantPrimeListener(participantHandler);
60         assertEquals(ParticipantMessageType.PARTICIPANT_PRIME.name(), participantPrimeListener.getType());
61
62         var automationCompositionUpdateListener = new AutomationCompositionDeployListener(participantHandler);
63         assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY.name(),
64             automationCompositionUpdateListener.getType());
65
66         var automationCompositionStateChangeListener = new AutomationCompositionStateChangeListener(participantHandler);
67         assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE.name(),
68             automationCompositionStateChangeListener.getType());
69     }
70
71     @Test
72     void participantMessagePublisherExceptionsTest() {
73         var participantMessagePublisher = new ParticipantMessagePublisher();
74
75         var participantStatus = Mockito.mock(ParticipantStatus.class);
76         assertThrows(AutomationCompositionRuntimeException.class,
77             () -> participantMessagePublisher.sendParticipantStatus(participantStatus));
78         assertThrows(AutomationCompositionRuntimeException.class,
79             () -> participantMessagePublisher.sendHeartbeat(participantStatus));
80
81         var participantRegister = Mockito.mock(ParticipantRegister.class);
82         assertThrows(AutomationCompositionRuntimeException.class,
83             () -> participantMessagePublisher.sendParticipantRegister(participantRegister));
84
85         var participantDeregister = Mockito.mock(ParticipantDeregister.class);
86         assertThrows(AutomationCompositionRuntimeException.class,
87             () -> participantMessagePublisher.sendParticipantDeregister(participantDeregister));
88
89         var automationCompositionAck = Mockito.mock(AutomationCompositionDeployAck.class);
90         assertThrows(AutomationCompositionRuntimeException.class,
91             () -> participantMessagePublisher.sendAutomationCompositionAck(automationCompositionAck));
92
93         List<TopicSink> emptyList = Collections.emptyList();
94         assertThrows(IllegalArgumentException.class, () -> participantMessagePublisher.active(emptyList));
95
96         participantMessagePublisher.stop();
97     }
98
99     @Test
100     void messageSenderTest() throws CoderException {
101         var participantHandler = commonTestData.getParticipantHandlerAutomationCompositions();
102         var participantParameters = CommonTestData.getParticipantParameters();
103         var messageSender = new MessageSender(participantHandler, participantParameters);
104         messageSender.run();
105         assertFalse(messageSender.makeTimerPool().isTerminated());
106         messageSender.close();
107     }
108
109 }