c82969d47a35f03413bc4d7b9753424ff77561f4
[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.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30
31 import java.util.Map;
32 import java.util.UUID;
33 import java.util.function.Consumer;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.clamp.acm.runtime.instantiation.InstantiationUtils;
36 import org.onap.policy.clamp.acm.runtime.main.utils.EncryptionUtils;
37 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantSyncPublisher;
38 import org.onap.policy.clamp.acm.runtime.util.CommonTestData;
39 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
40 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
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.concepts.StateChangeResult;
44 import org.onap.policy.clamp.models.acm.concepts.SubState;
45 import org.onap.policy.clamp.models.acm.document.concepts.DocMessage;
46 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessageType;
47 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
48 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
49
50 class SimpleScannerTest {
51
52     private static final String AC_JSON = "src/test/resources/rest/acm/AutomationCompositionSmoke.json";
53     private static final String ELEMENT_NAME =
54             "org.onap.domain.database.Http_PMSHMicroserviceAutomationCompositionElement";
55
56     private static final UUID COMPOSITION_ID = UUID.randomUUID();
57     private static final UUID INSTANCE_ID = UUID.randomUUID();
58     private static final Map<String, Object> OUT_PROPERTIES = Map.of("key", "value");
59
60     @Test
61     void testFailScenario() {
62         var automationComposition = createDeploying();
63         var elementId = automationComposition.getElements().values().iterator().next().getId();
64         var docMessage = new DocMessage();
65         docMessage.setMessageType(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY_ACK);
66         docMessage.setInstanceId(INSTANCE_ID);
67         docMessage.setInstanceElementId(elementId);
68         docMessage.setDeployState(DeployState.UNDEPLOYED);
69         docMessage.setLockState(LockState.NONE);
70         docMessage.setStateChangeResult(StateChangeResult.FAILED);
71         var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
72         var acProvider = mock(AutomationCompositionProvider.class);
73         var encryptionUtils = new EncryptionUtils(acRuntimeParameterGroup);
74         var simpleScanner = new SimpleScanner(acProvider, mock(ParticipantSyncPublisher.class),
75                 acRuntimeParameterGroup, encryptionUtils);
76         var result = simpleScanner.scanMessage(automationComposition, docMessage);
77         assertTrue(result.isUpdated());
78         assertTrue(result.isToBeSync());
79         assertEquals(docMessage.getDeployState(),
80                 automationComposition.getElements().get(elementId).getDeployState());
81         assertEquals(docMessage.getLockState(),
82                 automationComposition.getElements().get(elementId).getLockState());
83         assertEquals(docMessage.getStateChangeResult(), automationComposition.getStateChangeResult());
84     }
85
86     @Test
87     void testWithWrongData() {
88         var automationComposition = createDeploying();
89         var elementId = automationComposition.getElements().values().iterator().next().getId();
90         var docMessage = new DocMessage();
91         docMessage.setInstanceId(INSTANCE_ID);
92         docMessage.setInstanceElementId(elementId);
93         docMessage.setStateChangeResult(StateChangeResult.NO_ERROR);
94         docMessage.setDeployState(DeployState.DEPLOYED);
95         docMessage.setLockState(LockState.LOCKED);
96         var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
97         var acProvider = mock(AutomationCompositionProvider.class);
98         var encryptionUtils = new EncryptionUtils(acRuntimeParameterGroup);
99         var simpleScanner = new SimpleScanner(acProvider, mock(ParticipantSyncPublisher.class),
100                 acRuntimeParameterGroup, encryptionUtils);
101
102         // wrong MessageType
103         docMessage.setMessageType(ParticipantMessageType.PARTICIPANT_PRIME_ACK);
104         var result = simpleScanner.scanMessage(automationComposition, docMessage);
105         assertFalse(result.isUpdated());
106         assertFalse(result.isToBeSync());
107
108         // wrong elementId in outProperties
109         docMessage.setMessageType(ParticipantMessageType.PARTICIPANT_STATUS);
110         docMessage.setInstanceElementId(UUID.randomUUID());
111         docMessage.setOutProperties(OUT_PROPERTIES);
112         result = simpleScanner.scanMessage(automationComposition, docMessage);
113         assertFalse(result.isUpdated());
114         assertFalse(result.isToBeSync());
115
116         // wrong elementId in StateChange
117         docMessage.setMessageType(ParticipantMessageType.AUTOMATION_COMPOSITION_STATECHANGE_ACK);
118         result = simpleScanner.scanMessage(automationComposition, docMessage);
119         assertFalse(result.isUpdated());
120         assertFalse(result.isToBeSync());
121     }
122
123     @Test
124     void testScanMessageOutProperties() {
125         var automationComposition = createDeploying();
126         var elementId = automationComposition.getElements().values().iterator().next().getId();
127         var docMessage = new DocMessage();
128         docMessage.setMessageType(ParticipantMessageType.PARTICIPANT_STATUS);
129         docMessage.setInstanceId(INSTANCE_ID);
130         docMessage.setInstanceElementId(elementId);
131         docMessage.setOutProperties(Map.of("key", "value"));
132         var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
133         var acProvider = mock(AutomationCompositionProvider.class);
134         var encryptionUtils = new EncryptionUtils(acRuntimeParameterGroup);
135         var simpleScanner = new SimpleScanner(acProvider, mock(ParticipantSyncPublisher.class),
136                 acRuntimeParameterGroup, encryptionUtils);
137         var result = simpleScanner.scanMessage(automationComposition, docMessage);
138         assertTrue(result.isUpdated());
139         assertTrue(result.isToBeSync());
140         assertEquals(docMessage.getOutProperties(),
141                 automationComposition.getElements().get(elementId).getOutProperties());
142     }
143
144     @Test
145     void testScanMessageStateChange() {
146         var automationComposition = createDeploying();
147         var elementId = automationComposition.getElements().values().iterator().next().getId();
148         var docMessage = new DocMessage();
149         docMessage.setMessageType(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY_ACK);
150         docMessage.setStateChangeResult(StateChangeResult.NO_ERROR);
151         docMessage.setInstanceId(INSTANCE_ID);
152         docMessage.setInstanceElementId(elementId);
153         docMessage.setDeployState(DeployState.DEPLOYED);
154         docMessage.setLockState(LockState.LOCKED);
155         var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
156         var acProvider = mock(AutomationCompositionProvider.class);
157         var encryptionUtils = new EncryptionUtils(acRuntimeParameterGroup);
158         var simpleScanner = new SimpleScanner(acProvider, mock(ParticipantSyncPublisher.class),
159                 acRuntimeParameterGroup, encryptionUtils);
160         var result = simpleScanner.scanMessage(automationComposition, docMessage);
161         assertTrue(result.isUpdated());
162         assertFalse(result.isToBeSync());
163         assertEquals(docMessage.getDeployState(),
164                 automationComposition.getElements().get(elementId).getDeployState());
165         assertEquals(docMessage.getLockState(),
166                 automationComposition.getElements().get(elementId).getLockState());
167     }
168
169     private AutomationComposition createDeploying() {
170         var automationComposition = InstantiationUtils.getAutomationCompositionFromResource(AC_JSON, "Crud");
171         automationComposition.setInstanceId(INSTANCE_ID);
172         automationComposition.setDeployState(DeployState.DEPLOYING);
173         automationComposition.setLockState(LockState.NONE);
174         automationComposition.setPhase(0);
175         automationComposition.setLastMsg(TimestampHelper.now());
176         automationComposition.setCompositionId(COMPOSITION_ID);
177         for (var element : automationComposition.getElements().values()) {
178             element.setDeployState(DeployState.DEPLOYING);
179             element.setLockState(LockState.NONE);
180         }
181         return automationComposition;
182     }
183
184     @Test
185     void testSendAutomationCompositionMigratingPrecheck() {
186         var automationComposition = InstantiationUtils.getAutomationCompositionFromResource(AC_JSON, "Crud");
187         automationComposition.setLockState(LockState.LOCKED);
188         automationComposition.setDeployState(DeployState.DEPLOYED);
189         automationComposition.setSubState(SubState.MIGRATION_PRECHECKING);
190         for (var element : automationComposition.getElements().values()) {
191             element.setDeployState(DeployState.DEPLOYED);
192             element.setSubState(SubState.NONE);
193             element.setLockState(LockState.LOCKED);
194             if (ELEMENT_NAME.equals(element.getDefinition().getName())) {
195                 element.setSubState(SubState.MIGRATION_PRECHECKING);
196             }
197         }
198         testSimpleScan(automationComposition, element -> element.setSubState(SubState.NONE));
199     }
200
201     @Test
202     void testSendAutomationCompositionPrepare() {
203         var automationComposition = InstantiationUtils.getAutomationCompositionFromResource(AC_JSON, "Crud");
204         automationComposition.setLockState(LockState.NONE);
205         automationComposition.setDeployState(DeployState.UNDEPLOYED);
206         automationComposition.setSubState(SubState.PREPARING);
207         for (var element : automationComposition.getElements().values()) {
208             element.setDeployState(DeployState.UNDEPLOYED);
209             element.setSubState(SubState.NONE);
210             element.setLockState(LockState.NONE);
211             if (ELEMENT_NAME.equals(element.getDefinition().getName())) {
212                 element.setSubState(SubState.PREPARING);
213             }
214         }
215         testSimpleScan(automationComposition, element -> element.setSubState(SubState.NONE));
216     }
217
218     @Test
219     void testSendAutomationCompositionUpdate() {
220         var automationComposition = InstantiationUtils.getAutomationCompositionFromResource(AC_JSON, "Crud");
221         automationComposition.setLockState(LockState.LOCKED);
222         automationComposition.setDeployState(DeployState.UPDATING);
223         for (var element : automationComposition.getElements().values()) {
224             element.setSubState(SubState.NONE);
225             element.setLockState(LockState.LOCKED);
226             if (ELEMENT_NAME.equals(element.getDefinition().getName())) {
227                 element.setDeployState(DeployState.UPDATING);
228             } else {
229                 element.setDeployState(DeployState.DEPLOYED);
230             }
231         }
232         testSimpleScan(automationComposition, element -> element.setDeployState(DeployState.DEPLOYED));
233     }
234
235     private void testSimpleScan(AutomationComposition automationComposition, Consumer<AutomationCompositionElement> c) {
236         automationComposition.setInstanceId(INSTANCE_ID);
237         automationComposition.setLockState(LockState.NONE);
238         automationComposition.setCompositionId(COMPOSITION_ID);
239         automationComposition.setLastMsg(TimestampHelper.now());
240         var acProvider = mock(AutomationCompositionProvider.class);
241         var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
242         var encryptionUtils = new EncryptionUtils(acRuntimeParameterGroup);
243         var simpleScanner = new SimpleScanner(acProvider, mock(ParticipantSyncPublisher.class),
244                 acRuntimeParameterGroup, encryptionUtils);
245         simpleScanner.simpleScan(automationComposition, new UpdateSync());
246         verify(acProvider, times(0)).updateAutomationComposition(any());
247
248         automationComposition.getElements().values().forEach(c);
249         simpleScanner.simpleScan(automationComposition, new UpdateSync());
250         verify(acProvider).updateAutomationComposition(any());
251     }
252 }