077580deda28fd311956d9318b891d9a9bc77e0d
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.runtime.supervision.scanner;
22
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;
31
32 import java.util.List;
33 import java.util.Map;
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.AutomationCompositionMigrationPublisher;
39 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantSyncPublisher;
40 import org.onap.policy.clamp.acm.runtime.util.CommonTestData;
41 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
42 import org.onap.policy.clamp.models.acm.concepts.DeployState;
43 import org.onap.policy.clamp.models.acm.concepts.LockState;
44 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
45 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
46
47 class StageScannerTest {
48     private static final String AC_JSON = "src/test/resources/rest/acm/AutomationCompositionSmoke.json";
49     private static final UUID COMPOSITION_ID = UUID.randomUUID();
50
51     @Test
52     void testSendAutomationCompositionMigrate() {
53         var automationComposition = InstantiationUtils.getAutomationCompositionFromResource(AC_JSON, "Crud");
54         automationComposition.setInstanceId(UUID.randomUUID());
55         automationComposition.setDeployState(DeployState.MIGRATING);
56         automationComposition.setCompositionId(COMPOSITION_ID);
57         var compositionTargetId = UUID.randomUUID();
58         automationComposition.setCompositionTargetId(compositionTargetId);
59         automationComposition.setLockState(LockState.LOCKED);
60         automationComposition.setLastMsg(TimestampHelper.now());
61         automationComposition.setPhase(0);
62         for (var element : automationComposition.getElements().values()) {
63             element.setDeployState(DeployState.DEPLOYED);
64             element.setLockState(LockState.LOCKED);
65         }
66         // first element is not migrated yet
67         var element = automationComposition.getElements().entrySet().iterator().next().getValue();
68         element.setDeployState(DeployState.MIGRATING);
69
70         var acProvider = mock(AutomationCompositionProvider.class);
71         when(acProvider.updateAutomationComposition(any())).thenReturn(automationComposition);
72
73         var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
74         var encryptionUtils = new EncryptionUtils(acRuntimeParameterGroup);
75         var supervisionScanner = new StageScanner(acProvider, mock(ParticipantSyncPublisher.class),
76                 mock(AutomationCompositionMigrationPublisher.class), acRuntimeParameterGroup, encryptionUtils);
77
78         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
79         supervisionScanner.scanStage(automationComposition, serviceTemplate, new UpdateSync());
80         verify(acProvider, times(0)).updateAutomationComposition(any(AutomationComposition.class));
81         assertEquals(DeployState.MIGRATING, automationComposition.getDeployState());
82
83         // send message for next stage
84         clearInvocations(acProvider);
85         var toscaNodeTemplate = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
86                 .get(element.getDefinition().getName());
87         toscaNodeTemplate.setProperties(Map.of("stage", List.of(1)));
88
89         supervisionScanner.scanStage(automationComposition, serviceTemplate, new UpdateSync());
90         verify(acProvider).updateAutomationComposition(any(AutomationComposition.class));
91         assertEquals(DeployState.MIGRATING, automationComposition.getDeployState());
92
93         // first element is migrated
94         clearInvocations(acProvider);
95         element.setDeployState(DeployState.DEPLOYED);
96         supervisionScanner.scanStage(automationComposition, serviceTemplate, new UpdateSync());
97         verify(acProvider).updateAutomationComposition(any(AutomationComposition.class));
98
99         assertEquals(DeployState.DEPLOYED, automationComposition.getDeployState());
100         assertEquals(compositionTargetId, automationComposition.getCompositionId());
101     }
102 }