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();
89 participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
90 participantMessagePublisher.sendParticipantRegister(participantRegisterMsg);
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);
101 synchronized (lockit) {
102 ParticipantRegisterAckListener participantRegisterAckListener =
103 new ParticipantRegisterAckListener(participantHandler);
104 participantRegisterAckListener.onTopicEvent(INFRA, TOPIC, null, participantRegisterAckMsg);
109 void testSendParticipantDeregisterMessage() throws Exception {
110 final ParticipantDeregister participantDeregisterMsg = new ParticipantDeregister();
111 participantDeregisterMsg.setParticipantId(getParticipantId());
112 participantDeregisterMsg.setTimestamp(Instant.now());
113 participantDeregisterMsg.setParticipantType(getParticipantType());
115 synchronized (lockit) {
116 ParticipantMessagePublisher participantMessagePublisher =
117 new ParticipantMessagePublisher();
118 participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
119 participantMessagePublisher.sendParticipantDeregister(participantDeregisterMsg);
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);
130 synchronized (lockit) {
131 ParticipantDeregisterAckListener participantDeregisterAckListener =
132 new ParticipantDeregisterAckListener(participantHandler);
133 participantDeregisterAckListener.onTopicEvent(INFRA, TOPIC, null, participantDeregisterAckMsg);
138 void testReceiveParticipantUpdateMessage() throws Exception {
139 ParticipantUpdate participantUpdateMsg = TestListenerUtils.createParticipantUpdateMsg();
141 synchronized (lockit) {
142 ParticipantUpdateListener participantUpdateListener = new ParticipantUpdateListener(participantHandler);
143 participantUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateMsg);
146 // Verify the result of GET participants with what is stored
147 assertEquals("org.onap.PM_Policy", participantHandler.getParticipantId().getName());
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);
157 synchronized (lockit) {
158 ParticipantMessagePublisher participantMessagePublisher = new ParticipantMessagePublisher();
159 participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
160 participantMessagePublisher.sendParticipantUpdateAck(participantUpdateAckMsg);
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();
174 private ToscaConceptIdentifier getParticipantId() {
175 return new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
178 private ToscaConceptIdentifier getParticipantType() {
179 return new ToscaConceptIdentifier("org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");
182 private ToscaConceptIdentifier getControlLoopId() {
183 return new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
186 private ControlLoopInfo getControlLoopInfo(ToscaConceptIdentifier id) {
187 ControlLoopInfo clInfo = new ControlLoopInfo();
188 clInfo.setState(ControlLoopState.PASSIVE2RUNNING);
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);
206 clInfo.setControlLoopStatistics(clStatistics);
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");
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);