8187378ae078ee6e8a3f959c58a4a690a45a33c3
[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.policy.endtoend;
22
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.junit.Assert.assertEquals;
25
26 import java.time.Instant;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.UUID;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.api.extension.ExtendWith;
34 import org.mockito.Mockito;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopInfo;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopStatistics;
41 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition;
42 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
43 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
44 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck;
45 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
46 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck;
47 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
48 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
49 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
50 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopUpdateListener;
51 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantDeregisterAckListener;
52 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher;
53 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantRegisterAckListener;
54 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantUpdateListener;
55 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
56 import org.onap.policy.clamp.controlloop.participant.policy.main.utils.TestListenerUtils;
57 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
58 import org.onap.policy.common.endpoints.event.comm.TopicSink;
59 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
60 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
61 import org.springframework.beans.factory.annotation.Autowired;
62 import org.springframework.boot.test.context.SpringBootTest;
63 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
64 import org.springframework.test.context.TestPropertySource;
65 import org.springframework.test.context.junit.jupiter.SpringExtension;
66
67 @ExtendWith(SpringExtension.class)
68 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
69 @TestPropertySource(locations = {"classpath:application_test.properties"})
70 class ParticipantMessagesTest {
71
72     private static final Object lockit = new Object();
73     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
74     private static final String TOPIC = "my-topic";
75
76     @Autowired
77     private ParticipantHandler participantHandler;
78
79     @Test
80     void testSendParticipantRegisterMessage() throws Exception {
81         final ParticipantRegister participantRegisterMsg = new ParticipantRegister();
82         participantRegisterMsg.setParticipantId(getParticipantId());
83         participantRegisterMsg.setTimestamp(Instant.now());
84         participantRegisterMsg.setParticipantType(getParticipantType());
85
86         synchronized (lockit) {
87             ParticipantMessagePublisher participantMessagePublisher =
88                     new ParticipantMessagePublisher();
89             participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
90             participantMessagePublisher.sendParticipantRegister(participantRegisterMsg);
91         }
92     }
93
94     @Test
95     void testReceiveParticipantRegisterAckMessage() throws Exception {
96         final ParticipantRegisterAck participantRegisterAckMsg = new ParticipantRegisterAck();
97         participantRegisterAckMsg.setMessage("ParticipantRegisterAck message");
98         participantRegisterAckMsg.setResponseTo(UUID.randomUUID());
99         participantRegisterAckMsg.setResult(true);
100
101         synchronized (lockit) {
102             ParticipantRegisterAckListener participantRegisterAckListener =
103                 new ParticipantRegisterAckListener(participantHandler);
104             participantRegisterAckListener.onTopicEvent(INFRA, TOPIC, null, participantRegisterAckMsg);
105         }
106     }
107
108     @Test
109     void testSendParticipantDeregisterMessage() throws Exception {
110         final ParticipantDeregister participantDeregisterMsg = new ParticipantDeregister();
111         participantDeregisterMsg.setParticipantId(getParticipantId());
112         participantDeregisterMsg.setTimestamp(Instant.now());
113         participantDeregisterMsg.setParticipantType(getParticipantType());
114
115         synchronized (lockit) {
116             ParticipantMessagePublisher participantMessagePublisher =
117                     new ParticipantMessagePublisher();
118             participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
119             participantMessagePublisher.sendParticipantDeregister(participantDeregisterMsg);
120         }
121     }
122
123     @Test
124     void testReceiveParticipantDeregisterAckMessage() throws Exception {
125         final ParticipantDeregisterAck participantDeregisterAckMsg = new ParticipantDeregisterAck();
126         participantDeregisterAckMsg.setMessage("ParticipantDeregisterAck message");
127         participantDeregisterAckMsg.setResponseTo(UUID.randomUUID());
128         participantDeregisterAckMsg.setResult(true);
129
130         synchronized (lockit) {
131             ParticipantDeregisterAckListener participantDeregisterAckListener =
132                     new ParticipantDeregisterAckListener(participantHandler);
133             participantDeregisterAckListener.onTopicEvent(INFRA, TOPIC, null, participantDeregisterAckMsg);
134         }
135     }
136
137     @Test
138     void testReceiveParticipantUpdateMessage() throws Exception {
139         ParticipantUpdate participantUpdateMsg = TestListenerUtils.createParticipantUpdateMsg();
140
141         synchronized (lockit) {
142             ParticipantUpdateListener participantUpdateListener = new ParticipantUpdateListener(participantHandler);
143             participantUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateMsg);
144         }
145
146         // Verify the result of GET participants with what is stored
147         assertEquals("org.onap.PM_Policy", participantHandler.getParticipantId().getName());
148     }
149
150     @Test
151     void testSendParticipantUpdateAckMessage() throws Exception {
152         final ParticipantUpdateAck participantUpdateAckMsg = new ParticipantUpdateAck();
153         participantUpdateAckMsg.setMessage("ParticipantUpdateAck message");
154         participantUpdateAckMsg.setResponseTo(UUID.randomUUID());
155         participantUpdateAckMsg.setResult(true);
156
157         synchronized (lockit) {
158             ParticipantMessagePublisher participantMessagePublisher = new ParticipantMessagePublisher();
159             participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
160             participantMessagePublisher.sendParticipantUpdateAck(participantUpdateAckMsg);
161         }
162     }
163
164     @Test
165     void testParticipantStatusHeartbeat() throws Exception {
166         final ParticipantStatus heartbeat = participantHandler.makeHeartbeat(true);
167         synchronized (lockit) {
168             ParticipantMessagePublisher publisher = new ParticipantMessagePublisher();
169             publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
170             assertThatCode(() -> publisher.sendHeartbeat(heartbeat)).doesNotThrowAnyException();
171         }
172     }
173
174     private ToscaConceptIdentifier getParticipantId() {
175         return new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
176     }
177
178     private ToscaConceptIdentifier getParticipantType() {
179         return new ToscaConceptIdentifier("org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");
180     }
181
182     private ToscaConceptIdentifier getControlLoopId() {
183         return new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
184     }
185
186     private ControlLoopInfo getControlLoopInfo(ToscaConceptIdentifier id) {
187         ControlLoopInfo clInfo = new ControlLoopInfo();
188         clInfo.setState(ControlLoopState.PASSIVE2RUNNING);
189
190         ControlLoopStatistics clStatistics = new ControlLoopStatistics();
191         clStatistics.setControlLoopId(id);
192         clStatistics.setAverageExecutionTime(12345);
193         clStatistics.setEventCount(12345);
194         clStatistics.setLastEnterTime(12345);
195         clStatistics.setLastExecutionTime(12345);
196         clStatistics.setLastStart(12345);
197         clStatistics.setTimeStamp(Instant.ofEpochMilli(3000));
198         clStatistics.setUpTime(12345);
199         ClElementStatisticsList clElementStatisticsList = new ClElementStatisticsList();
200         ClElementStatistics clElementStatistics = new ClElementStatistics();
201         clElementStatistics.setParticipantId(new ToscaConceptIdentifier("defName", "0.0.1"));
202         clElementStatistics.setTimeStamp(Instant.now());
203         clElementStatisticsList.setClElementStatistics(List.of(clElementStatistics));
204         clStatistics.setClElementStatisticsList(clElementStatisticsList);
205
206         clInfo.setControlLoopStatistics(clStatistics);
207         return clInfo;
208     }
209
210     private ControlLoopElementDefinition getClElementDefinition() {
211         ToscaNodeTemplate toscaNodeTemplate = new ToscaNodeTemplate();
212         toscaNodeTemplate.setName("serviceTemplate");
213         toscaNodeTemplate.setDerivedFrom("parentServiceTemplate");
214         toscaNodeTemplate.setDescription("Description of serviceTemplate");
215         toscaNodeTemplate.setVersion("1.2.3");
216
217         ControlLoopElementDefinition clDefinition = new ControlLoopElementDefinition();
218         clDefinition.setCommonPropertiesMap(Map.of("Prop1", "Prop1Value"));
219         clDefinition.setControlLoopElementToscaNodeTemplate(toscaNodeTemplate);
220         Map<String, String> commonPropertiesMap = Map.of("Prop1", "PropValue");
221         clDefinition.setCommonPropertiesMap(commonPropertiesMap);
222         return clDefinition;
223     }
224 }