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