2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved.
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.Mockito.clearInvocations;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30 import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_SERVICE_TEMPLATE_YAML;
32 import java.util.List;
34 import java.util.UUID;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.clamp.acm.runtime.instantiation.InstantiationUtils;
37 import org.onap.policy.clamp.acm.runtime.main.utils.EncryptionUtils;
38 import org.onap.policy.clamp.acm.runtime.supervision.comm.AcPreparePublisher;
39 import org.onap.policy.clamp.acm.runtime.supervision.comm.AutomationCompositionMigrationPublisher;
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.DeployState;
44 import org.onap.policy.clamp.models.acm.concepts.LockState;
45 import org.onap.policy.clamp.models.acm.concepts.SubState;
46 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
47 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
49 class StageScannerTest {
50 private static final String AC_JSON = "src/test/resources/rest/acm/AutomationCompositionSmoke.json";
51 private static final UUID COMPOSITION_ID = UUID.randomUUID();
54 void testSendAutomationCompositionMigrate() {
55 var automationComposition = InstantiationUtils.getAutomationCompositionFromResource(AC_JSON, "Crud");
56 automationComposition.setInstanceId(UUID.randomUUID());
57 automationComposition.setDeployState(DeployState.MIGRATING);
58 automationComposition.setCompositionId(COMPOSITION_ID);
59 var compositionTargetId = UUID.randomUUID();
60 automationComposition.setCompositionTargetId(compositionTargetId);
61 automationComposition.setLockState(LockState.LOCKED);
62 automationComposition.setLastMsg(TimestampHelper.now());
63 automationComposition.setPhase(0);
64 for (var element : automationComposition.getElements().values()) {
65 element.setDeployState(DeployState.DEPLOYED);
66 element.setLockState(LockState.LOCKED);
68 // first element is not migrated yet
69 var element = automationComposition.getElements().entrySet().iterator().next().getValue();
70 element.setDeployState(DeployState.MIGRATING);
72 var acProvider = mock(AutomationCompositionProvider.class);
73 when(acProvider.updateAutomationComposition(any())).thenReturn(automationComposition);
74 var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
75 var encryptionUtils = new EncryptionUtils(acRuntimeParameterGroup);
76 var supervisionScanner = new StageScanner(acProvider, mock(ParticipantSyncPublisher.class),
77 mock(AutomationCompositionMigrationPublisher.class), mock(AcPreparePublisher.class),
78 acRuntimeParameterGroup, encryptionUtils);
79 var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
80 supervisionScanner.scanStage(automationComposition, serviceTemplate, new UpdateSync());
81 verify(acProvider, times(0)).updateAutomationComposition(any(AutomationComposition.class));
82 assertEquals(DeployState.MIGRATING, automationComposition.getDeployState());
84 // send message for next stage
85 clearInvocations(acProvider);
86 var toscaNodeTemplate = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
87 .get(element.getDefinition().getName());
88 toscaNodeTemplate.setProperties(Map.of("stage", List.of(1)));
90 supervisionScanner.scanStage(automationComposition, serviceTemplate, new UpdateSync());
91 verify(acProvider).updateAutomationComposition(any(AutomationComposition.class));
92 assertEquals(DeployState.MIGRATING, automationComposition.getDeployState());
94 // first element is migrated
95 clearInvocations(acProvider);
96 element.setDeployState(DeployState.DEPLOYED);
97 supervisionScanner.scanStage(automationComposition, serviceTemplate, new UpdateSync());
98 verify(acProvider).updateAutomationComposition(any(AutomationComposition.class));
100 assertEquals(DeployState.DEPLOYED, automationComposition.getDeployState());
101 assertEquals(compositionTargetId, automationComposition.getCompositionId());
105 void testSendAutomationCompositionPrepare() {
106 var automationComposition = InstantiationUtils.getAutomationCompositionFromResource(AC_JSON, "Crud");
107 automationComposition.setDeployState(DeployState.UNDEPLOYED);
108 automationComposition.setSubState(SubState.PREPARING);
109 automationComposition.setLockState(LockState.NONE);
110 automationComposition.setCompositionId(COMPOSITION_ID);
111 automationComposition.setLastMsg(TimestampHelper.now());
112 automationComposition.setPhase(0);
113 for (var element : automationComposition.getElements().values()) {
114 element.setDeployState(DeployState.UNDEPLOYED);
115 element.setLockState(LockState.NONE);
116 element.setSubState(SubState.NONE);
118 // first element is not prepared yet
119 var element = automationComposition.getElements().entrySet().iterator().next().getValue();
120 element.setSubState(SubState.PREPARING);
122 var acProvider = mock(AutomationCompositionProvider.class);
123 when(acProvider.updateAutomationComposition(any())).thenReturn(automationComposition);
125 var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
126 var encryptionUtils = new EncryptionUtils(acRuntimeParameterGroup);
127 var supervisionScanner = new StageScanner(acProvider, mock(ParticipantSyncPublisher.class),
128 mock(AutomationCompositionMigrationPublisher.class), mock(AcPreparePublisher.class),
129 acRuntimeParameterGroup, encryptionUtils);
131 var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
132 supervisionScanner.scanStage(automationComposition, serviceTemplate, new UpdateSync());
133 verify(acProvider, times(0)).updateAutomationComposition(any(AutomationComposition.class));
134 assertEquals(SubState.PREPARING, automationComposition.getSubState());
136 // send message for next stage
137 clearInvocations(acProvider);
138 var toscaNodeTemplate = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
139 .get(element.getDefinition().getName());
140 var prepare = Map.of("prepare", List.of(1));
141 toscaNodeTemplate.setProperties(Map.of("stage", prepare));
143 supervisionScanner.scanStage(automationComposition, serviceTemplate, new UpdateSync());
144 verify(acProvider).updateAutomationComposition(any(AutomationComposition.class));
145 assertEquals(SubState.PREPARING, automationComposition.getSubState());
147 // first element is prepared
148 clearInvocations(acProvider);
149 element.setSubState(SubState.NONE);
150 supervisionScanner.scanStage(automationComposition, serviceTemplate, new UpdateSync());
151 verify(acProvider).updateAutomationComposition(any(AutomationComposition.class));
153 assertEquals(SubState.NONE, automationComposition.getSubState());