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;
25 import java.time.Instant;
26 import java.util.Collections;
27 import java.util.List;
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;
62 @ExtendWith(SpringExtension.class)
63 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
64 @TestPropertySource(locations = {"classpath:application_test.properties"})
65 class ParticipantMessagesTest {
67 private static final Object lockit = new Object();
68 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
69 private static final String TOPIC = "my-topic";
72 private ParticipantHandler participantHandler;
75 void testSendParticipantRegisterMessage() throws Exception {
76 final ParticipantRegister participantRegisterMsg = new ParticipantRegister();
77 participantRegisterMsg.setParticipantId(getParticipantId());
78 participantRegisterMsg.setTimestamp(Instant.now());
79 participantRegisterMsg.setParticipantType(getParticipantType());
81 synchronized (lockit) {
82 ParticipantMessagePublisher participantMessagePublisher =
83 new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
84 participantMessagePublisher.sendParticipantRegister(participantRegisterMsg);
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);
95 synchronized (lockit) {
96 ParticipantRegisterAckListener participantRegisterAckListener =
97 new ParticipantRegisterAckListener(participantHandler);
98 participantRegisterAckListener.onTopicEvent(INFRA, TOPIC, null, participantRegisterAckMsg);
103 void testSendParticipantDeregisterMessage() throws Exception {
104 final ParticipantDeregister participantDeregisterMsg = new ParticipantDeregister();
105 participantDeregisterMsg.setParticipantId(getParticipantId());
106 participantDeregisterMsg.setTimestamp(Instant.now());
107 participantDeregisterMsg.setParticipantType(getParticipantType());
109 synchronized (lockit) {
110 ParticipantMessagePublisher participantMessagePublisher =
111 new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
112 participantMessagePublisher.sendParticipantDeregister(participantDeregisterMsg);
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);
123 synchronized (lockit) {
124 ParticipantDeregisterAckListener participantDeregisterAckListener =
125 new ParticipantDeregisterAckListener(participantHandler);
126 participantDeregisterAckListener.onTopicEvent(INFRA, TOPIC, null, participantDeregisterAckMsg);
131 void testReceiveParticipantUpdateMessage() throws Exception {
132 ParticipantUpdate participantUpdateMsg = TestListenerUtils.createParticipantUpdateMsg();
134 synchronized (lockit) {
135 ParticipantUpdateListener participantUpdateListener = new ParticipantUpdateListener(participantHandler);
136 participantUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateMsg);
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);
147 synchronized (lockit) {
148 ParticipantMessagePublisher participantMessagePublisher =
149 new ParticipantMessagePublisher(Collections.singletonList(Mockito.mock(TopicSink.class)));
150 participantMessagePublisher.sendParticipantUpdateAck(participantUpdateAckMsg);
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));
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);
167 synchronized (lockit) {
168 ParticipantMessagePublisher publisher =
169 new ParticipantMessagePublisher(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 ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
212 toscaServiceTemplate.setName("serviceTemplate");
213 toscaServiceTemplate.setDerivedFrom("parentServiceTemplate");
214 toscaServiceTemplate.setDescription("Description of serviceTemplate");
215 toscaServiceTemplate.setVersion("1.2.3");
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);