699df25e814c744b263668497f7f502678143d4b
[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 = participantHandler.makeHeartbeat(true);
165         synchronized (lockit) {
166             ParticipantMessagePublisher publisher =
167                     new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
168             assertThatCode(() -> publisher.sendHeartbeat(heartbeat)).doesNotThrowAnyException();
169         }
170     }
171
172     private ToscaConceptIdentifier getParticipantId() {
173         return new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
174     }
175
176     private ToscaConceptIdentifier getParticipantType() {
177         return new ToscaConceptIdentifier("org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");
178     }
179
180     private ToscaConceptIdentifier getControlLoopId() {
181         return new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
182     }
183
184     private ControlLoopInfo getControlLoopInfo(ToscaConceptIdentifier id) {
185         ControlLoopInfo clInfo = new ControlLoopInfo();
186         clInfo.setState(ControlLoopState.PASSIVE2RUNNING);
187
188         ControlLoopStatistics clStatistics = new ControlLoopStatistics();
189         clStatistics.setControlLoopId(id);
190         clStatistics.setAverageExecutionTime(12345);
191         clStatistics.setEventCount(12345);
192         clStatistics.setLastEnterTime(12345);
193         clStatistics.setLastExecutionTime(12345);
194         clStatistics.setLastStart(12345);
195         clStatistics.setTimeStamp(Instant.ofEpochMilli(3000));
196         clStatistics.setUpTime(12345);
197         ClElementStatisticsList clElementStatisticsList = new ClElementStatisticsList();
198         ClElementStatistics clElementStatistics = new ClElementStatistics();
199         clElementStatistics.setParticipantId(new ToscaConceptIdentifier("defName", "0.0.1"));
200         clElementStatistics.setTimeStamp(Instant.now());
201         clElementStatisticsList.setClElementStatistics(List.of(clElementStatistics));
202         clStatistics.setClElementStatisticsList(clElementStatisticsList);
203
204         clInfo.setControlLoopStatistics(clStatistics);
205         return clInfo;
206     }
207
208     private ControlLoopElementDefinition getClElementDefinition() {
209         ToscaNodeTemplate toscaNodeTemplate = new ToscaNodeTemplate();
210         toscaNodeTemplate.setName("serviceTemplate");
211         toscaNodeTemplate.setDerivedFrom("parentServiceTemplate");
212         toscaNodeTemplate.setDescription("Description of serviceTemplate");
213         toscaNodeTemplate.setVersion("1.2.3");
214
215         ControlLoopElementDefinition clDefinition = new ControlLoopElementDefinition();
216         clDefinition.setCommonPropertiesMap(Map.of("Prop1", "Prop1Value"));
217         clDefinition.setControlLoopElementToscaNodeTemplate(toscaNodeTemplate);
218         Map<String, String> commonPropertiesMap = Map.of("Prop1", "PropValue");
219         clDefinition.setCommonPropertiesMap(commonPropertiesMap);
220         return clDefinition;
221     }
222 }