2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2025 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.scanner;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.anyBoolean;
26 import static org.mockito.ArgumentMatchers.anyInt;
27 import static org.mockito.Mockito.clearInvocations;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32 import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_SERVICE_TEMPLATE_YAML;
35 import java.util.UUID;
36 import org.junit.jupiter.api.Test;
37 import org.onap.policy.clamp.acm.runtime.instantiation.InstantiationUtils;
38 import org.onap.policy.clamp.acm.runtime.supervision.comm.AutomationCompositionDeployPublisher;
39 import org.onap.policy.clamp.acm.runtime.supervision.comm.AutomationCompositionStateChangePublisher;
40 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantSyncPublisher;
41 import org.onap.policy.clamp.acm.runtime.util.CommonTestData;
42 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
43 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
44 import org.onap.policy.clamp.models.acm.concepts.DeployState;
45 import org.onap.policy.clamp.models.acm.concepts.LockState;
46 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
47 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
48 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
50 class PhaseScannerTest {
52 private static final String AC_JSON = "src/test/resources/rest/acm/AutomationCompositionSmoke.json";
53 private static final UUID COMPOSITION_ID = UUID.randomUUID();
54 private static final UUID INSTANCE_ID = UUID.randomUUID();
55 private static final String ELEMENT_NAME =
56 "org.onap.domain.database.Http_PMSHMicroserviceAutomationCompositionElement";
59 void testAcUndeployCompleted() {
60 var automationComposition = InstantiationUtils.getAutomationCompositionFromResource(AC_JSON, "Crud");
61 automationComposition.setInstanceId(INSTANCE_ID);
62 automationComposition.setDeployState(DeployState.UNDEPLOYING);
63 automationComposition.setLockState(LockState.NONE);
64 automationComposition.setCompositionId(COMPOSITION_ID);
65 var automationCompositionProvider = mock(AutomationCompositionProvider.class);
66 when(automationCompositionProvider.updateAutomationComposition(any())).thenReturn(automationComposition);
68 var acDeployPublisher = mock(AutomationCompositionDeployPublisher.class);
69 var acStateChangePublisher = mock(AutomationCompositionStateChangePublisher.class);
70 var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
72 var phaseScanner = new PhaseScanner(automationCompositionProvider, mock(ParticipantSyncPublisher.class),
73 acStateChangePublisher, acDeployPublisher, acRuntimeParameterGroup);
74 var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
75 phaseScanner.scanWithPhase(automationComposition, serviceTemplate, new UpdateSync());
77 verify(automationCompositionProvider).updateAutomationComposition(any(AutomationComposition.class));
81 void testAcDeleted() {
82 var automationComposition = InstantiationUtils.getAutomationCompositionFromResource(AC_JSON, "Crud");
83 automationComposition.setInstanceId(INSTANCE_ID);
84 automationComposition.setDeployState(DeployState.DELETING);
85 automationComposition.setLockState(LockState.NONE);
86 automationComposition.setCompositionId(COMPOSITION_ID);
87 var automationCompositionProvider = mock(AutomationCompositionProvider.class);
88 var acDeployPublisher = mock(AutomationCompositionDeployPublisher.class);
89 var acStateChangePublisher = mock(AutomationCompositionStateChangePublisher.class);
90 var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
92 var phaseScanner = new PhaseScanner(automationCompositionProvider, mock(ParticipantSyncPublisher.class),
93 acStateChangePublisher, acDeployPublisher, acRuntimeParameterGroup);
94 var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
95 phaseScanner.scanWithPhase(automationComposition, serviceTemplate, new UpdateSync());
97 verify(automationCompositionProvider).deleteAutomationComposition(automationComposition.getInstanceId());
101 void testScannerForTimeout() {
102 var automationComposition = InstantiationUtils.getAutomationCompositionFromResource(AC_JSON, "Crud");
103 automationComposition.setInstanceId(INSTANCE_ID);
104 automationComposition.setDeployState(DeployState.DEPLOYING);
105 automationComposition.setLockState(LockState.NONE);
106 automationComposition.setPhase(0);
107 automationComposition.setCompositionId(COMPOSITION_ID);
108 for (var entry : automationComposition.getElements().entrySet()) {
109 entry.getValue().setDeployState(DeployState.DEPLOYING);
111 // the first element is already completed
112 automationComposition.getElements().entrySet().iterator().next().getValue()
113 .setDeployState(DeployState.DEPLOYED);
115 var automationCompositionProvider = mock(AutomationCompositionProvider.class);
116 when(automationCompositionProvider.updateAutomationComposition(any())).thenReturn(automationComposition);
117 var acDeployPublisher = mock(AutomationCompositionDeployPublisher.class);
118 var acStateChangePublisher = mock(AutomationCompositionStateChangePublisher.class);
119 var participantSyncPublisher = mock(ParticipantSyncPublisher.class);
120 var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
121 acRuntimeParameterGroup.getParticipantParameters().setMaxStatusWaitMs(-1);
123 // verify timeout scenario
124 var phaseScanner = new PhaseScanner(automationCompositionProvider, participantSyncPublisher,
125 acStateChangePublisher, acDeployPublisher, acRuntimeParameterGroup);
127 automationComposition.setStateChangeResult(StateChangeResult.NO_ERROR);
128 automationComposition.setLastMsg(TimestampHelper.now());
129 var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
130 phaseScanner.scanWithPhase(automationComposition, serviceTemplate, new UpdateSync());
131 verify(automationCompositionProvider).updateAutomationComposition(any(AutomationComposition.class));
132 verify(participantSyncPublisher).sendSync(any(AutomationComposition.class));
133 assertEquals(StateChangeResult.TIMEOUT, automationComposition.getStateChangeResult());
136 clearInvocations(automationCompositionProvider);
137 clearInvocations(participantSyncPublisher);
138 phaseScanner.scanWithPhase(automationComposition, serviceTemplate, new UpdateSync());
139 verify(automationCompositionProvider, times(0)).updateAutomationComposition(any(AutomationComposition.class));
140 verify(participantSyncPublisher, times(0))
141 .sendSync(any(AutomationComposition.class));
143 clearInvocations(automationCompositionProvider);
144 clearInvocations(participantSyncPublisher);
145 for (Map.Entry<UUID, AutomationCompositionElement> entry : automationComposition.getElements().entrySet()) {
146 entry.getValue().setDeployState(DeployState.DEPLOYED);
148 phaseScanner.scanWithPhase(automationComposition, serviceTemplate, new UpdateSync());
149 verify(automationCompositionProvider).updateAutomationComposition(any(AutomationComposition.class));
150 verify(participantSyncPublisher).sendSync(any(AutomationComposition.class));
151 assertEquals(StateChangeResult.NO_ERROR, automationComposition.getStateChangeResult());
155 void testSendAutomationCompositionMsgStartPhase() {
156 var automationComposition = InstantiationUtils.getAutomationCompositionFromResource(AC_JSON, "Crud");
157 automationComposition.setInstanceId(INSTANCE_ID);
158 automationComposition.setDeployState(DeployState.DEPLOYING);
159 automationComposition.setLockState(LockState.NONE);
160 automationComposition.setPhase(0);
161 automationComposition.setCompositionId(COMPOSITION_ID);
162 for (var element : automationComposition.getElements().values()) {
163 if (ELEMENT_NAME.equals(element.getDefinition().getName())) {
164 element.setDeployState(DeployState.DEPLOYING);
165 element.setLockState(LockState.NONE);
167 element.setDeployState(DeployState.DEPLOYED);
168 element.setLockState(LockState.LOCKED);
172 var automationCompositionProvider = mock(AutomationCompositionProvider.class);
173 var acDeployPublisher = mock(AutomationCompositionDeployPublisher.class);
174 var acStateChangePublisher = mock(AutomationCompositionStateChangePublisher.class);
175 var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
177 var phaseScanner = new PhaseScanner(automationCompositionProvider, mock(ParticipantSyncPublisher.class),
178 acStateChangePublisher, acDeployPublisher, acRuntimeParameterGroup);
180 var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
181 phaseScanner.scanWithPhase(automationComposition, serviceTemplate, new UpdateSync());
183 verify(acDeployPublisher).send(any(AutomationComposition.class), anyInt(), anyBoolean());
187 void testStartPhaseWithNull() {
188 var automationComposition = InstantiationUtils.getAutomationCompositionFromResource(AC_JSON, "Crud");
189 automationComposition.setInstanceId(INSTANCE_ID);
190 automationComposition.setDeployState(DeployState.DEPLOYING);
191 automationComposition.setLockState(LockState.NONE);
192 automationComposition.setPhase(0);
193 automationComposition.setLastMsg(TimestampHelper.now());
194 automationComposition.setCompositionId(COMPOSITION_ID);
195 for (var element : automationComposition.getElements().values()) {
196 if (ELEMENT_NAME.equals(element.getDefinition().getName())) {
197 element.setDeployState(DeployState.DEPLOYING);
198 element.getDefinition().setName("NotExistElement");
199 element.setLockState(LockState.NONE);
201 element.setDeployState(DeployState.DEPLOYING);
202 element.getDefinition().setVersion("0.0.0");
203 element.setLockState(LockState.NONE);
207 var acProvider = mock(AutomationCompositionProvider.class);
208 var acDeployPublisher = mock(AutomationCompositionDeployPublisher.class);
209 var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
211 var phaseScanner = new PhaseScanner(acProvider, mock(ParticipantSyncPublisher.class),
212 mock(AutomationCompositionStateChangePublisher.class), acDeployPublisher,
213 acRuntimeParameterGroup);
215 var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
216 phaseScanner.scanWithPhase(automationComposition, serviceTemplate, new UpdateSync());
218 verify(acDeployPublisher, times(0))
219 .send(any(AutomationComposition.class), anyInt(), anyBoolean());
223 void testSendAutomationCompositionMsgUnlocking() {
224 var automationComposition = InstantiationUtils.getAutomationCompositionFromResource(AC_JSON, "Crud");
225 automationComposition.setInstanceId(INSTANCE_ID);
226 automationComposition.setDeployState(DeployState.DEPLOYED);
227 automationComposition.setLockState(LockState.UNLOCKING);
228 automationComposition.setCompositionId(COMPOSITION_ID);
229 automationComposition.setPhase(0);
230 for (var element : automationComposition.getElements().values()) {
231 if (ELEMENT_NAME.equals(element.getDefinition().getName())) {
232 element.setDeployState(DeployState.DEPLOYED);
233 element.setLockState(LockState.UNLOCKING);
235 element.setDeployState(DeployState.DEPLOYED);
236 element.setLockState(LockState.UNLOCKED);
240 var acProvider = mock(AutomationCompositionProvider.class);
241 var acDeployPublisher = mock(AutomationCompositionDeployPublisher.class);
242 var acStateChangePublisher = mock(AutomationCompositionStateChangePublisher.class);
243 var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
245 var phaseScanner = new PhaseScanner(acProvider, mock(ParticipantSyncPublisher.class),
246 acStateChangePublisher, acDeployPublisher, acRuntimeParameterGroup);
248 var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
249 phaseScanner.scanWithPhase(automationComposition, serviceTemplate, new UpdateSync());
251 verify(acStateChangePublisher).send(any(AutomationComposition.class), anyInt(),