2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 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.acm.runtime.supervision;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
28 import java.util.List;
30 import java.util.Optional;
32 import java.util.UUID;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.clamp.acm.runtime.instantiation.InstantiationUtils;
35 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantDeregisterAckPublisher;
36 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantRegisterAckPublisher;
37 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantRestartPublisher;
38 import org.onap.policy.clamp.acm.runtime.util.CommonTestData;
39 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
40 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
41 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
42 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionInfo;
43 import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState;
44 import org.onap.policy.clamp.models.acm.concepts.Participant;
45 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
46 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
47 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
48 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
49 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
50 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
51 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
52 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
55 class SupervisionParticipantHandlerTest {
57 private static final String AC_INSTANTIATION_CREATE_JSON = "src/test/resources/rest/acm/AutomationComposition.json";
60 void testHandleParticipantDeregister() {
61 var participant = CommonTestData.createParticipant(CommonTestData.getParticipantId());
63 var participantProvider = mock(ParticipantProvider.class);
64 when(participantProvider.findParticipant(CommonTestData.getParticipantId()))
65 .thenReturn(Optional.of(participant));
67 var participantDeregisterMessage = new ParticipantDeregister();
68 participantDeregisterMessage.setMessageId(UUID.randomUUID());
69 participantDeregisterMessage.setParticipantId(CommonTestData.getParticipantId());
70 var participantDeregisterAckPublisher = mock(ParticipantDeregisterAckPublisher.class);
72 new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
73 participantDeregisterAckPublisher, mock(AutomationCompositionProvider.class),
74 mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class));
76 handler.handleParticipantMessage(participantDeregisterMessage);
78 verify(participantProvider).updateParticipant(any());
79 verify(participantDeregisterAckPublisher).send(participantDeregisterMessage.getMessageId());
83 void testHandleParticipantRegister() {
84 var participantRegisterMessage = new ParticipantRegister();
85 participantRegisterMessage.setMessageId(UUID.randomUUID());
86 participantRegisterMessage.setParticipantId(CommonTestData.getParticipantId());
87 var supportedElementType = CommonTestData.createParticipantSupportedElementType();
88 participantRegisterMessage.setParticipantSupportedElementType(List.of(supportedElementType));
90 var participantProvider = mock(ParticipantProvider.class);
91 var participantRegisterAckPublisher = mock(ParticipantRegisterAckPublisher.class);
92 var handler = new SupervisionParticipantHandler(participantProvider, participantRegisterAckPublisher,
93 mock(ParticipantDeregisterAckPublisher.class), mock(AutomationCompositionProvider.class),
94 mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class));
95 handler.handleParticipantMessage(participantRegisterMessage);
97 verify(participantProvider).saveParticipant(any());
98 verify(participantRegisterAckPublisher).send(participantRegisterMessage.getMessageId(),
99 CommonTestData.getParticipantId());
103 void testHandleParticipantRestart() {
104 var participantRegisterMessage = new ParticipantRegister();
105 participantRegisterMessage.setMessageId(UUID.randomUUID());
106 var participantId = CommonTestData.getParticipantId();
107 participantRegisterMessage.setParticipantId(participantId);
108 var supportedElementType = CommonTestData.createParticipantSupportedElementType();
109 participantRegisterMessage.setParticipantSupportedElementType(List.of(supportedElementType));
111 var participant = new Participant();
112 participant.setParticipantId(participantId);
113 var participantProvider = mock(ParticipantProvider.class);
114 when(participantProvider.findParticipant(participantId)).thenReturn(Optional.of(participant));
115 var compositionId = UUID.randomUUID();
116 var composition2Id = UUID.randomUUID();
117 when(participantProvider.getCompositionIds(participantId)).thenReturn(Set.of(compositionId, composition2Id));
119 var acDefinitionProvider = mock(AcDefinitionProvider.class);
120 var acDefinition = new AutomationCompositionDefinition();
121 acDefinition.setState(AcTypeState.COMMISSIONED);
122 acDefinition.setCompositionId(composition2Id);
123 when(acDefinitionProvider.getAcDefinition(composition2Id)).thenReturn(acDefinition);
125 acDefinition = new AutomationCompositionDefinition();
126 acDefinition.setCompositionId(compositionId);
127 acDefinition.setState(AcTypeState.PRIMED);
128 var nodeTemplateState = new NodeTemplateState();
129 nodeTemplateState.setParticipantId(participantId);
130 acDefinition.setElementStateMap(Map.of("code", nodeTemplateState));
131 when(acDefinitionProvider.getAcDefinition(compositionId)).thenReturn(acDefinition);
133 var automationComposition =
134 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Crud");
135 automationComposition.getElements().values().iterator().next().setParticipantId(participantId);
136 var automationCompositionProvider = mock(AutomationCompositionProvider.class);
137 when(automationCompositionProvider.getAcInstancesByCompositionId(compositionId))
138 .thenReturn(List.of(automationComposition));
140 var participantRegisterAckPublisher = mock(ParticipantRegisterAckPublisher.class);
141 var participantRestartPublisher = mock(ParticipantRestartPublisher.class);
142 var handler = new SupervisionParticipantHandler(participantProvider, participantRegisterAckPublisher,
143 mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider, acDefinitionProvider,
144 participantRestartPublisher);
145 handler.handleParticipantMessage(participantRegisterMessage);
147 verify(participantRegisterAckPublisher).send(participantRegisterMessage.getMessageId(), participantId);
148 verify(acDefinitionProvider).updateAcDefinition(any(AutomationCompositionDefinition.class));
149 verify(participantRestartPublisher).send(any(), any(AutomationCompositionDefinition.class), any());
153 void testHandleParticipantStatus() {
154 var participantStatusMessage = createParticipantStatus();
155 participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
157 var participantProvider = mock(ParticipantProvider.class);
158 var automationCompositionProvider = mock(AutomationCompositionProvider.class);
160 new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
161 mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider,
162 mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class));
163 var participant = CommonTestData.createParticipant(CommonTestData.getParticipantId());
164 when(participantProvider.findParticipant(CommonTestData.getParticipantId()))
165 .thenReturn(Optional.of(participant));
166 handler.handleParticipantMessage(participantStatusMessage);
168 verify(automationCompositionProvider).upgradeStates(any());
172 void testAcDefinitionOutProperties() {
173 var participantStatusMessage = createParticipantStatus();
174 participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
175 var participantDefinition = new ParticipantDefinition();
176 participantStatusMessage.setParticipantDefinitionUpdates(List.of(participantDefinition));
177 participantDefinition.setParticipantId(participantStatusMessage.getParticipantId());
178 var acElementDefinition = new AutomationCompositionElementDefinition();
179 acElementDefinition.setAcElementDefinitionId(new ToscaConceptIdentifier("code", "1.0.0"));
180 participantDefinition.setAutomationCompositionElementDefinitionList(List.of(acElementDefinition));
182 var compositionId = UUID.randomUUID();
183 participantStatusMessage.setCompositionId(compositionId);
184 var acDefinition = new AutomationCompositionDefinition();
185 acDefinition.setState(AcTypeState.COMMISSIONED);
186 acDefinition.setCompositionId(compositionId);
187 var nodeTemplateState = new NodeTemplateState();
188 acDefinition.setElementStateMap(
189 Map.of(acElementDefinition.getAcElementDefinitionId().getName(), nodeTemplateState));
190 var acDefinitionProvider = mock(AcDefinitionProvider.class);
191 when(acDefinitionProvider.findAcDefinition(compositionId)).thenReturn(Optional.of(acDefinition));
193 var participantProvider = mock(ParticipantProvider.class);
195 new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
196 mock(ParticipantDeregisterAckPublisher.class), mock(AutomationCompositionProvider.class),
197 acDefinitionProvider, mock(ParticipantRestartPublisher.class));
198 handler.handleParticipantMessage(participantStatusMessage);
200 verify(acDefinitionProvider).updateAcDefinition(acDefinition);
204 void testHandleParticipantStatusNotRegisterd() {
205 var participantStatusMessage = createParticipantStatus();
206 participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
208 var participantProvider = mock(ParticipantProvider.class);
209 var automationCompositionProvider = mock(AutomationCompositionProvider.class);
211 new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
212 mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider,
213 mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class));
214 handler.handleParticipantMessage(participantStatusMessage);
216 verify(participantProvider).saveParticipant(any());
217 verify(automationCompositionProvider).upgradeStates(any());
221 void testHandleParticipantStatusCheckOnline() {
222 var participantStatusMessage = createParticipantStatus();
223 participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
225 var participantProvider = mock(ParticipantProvider.class);
226 var automationCompositionProvider = mock(AutomationCompositionProvider.class);
228 new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
229 mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider,
230 mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class));
231 var participant = CommonTestData.createParticipant(CommonTestData.getParticipantId());
232 participant.setParticipantState(ParticipantState.OFF_LINE);
233 when(participantProvider.findParticipant(CommonTestData.getParticipantId()))
234 .thenReturn(Optional.of(participant));
235 handler.handleParticipantMessage(participantStatusMessage);
237 verify(participantProvider).saveParticipant(any());
238 verify(automationCompositionProvider).upgradeStates(any());
241 private ParticipantStatus createParticipantStatus() {
242 var statusMessage = new ParticipantStatus();
243 statusMessage.setParticipantId(CommonTestData.getParticipantId());
244 statusMessage.setState(ParticipantState.ON_LINE);
245 var supportedElementType = CommonTestData.createParticipantSupportedElementType();
246 statusMessage.setParticipantSupportedElementType(List.of(supportedElementType));
247 return statusMessage;