a40a41853e65a9d28d2cbe1fa84fc364780c1019
[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.controlloop.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.controlloop.common.exception.ControlLoopRuntimeException;
32 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopAck;
33 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
34 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType;
35 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
36 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
37 import org.onap.policy.clamp.controlloop.participant.intermediary.main.parameters.CommonTestData;
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.getParticipantHandlerControlLoops();
48
49         var participantRegisterAckListener = new ParticipantRegisterAckListener(participantHandler);
50         assertEquals(ParticipantMessageType.PARTICIPANT_REGISTER_ACK.name(),
51                 participantRegisterAckListener.getType());
52
53         var participantStatusReqListener = new ParticipantStatusReqListener(participantHandler);
54         assertEquals(ParticipantMessageType.PARTICIPANT_STATUS_REQ.name(),
55                 participantStatusReqListener.getType());
56
57         var participantDeregisterAckListener = new ParticipantDeregisterAckListener(participantHandler);
58         assertEquals(ParticipantMessageType.PARTICIPANT_DEREGISTER_ACK.name(),
59                 participantDeregisterAckListener.getType());
60
61         var participantUpdateListener = new ParticipantUpdateListener(participantHandler);
62         assertEquals(ParticipantMessageType.PARTICIPANT_UPDATE.name(),
63                 participantUpdateListener.getType());
64
65         var controlLoopUpdateListener = new ControlLoopUpdateListener(participantHandler);
66         assertEquals(ParticipantMessageType.CONTROL_LOOP_UPDATE.name(),
67                 controlLoopUpdateListener.getType());
68
69         var controlLoopStateChangeListener = new ControlLoopStateChangeListener(participantHandler);
70         assertEquals(ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE.name(),
71                 controlLoopStateChangeListener.getType());
72     }
73
74     @Test
75     void participantMessagePublisherExceptionsTest() {
76         var participantMessagePublisher = new ParticipantMessagePublisher();
77
78         var participantStatus = Mockito.mock(ParticipantStatus.class);
79         assertThrows(ControlLoopRuntimeException.class, () -> {
80             participantMessagePublisher.sendParticipantStatus(participantStatus);
81         });
82         assertThrows(ControlLoopRuntimeException.class, () -> {
83             participantMessagePublisher.sendHeartbeat(participantStatus);
84         });
85
86         var participantRegister = Mockito.mock(ParticipantRegister.class);
87         assertThrows(ControlLoopRuntimeException.class, () -> {
88             participantMessagePublisher.sendParticipantRegister(participantRegister);
89         });
90
91         var participantDeregister = Mockito.mock(ParticipantDeregister.class);
92         assertThrows(ControlLoopRuntimeException.class, () -> {
93             participantMessagePublisher.sendParticipantDeregister(participantDeregister);
94         });
95
96         var controlLoopAck = Mockito.mock(ControlLoopAck.class);
97         assertThrows(ControlLoopRuntimeException.class, () -> {
98             participantMessagePublisher.sendControlLoopAck(controlLoopAck);
99         });
100
101         List<TopicSink> emptyList = Collections.emptyList();
102         assertThrows(IllegalArgumentException.class, () -> {
103             participantMessagePublisher.active(emptyList);
104         });
105
106         participantMessagePublisher.stop();
107     }
108
109     @Test
110     void messageSenderTest() throws CoderException {
111         var participantHandler = commonTestData.getParticipantHandlerControlLoops();
112         var participantParameters = CommonTestData.getParticipantParameters();
113         var messageSender = new MessageSender(participantHandler, participantParameters);
114         messageSender.run();
115         assertFalse(messageSender.makeTimerPool().isTerminated());
116         messageSender.close();
117     }
118
119 }