2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 Nordix Foundation.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.acm.runtime.supervision.comm;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.mockito.ArgumentMatchers.anyString;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
31 import java.time.Instant;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.UUID;
35 import org.junit.jupiter.api.BeforeAll;
36 import org.junit.jupiter.api.Test;
37 import org.mockito.Mockito;
38 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionHandler;
39 import org.onap.policy.clamp.acm.runtime.util.rest.CommonRestController;
40 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
41 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregisterAck;
44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
45 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdateAck;
46 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
47 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
48 import org.onap.policy.clamp.models.acm.persistence.provider.ServiceTemplateProvider;
49 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
50 import org.onap.policy.common.endpoints.event.comm.TopicSink;
51 import org.onap.policy.models.base.PfModelException;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
54 class SupervisionMessagesTest extends CommonRestController {
56 private static final String NOT_ACTIVE = "Not Active!";
57 private static final Object lockit = new Object();
58 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
59 private static final String TOPIC = "my-topic";
60 private static SupervisionHandler supervisionHandler;
63 * setup Db Provider Parameters.
65 * @throws PfModelException if an error occurs
68 public static void setupDbProviderParameters() throws PfModelException {
69 var acProvider = mock(AutomationCompositionProvider.class);
70 var participantProvider = mock(ParticipantProvider.class);
71 var serviceTemplateProvider = Mockito.mock(ServiceTemplateProvider.class);
72 var automationCompositionUpdatePublisher = Mockito.mock(AutomationCompositionUpdatePublisher.class);
73 var automationCompositionStateChangePublisher = Mockito.mock(AutomationCompositionStateChangePublisher.class);
74 var participantRegisterAckPublisher = Mockito.mock(ParticipantRegisterAckPublisher.class);
75 var participantDeregisterAckPublisher = Mockito.mock(ParticipantDeregisterAckPublisher.class);
76 var participantUpdatePublisher = Mockito.mock(ParticipantUpdatePublisher.class);
77 supervisionHandler = new SupervisionHandler(acProvider, participantProvider,
78 serviceTemplateProvider, automationCompositionUpdatePublisher, automationCompositionStateChangePublisher,
79 participantRegisterAckPublisher, participantDeregisterAckPublisher, participantUpdatePublisher);
83 void testSendParticipantRegisterAck() throws Exception {
84 final ParticipantRegisterAck participantRegisterAckMsg = new ParticipantRegisterAck();
85 participantRegisterAckMsg.setMessage("ParticipantRegisterAck message");
86 participantRegisterAckMsg.setResponseTo(UUID.randomUUID());
87 participantRegisterAckMsg.setResult(true);
89 synchronized (lockit) {
90 ParticipantRegisterAckPublisher acRegisterAckPublisher = new ParticipantRegisterAckPublisher();
91 acRegisterAckPublisher.active(List.of(Mockito.mock(TopicSink.class)));
92 assertThatCode(() -> acRegisterAckPublisher.send(participantRegisterAckMsg)).doesNotThrowAnyException();
97 void testReceiveParticipantDeregister() throws Exception {
98 final ParticipantDeregister participantDeregisterMsg = new ParticipantDeregister();
99 participantDeregisterMsg.setParticipantId(getParticipantId());
100 participantDeregisterMsg.setTimestamp(Instant.now());
101 participantDeregisterMsg.setParticipantType(getParticipantType());
103 synchronized (lockit) {
104 ParticipantDeregisterListener participantDeregisterListener =
105 new ParticipantDeregisterListener(supervisionHandler);
107 () -> participantDeregisterListener.onTopicEvent(INFRA, TOPIC, null, participantDeregisterMsg))
108 .doesNotThrowAnyException();
113 void testSendParticipantDeregisterAck() throws Exception {
114 final ParticipantDeregisterAck participantDeregisterAckMsg = new ParticipantDeregisterAck();
115 participantDeregisterAckMsg.setMessage("ParticipantDeregisterAck message");
116 participantDeregisterAckMsg.setResponseTo(UUID.randomUUID());
117 participantDeregisterAckMsg.setResult(true);
119 synchronized (lockit) {
120 ParticipantDeregisterAckPublisher acDeregisterAckPublisher = new ParticipantDeregisterAckPublisher();
121 acDeregisterAckPublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
122 assertThatCode(() -> acDeregisterAckPublisher.send(participantDeregisterAckMsg)).doesNotThrowAnyException();
127 void testReceiveParticipantUpdateAckMessage() throws Exception {
128 final ParticipantUpdateAck participantUpdateAckMsg = new ParticipantUpdateAck();
129 participantUpdateAckMsg.setMessage("ParticipantUpdateAck message");
130 participantUpdateAckMsg.setResponseTo(UUID.randomUUID());
131 participantUpdateAckMsg.setResult(true);
132 participantUpdateAckMsg.setParticipantId(getParticipantId());
133 participantUpdateAckMsg.setParticipantType(getParticipantType());
135 synchronized (lockit) {
136 ParticipantUpdateAckListener participantUpdateAckListener =
137 new ParticipantUpdateAckListener(supervisionHandler);
138 assertThatCode(() -> participantUpdateAckListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateAckMsg))
139 .doesNotThrowAnyException();
144 void testSendAutomationCompositionStateChangePublisherNotActive() {
145 var publisher = new AutomationCompositionStateChangePublisher();
146 assertThatThrownBy(() -> publisher.send(getAutomationComposition(), 0)).hasMessage(NOT_ACTIVE);
150 void testSendAutomationCompositionStateChangePublisher() {
151 var publisher = new AutomationCompositionStateChangePublisher();
152 var topicSink = mock(TopicSink.class);
153 publisher.active(List.of(topicSink));
154 publisher.send(getAutomationComposition(), 0);
155 verify(topicSink).send(anyString());
159 void testParticipantUpdatePublisherDecomisioning() {
160 var publisher = new ParticipantUpdatePublisher(mock(ServiceTemplateProvider.class));
161 var topicSink = mock(TopicSink.class);
162 publisher.active(List.of(topicSink));
163 publisher.sendDecomisioning();
164 verify(topicSink).send(anyString());
168 void testParticipantUpdatePublisherComissioning() {
169 var publisher = new ParticipantUpdatePublisher(mock(ServiceTemplateProvider.class));
170 var topicSink = mock(TopicSink.class);
171 publisher.active(List.of(topicSink));
172 publisher.sendComissioningBroadcast("NAME", "1.0.0");
173 verify(topicSink, times(0)).send(anyString());
177 void testParticipantStatusReqPublisher() {
178 var publisher = new ParticipantStatusReqPublisher();
179 var topicSink = mock(TopicSink.class);
180 publisher.active(List.of(topicSink));
181 publisher.send(getParticipantId());
182 verify(topicSink).send(anyString());
186 void testParticipantRegisterAckPublisher() {
187 var publisher = new ParticipantRegisterAckPublisher();
188 var topicSink = mock(TopicSink.class);
189 publisher.active(List.of(topicSink));
190 publisher.send(UUID.randomUUID(), getParticipantId(), getParticipantType());
191 verify(topicSink).send(anyString());
195 void testParticipantDeregisterAckPublisher() {
196 var publisher = new ParticipantDeregisterAckPublisher();
197 var topicSink = mock(TopicSink.class);
198 publisher.active(List.of(topicSink));
199 publisher.send(UUID.randomUUID());
200 verify(topicSink).send(anyString());
203 private AutomationComposition getAutomationComposition() {
204 var automationComposition = new AutomationComposition();
205 automationComposition.setName("NAME");
206 automationComposition.setVersion("0.0.1");
207 automationComposition.setState(AutomationCompositionState.UNINITIALISED);
208 return automationComposition;
211 private ToscaConceptIdentifier getParticipantId() {
212 return new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
215 private ToscaConceptIdentifier getParticipantType() {
216 return new ToscaConceptIdentifier("org.onap.policy.acm.PolicyAutomationCompositionParticipant", "2.3.1");