aed03556a0606ead355ef930429b51ccffcb361e
[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
25 import java.time.Instant;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.UUID;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.extension.ExtendWith;
32 import org.mockito.Mockito;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopInfo;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopStatistics;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck;
41 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
42 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck;
43 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
44 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
45 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
46 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantDeregisterAckListener;
47 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher;
48 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantRegisterAckListener;
49 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantUpdateListener;
50 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
51 import org.onap.policy.clamp.controlloop.participant.policy.main.utils.TestListenerUtils;
52 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
53 import org.onap.policy.common.endpoints.event.comm.TopicSink;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.boot.test.context.SpringBootTest;
58 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
59 import org.springframework.test.context.TestPropertySource;
60 import org.springframework.test.context.junit.jupiter.SpringExtension;
61
62 @ExtendWith(SpringExtension.class)
63 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
64 @TestPropertySource(locations = {"classpath:application_test.properties"})
65 class ParticipantMessagesTest {
66
67     private static final Object lockit = new Object();
68     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
69     private static final String TOPIC = "my-topic";
70
71     @Autowired
72     private ParticipantHandler participantHandler;
73
74     @Test
75     void testSendParticipantRegisterMessage() throws Exception {
76         final ParticipantRegister participantRegisterMsg = new ParticipantRegister();
77         participantRegisterMsg.setParticipantId(getParticipantId());
78         participantRegisterMsg.setTimestamp(Instant.now());
79         participantRegisterMsg.setParticipantType(getParticipantType());
80
81         synchronized (lockit) {
82             ParticipantMessagePublisher participantMessagePublisher =
83                     new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
84             participantMessagePublisher.sendParticipantRegister(participantRegisterMsg);
85         }
86     }
87
88     @Test
89     void testReceiveParticipantRegisterAckMessage() throws Exception {
90         final ParticipantRegisterAck participantRegisterAckMsg = new ParticipantRegisterAck();
91         participantRegisterAckMsg.setMessage("ParticipantRegisterAck message");
92         participantRegisterAckMsg.setResponseTo(UUID.randomUUID());
93         participantRegisterAckMsg.setResult(true);
94
95         synchronized (lockit) {
96             ParticipantRegisterAckListener participantRegisterAckListener =
97                 new ParticipantRegisterAckListener(participantHandler);
98             participantRegisterAckListener.onTopicEvent(INFRA, TOPIC, null, participantRegisterAckMsg);
99         }
100     }
101
102     @Test
103     void testSendParticipantDeregisterMessage() throws Exception {
104         final ParticipantDeregister participantDeregisterMsg = new ParticipantDeregister();
105         participantDeregisterMsg.setParticipantId(getParticipantId());
106         participantDeregisterMsg.setTimestamp(Instant.now());
107         participantDeregisterMsg.setParticipantType(getParticipantType());
108
109         synchronized (lockit) {
110             ParticipantMessagePublisher participantMessagePublisher =
111                     new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
112             participantMessagePublisher.sendParticipantDeregister(participantDeregisterMsg);
113         }
114     }
115
116     @Test
117     void testReceiveParticipantDeregisterAckMessage() throws Exception {
118         final ParticipantDeregisterAck participantDeregisterAckMsg = new ParticipantDeregisterAck();
119         participantDeregisterAckMsg.setMessage("ParticipantDeregisterAck message");
120         participantDeregisterAckMsg.setResponseTo(UUID.randomUUID());
121         participantDeregisterAckMsg.setResult(true);
122
123         synchronized (lockit) {
124             ParticipantDeregisterAckListener participantDeregisterAckListener =
125                     new ParticipantDeregisterAckListener(participantHandler);
126             participantDeregisterAckListener.onTopicEvent(INFRA, TOPIC, null, participantDeregisterAckMsg);
127         }
128     }
129
130     @Test
131     void testReceiveParticipantUpdateMessage() throws Exception {
132         ParticipantUpdate participantUpdateMsg = TestListenerUtils.createParticipantUpdateMsg();
133
134         synchronized (lockit) {
135             ParticipantUpdateListener participantUpdateListener = new ParticipantUpdateListener(participantHandler);
136             participantUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateMsg);
137         }
138     }
139
140     @Test
141     void testSendParticipantUpdateAckMessage() throws Exception {
142         final ParticipantUpdateAck participantUpdateAckMsg = new ParticipantUpdateAck();
143         participantUpdateAckMsg.setMessage("ParticipantUpdateAck message");
144         participantUpdateAckMsg.setResponseTo(UUID.randomUUID());
145         participantUpdateAckMsg.setResult(true);
146
147         synchronized (lockit) {
148             ParticipantMessagePublisher participantMessagePublisher =
149                     new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
150             participantMessagePublisher.sendParticipantUpdateAck(participantUpdateAckMsg);
151         }
152     }
153
154     @Test
155     void testParticipantStatusHeartbeat() throws Exception {
156         final ParticipantStatus heartbeat = new ParticipantStatus();
157         heartbeat.setParticipantId(getParticipantId());
158         ControlLoopInfo clInfo = getControlLoopInfo(getControlLoopId());
159         heartbeat.setControlLoopInfoMap(Map.of(getControlLoopId(), clInfo));
160
161         ControlLoopElementDefinition clDefinition = getClElementDefinition();
162         Map<UUID, ControlLoopElementDefinition> clElementDefinitionMap = Map.of(UUID.randomUUID(), clDefinition);
163         Map<ToscaConceptIdentifier, Map<UUID, ControlLoopElementDefinition>>
164             participantDefinitionUpdateMap = Map.of(getParticipantId(), clElementDefinitionMap);
165         heartbeat.setParticipantDefinitionUpdateMap(participantDefinitionUpdateMap);
166
167         synchronized (lockit) {
168             ParticipantMessagePublisher publisher =
169                     new ParticipantMessagePublisher(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         ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
212         toscaServiceTemplate.setName("serviceTemplate");
213         toscaServiceTemplate.setDerivedFrom("parentServiceTemplate");
214         toscaServiceTemplate.setDescription("Description of serviceTemplate");
215         toscaServiceTemplate.setVersion("1.2.3");
216
217         ControlLoopElementDefinition clDefinition = new ControlLoopElementDefinition();
218         clDefinition.setId(UUID.randomUUID());
219         clDefinition.setControlLoopElementToscaServiceTemplate(toscaServiceTemplate);
220         Map<String, String> commonPropertiesMap = Map.of("Prop1", "PropValue");
221         clDefinition.setCommonPropertiesMap(commonPropertiesMap);
222         return clDefinition;
223     }
224 }