26dddc9ba7fc000ee02e3852ae79f897477b249c
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.Assert.assertEquals;
24 import static org.junit.Assert.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.AutomationCompositionAck;
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 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 participantUpdateListener = new ParticipantUpdateListener(participantHandler);
60         assertEquals(ParticipantMessageType.PARTICIPANT_UPDATE.name(), participantUpdateListener.getType());
61
62         var automationCompositionUpdateListener = new AutomationCompositionUpdateListener(participantHandler);
63         assertEquals(ParticipantMessageType.AUTOMATION_COMPOSITION_UPDATE.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         });
79         assertThrows(AutomationCompositionRuntimeException.class, () -> {
80             participantMessagePublisher.sendHeartbeat(participantStatus);
81         });
82
83         var participantRegister = Mockito.mock(ParticipantRegister.class);
84         assertThrows(AutomationCompositionRuntimeException.class, () -> {
85             participantMessagePublisher.sendParticipantRegister(participantRegister);
86         });
87
88         var participantDeregister = Mockito.mock(ParticipantDeregister.class);
89         assertThrows(AutomationCompositionRuntimeException.class, () -> {
90             participantMessagePublisher.sendParticipantDeregister(participantDeregister);
91         });
92
93         var automationCompositionAck = Mockito.mock(AutomationCompositionAck.class);
94         assertThrows(AutomationCompositionRuntimeException.class, () -> {
95             participantMessagePublisher.sendAutomationCompositionAck(automationCompositionAck);
96         });
97
98         List<TopicSink> emptyList = Collections.emptyList();
99         assertThrows(IllegalArgumentException.class, () -> {
100             participantMessagePublisher.active(emptyList);
101         });
102
103         participantMessagePublisher.stop();
104     }
105
106     @Test
107     void messageSenderTest() throws CoderException {
108         var participantHandler = commonTestData.getParticipantHandlerAutomationCompositions();
109         var participantParameters = CommonTestData.getParticipantParameters();
110         var messageSender = new MessageSender(participantHandler, participantParameters);
111         messageSender.run();
112         assertFalse(messageSender.makeTimerPool().isTerminated());
113         messageSender.close();
114     }
115
116 }