df9a4b259cd0f43d5504f854d70de5bf6c8c3863
[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(Collections.singletonList(Mockito.mock(TopicSink.class)));
89             participantMessagePublisher.sendParticipantRegister(participantRegisterMsg);
90         }
91     }
92
93     @Test
94     void testReceiveParticipantRegisterAckMessage() throws Exception {
95         final ParticipantRegisterAck participantRegisterAckMsg = new ParticipantRegisterAck();
96         participantRegisterAckMsg.setMessage("ParticipantRegisterAck message");
97         participantRegisterAckMsg.setResponseTo(UUID.randomUUID());
98         participantRegisterAckMsg.setResult(true);
99
100         synchronized (lockit) {
101             ParticipantRegisterAckListener participantRegisterAckListener =
102                 new ParticipantRegisterAckListener(participantHandler);
103             participantRegisterAckListener.onTopicEvent(INFRA, TOPIC, null, participantRegisterAckMsg);
104         }
105     }
106
107     @Test
108     void testSendParticipantDeregisterMessage() throws Exception {
109         final ParticipantDeregister participantDeregisterMsg = new ParticipantDeregister();
110         participantDeregisterMsg.setParticipantId(getParticipantId());
111         participantDeregisterMsg.setTimestamp(Instant.now());
112         participantDeregisterMsg.setParticipantType(getParticipantType());
113
114         synchronized (lockit) {
115             ParticipantMessagePublisher participantMessagePublisher =
116                     new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
117             participantMessagePublisher.sendParticipantDeregister(participantDeregisterMsg);
118         }
119     }
120
121     @Test
122     void testReceiveParticipantDeregisterAckMessage() throws Exception {
123         final ParticipantDeregisterAck participantDeregisterAckMsg = new ParticipantDeregisterAck();
124         participantDeregisterAckMsg.setMessage("ParticipantDeregisterAck message");
125         participantDeregisterAckMsg.setResponseTo(UUID.randomUUID());
126         participantDeregisterAckMsg.setResult(true);
127
128         synchronized (lockit) {
129             ParticipantDeregisterAckListener participantDeregisterAckListener =
130                     new ParticipantDeregisterAckListener(participantHandler);
131             participantDeregisterAckListener.onTopicEvent(INFRA, TOPIC, null, participantDeregisterAckMsg);
132         }
133     }
134
135     @Test
136     void testReceiveParticipantUpdateMessage() throws Exception {
137         ParticipantUpdate participantUpdateMsg = TestListenerUtils.createParticipantUpdateMsg();
138
139         synchronized (lockit) {
140             ParticipantUpdateListener participantUpdateListener = new ParticipantUpdateListener(participantHandler);
141             participantUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateMsg);
142         }
143
144         // Verify the result of GET participants with what is stored
145         assertEquals("org.onap.PM_Policy", participantHandler.getParticipantId().getName());
146     }
147
148     @Test
149     void testSendParticipantUpdateAckMessage() throws Exception {
150         final ParticipantUpdateAck participantUpdateAckMsg = new ParticipantUpdateAck();
151         participantUpdateAckMsg.setMessage("ParticipantUpdateAck message");
152         participantUpdateAckMsg.setResponseTo(UUID.randomUUID());
153         participantUpdateAckMsg.setResult(true);
154
155         synchronized (lockit) {
156             ParticipantMessagePublisher participantMessagePublisher =
157                     new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
158             participantMessagePublisher.sendParticipantUpdateAck(participantUpdateAckMsg);
159         }
160     }
161
162     @Test
163     void testParticipantStatusHeartbeat() throws Exception {
164         final ParticipantStatus heartbeat = new ParticipantStatus();
165         heartbeat.setParticipantId(getParticipantId());
166         ControlLoopInfo clInfo = getControlLoopInfo(getControlLoopId());
167         clInfo.setControlLoopId(getControlLoopId());
168         heartbeat.setControlLoopInfoList(List.of(clInfo));
169
170         ControlLoopElementDefinition clDefinition = getClElementDefinition();
171         List<ControlLoopElementDefinition> controlLoopElementDefinitionList =
172             List.of(clDefinition);
173         ParticipantDefinition participantDefinition = new ParticipantDefinition();
174         participantDefinition.setParticipantId(getParticipantId());
175         participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList);
176         List<ParticipantDefinition> participantDefinitionUpdates = List.of(participantDefinition);
177         heartbeat.setParticipantDefinitionUpdates(participantDefinitionUpdates);
178
179         synchronized (lockit) {
180             ParticipantMessagePublisher publisher =
181                     new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
182             assertThatCode(() -> publisher.sendHeartbeat(heartbeat)).doesNotThrowAnyException();
183         }
184     }
185
186     private ToscaConceptIdentifier getParticipantId() {
187         return new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
188     }
189
190     private ToscaConceptIdentifier getParticipantType() {
191         return new ToscaConceptIdentifier("org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");
192     }
193
194     private ToscaConceptIdentifier getControlLoopId() {
195         return new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
196     }
197
198     private ControlLoopInfo getControlLoopInfo(ToscaConceptIdentifier id) {
199         ControlLoopInfo clInfo = new ControlLoopInfo();
200         clInfo.setState(ControlLoopState.PASSIVE2RUNNING);
201
202         ControlLoopStatistics clStatistics = new ControlLoopStatistics();
203         clStatistics.setControlLoopId(id);
204         clStatistics.setAverageExecutionTime(12345);
205         clStatistics.setEventCount(12345);
206         clStatistics.setLastEnterTime(12345);
207         clStatistics.setLastExecutionTime(12345);
208         clStatistics.setLastStart(12345);
209         clStatistics.setTimeStamp(Instant.ofEpochMilli(3000));
210         clStatistics.setUpTime(12345);
211         ClElementStatisticsList clElementStatisticsList = new ClElementStatisticsList();
212         ClElementStatistics clElementStatistics = new ClElementStatistics();
213         clElementStatistics.setParticipantId(new ToscaConceptIdentifier("defName", "0.0.1"));
214         clElementStatistics.setTimeStamp(Instant.now());
215         clElementStatisticsList.setClElementStatistics(List.of(clElementStatistics));
216         clStatistics.setClElementStatisticsList(clElementStatisticsList);
217
218         clInfo.setControlLoopStatistics(clStatistics);
219         return clInfo;
220     }
221
222     private ControlLoopElementDefinition getClElementDefinition() {
223         ToscaNodeTemplate toscaNodeTemplate = new ToscaNodeTemplate();
224         toscaNodeTemplate.setName("serviceTemplate");
225         toscaNodeTemplate.setDerivedFrom("parentServiceTemplate");
226         toscaNodeTemplate.setDescription("Description of serviceTemplate");
227         toscaNodeTemplate.setVersion("1.2.3");
228
229         ControlLoopElementDefinition clDefinition = new ControlLoopElementDefinition();
230         clDefinition.setCommonPropertiesMap(Map.of("Prop1", "Prop1Value"));
231         clDefinition.setControlLoopElementToscaNodeTemplate(toscaNodeTemplate);
232         Map<String, String> commonPropertiesMap = Map.of("Prop1", "PropValue");
233         clDefinition.setCommonPropertiesMap(commonPropertiesMap);
234         return clDefinition;
235     }
236 }