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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.participant.policy.endtoend;
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.junit.Assert.assertEquals;
26 import java.time.Instant;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
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;
67 @ExtendWith(SpringExtension.class)
68 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
69 @TestPropertySource(locations = {"classpath:application_test.properties"})
70 class ParticipantMessagesTest {
72 private static final Object lockit = new Object();
73 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
74 private static final String TOPIC = "my-topic";
77 private ParticipantHandler participantHandler;
80 void testSendParticipantRegisterMessage() throws Exception {
81 final ParticipantRegister participantRegisterMsg = new ParticipantRegister();
82 participantRegisterMsg.setParticipantId(getParticipantId());
83 participantRegisterMsg.setTimestamp(Instant.now());
84 participantRegisterMsg.setParticipantType(getParticipantType());
86 synchronized (lockit) {
87 ParticipantMessagePublisher participantMessagePublisher =
88 new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
89 participantMessagePublisher.sendParticipantRegister(participantRegisterMsg);
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);
100 synchronized (lockit) {
101 ParticipantRegisterAckListener participantRegisterAckListener =
102 new ParticipantRegisterAckListener(participantHandler);
103 participantRegisterAckListener.onTopicEvent(INFRA, TOPIC, null, participantRegisterAckMsg);
108 void testSendParticipantDeregisterMessage() throws Exception {
109 final ParticipantDeregister participantDeregisterMsg = new ParticipantDeregister();
110 participantDeregisterMsg.setParticipantId(getParticipantId());
111 participantDeregisterMsg.setTimestamp(Instant.now());
112 participantDeregisterMsg.setParticipantType(getParticipantType());
114 synchronized (lockit) {
115 ParticipantMessagePublisher participantMessagePublisher =
116 new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
117 participantMessagePublisher.sendParticipantDeregister(participantDeregisterMsg);
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);
128 synchronized (lockit) {
129 ParticipantDeregisterAckListener participantDeregisterAckListener =
130 new ParticipantDeregisterAckListener(participantHandler);
131 participantDeregisterAckListener.onTopicEvent(INFRA, TOPIC, null, participantDeregisterAckMsg);
136 void testReceiveParticipantUpdateMessage() throws Exception {
137 ParticipantUpdate participantUpdateMsg = TestListenerUtils.createParticipantUpdateMsg();
139 synchronized (lockit) {
140 ParticipantUpdateListener participantUpdateListener = new ParticipantUpdateListener(participantHandler);
141 participantUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateMsg);
144 // Verify the result of GET participants with what is stored
145 assertEquals("org.onap.PM_Policy", participantHandler.getParticipantId().getName());
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);
155 synchronized (lockit) {
156 ParticipantMessagePublisher participantMessagePublisher =
157 new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
158 participantMessagePublisher.sendParticipantUpdateAck(participantUpdateAckMsg);
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();
172 private ToscaConceptIdentifier getParticipantId() {
173 return new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
176 private ToscaConceptIdentifier getParticipantType() {
177 return new ToscaConceptIdentifier("org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");
180 private ToscaConceptIdentifier getControlLoopId() {
181 return new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
184 private ControlLoopInfo getControlLoopInfo(ToscaConceptIdentifier id) {
185 ControlLoopInfo clInfo = new ControlLoopInfo();
186 clInfo.setState(ControlLoopState.PASSIVE2RUNNING);
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);
204 clInfo.setControlLoopStatistics(clStatistics);
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");
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);