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.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;
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.supervision.comm.ParticipantSyncPublisher;
37 import org.onap.policy.clamp.acm.runtime.util.CommonTestData;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
39 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
40 import org.onap.policy.clamp.models.acm.concepts.DeployState;
41 import org.onap.policy.clamp.models.acm.concepts.LockState;
42 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
43 import org.onap.policy.clamp.models.acm.concepts.SubState;
44 import org.onap.policy.clamp.models.acm.document.concepts.DocMessage;
45 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessageType;
46 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
47 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
49 class SimpleScannerTest {
51 private static final String AC_JSON = "src/test/resources/rest/acm/AutomationCompositionSmoke.json";
52 private static final String ELEMENT_NAME =
53 "org.onap.domain.database.Http_PMSHMicroserviceAutomationCompositionElement";
55 private static final UUID COMPOSITION_ID = UUID.randomUUID();
56 private static final UUID INSTANCE_ID = UUID.randomUUID();
57 private static final Map<String, Object> OUT_PROPERTIES = Map.of("key", "value");
60 void testFailScenario() {
61 var automationComposition = createDeploying();
62 var elementId = automationComposition.getElements().values().iterator().next().getId();
63 var docMessage = new DocMessage();
64 docMessage.setMessageType(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY_ACK);
65 docMessage.setInstanceId(INSTANCE_ID);
66 docMessage.setInstanceElementId(elementId);
67 docMessage.setDeployState(DeployState.UNDEPLOYED);
68 docMessage.setLockState(LockState.NONE);
69 docMessage.setStateChangeResult(StateChangeResult.FAILED);
70 var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
71 var acProvider = mock(AutomationCompositionProvider.class);
72 var simpleScanner = new SimpleScanner(acProvider, mock(ParticipantSyncPublisher.class),
73 acRuntimeParameterGroup);
74 var result = simpleScanner.scanMessage(automationComposition, docMessage);
75 assertTrue(result.isUpdated());
76 assertTrue(result.isToBeSync());
77 assertEquals(docMessage.getDeployState(),
78 automationComposition.getElements().get(elementId).getDeployState());
79 assertEquals(docMessage.getLockState(),
80 automationComposition.getElements().get(elementId).getLockState());
81 assertEquals(docMessage.getStateChangeResult(), automationComposition.getStateChangeResult());
85 void testWithWrongData() {
86 var automationComposition = createDeploying();
87 var elementId = automationComposition.getElements().values().iterator().next().getId();
88 var docMessage = new DocMessage();
89 docMessage.setInstanceId(INSTANCE_ID);
90 docMessage.setInstanceElementId(elementId);
91 docMessage.setStateChangeResult(StateChangeResult.NO_ERROR);
92 docMessage.setDeployState(DeployState.DEPLOYED);
93 docMessage.setLockState(LockState.LOCKED);
94 var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
95 var acProvider = mock(AutomationCompositionProvider.class);
96 var simpleScanner = new SimpleScanner(acProvider, mock(ParticipantSyncPublisher.class),
97 acRuntimeParameterGroup);
100 docMessage.setMessageType(ParticipantMessageType.PARTICIPANT_PRIME_ACK);
101 var result = simpleScanner.scanMessage(automationComposition, docMessage);
102 assertFalse(result.isUpdated());
103 assertFalse(result.isToBeSync());
105 // wrong elementId in outProperties
106 docMessage.setMessageType(ParticipantMessageType.PARTICIPANT_STATUS);
107 docMessage.setInstanceElementId(UUID.randomUUID());
108 docMessage.setOutProperties(OUT_PROPERTIES);
109 result = simpleScanner.scanMessage(automationComposition, docMessage);
110 assertFalse(result.isUpdated());
111 assertFalse(result.isToBeSync());
113 // wrong elementId in StateChange
114 docMessage.setMessageType(ParticipantMessageType.AUTOMATION_COMPOSITION_STATECHANGE_ACK);
115 result = simpleScanner.scanMessage(automationComposition, docMessage);
116 assertFalse(result.isUpdated());
117 assertFalse(result.isToBeSync());
121 void testScanMessageOutProperties() {
122 var automationComposition = createDeploying();
123 var elementId = automationComposition.getElements().values().iterator().next().getId();
124 var docMessage = new DocMessage();
125 docMessage.setMessageType(ParticipantMessageType.PARTICIPANT_STATUS);
126 docMessage.setInstanceId(INSTANCE_ID);
127 docMessage.setInstanceElementId(elementId);
128 docMessage.setOutProperties(Map.of("key", "value"));
129 var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
130 var acProvider = mock(AutomationCompositionProvider.class);
131 var simpleScanner = new SimpleScanner(acProvider, mock(ParticipantSyncPublisher.class),
132 acRuntimeParameterGroup);
133 var result = simpleScanner.scanMessage(automationComposition, docMessage);
134 assertTrue(result.isUpdated());
135 assertTrue(result.isToBeSync());
136 assertEquals(docMessage.getOutProperties(),
137 automationComposition.getElements().get(elementId).getOutProperties());
141 void testScanMessageStateChange() {
142 var automationComposition = createDeploying();
143 var elementId = automationComposition.getElements().values().iterator().next().getId();
144 var docMessage = new DocMessage();
145 docMessage.setMessageType(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY_ACK);
146 docMessage.setStateChangeResult(StateChangeResult.NO_ERROR);
147 docMessage.setInstanceId(INSTANCE_ID);
148 docMessage.setInstanceElementId(elementId);
149 docMessage.setDeployState(DeployState.DEPLOYED);
150 docMessage.setLockState(LockState.LOCKED);
151 var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
152 var acProvider = mock(AutomationCompositionProvider.class);
153 var simpleScanner = new SimpleScanner(acProvider, mock(ParticipantSyncPublisher.class),
154 acRuntimeParameterGroup);
155 var result = simpleScanner.scanMessage(automationComposition, docMessage);
156 assertTrue(result.isUpdated());
157 assertFalse(result.isToBeSync());
158 assertEquals(docMessage.getDeployState(),
159 automationComposition.getElements().get(elementId).getDeployState());
160 assertEquals(docMessage.getLockState(),
161 automationComposition.getElements().get(elementId).getLockState());
164 private AutomationComposition createDeploying() {
165 var automationComposition = InstantiationUtils.getAutomationCompositionFromResource(AC_JSON, "Crud");
166 automationComposition.setInstanceId(INSTANCE_ID);
167 automationComposition.setDeployState(DeployState.DEPLOYING);
168 automationComposition.setLockState(LockState.NONE);
169 automationComposition.setPhase(0);
170 automationComposition.setLastMsg(TimestampHelper.now());
171 automationComposition.setCompositionId(COMPOSITION_ID);
172 for (var element : automationComposition.getElements().values()) {
173 element.setDeployState(DeployState.DEPLOYING);
174 element.setLockState(LockState.NONE);
176 return automationComposition;
180 void testSendAutomationCompositionMigratingPrecheck() {
181 var automationComposition = InstantiationUtils.getAutomationCompositionFromResource(AC_JSON, "Crud");
182 automationComposition.setLockState(LockState.LOCKED);
183 automationComposition.setDeployState(DeployState.DEPLOYED);
184 automationComposition.setSubState(SubState.MIGRATION_PRECHECKING);
185 for (var element : automationComposition.getElements().values()) {
186 element.setDeployState(DeployState.DEPLOYED);
187 element.setSubState(SubState.NONE);
188 element.setLockState(LockState.LOCKED);
189 if (ELEMENT_NAME.equals(element.getDefinition().getName())) {
190 element.setSubState(SubState.MIGRATION_PRECHECKING);
193 testSimpleScan(automationComposition, element -> element.setSubState(SubState.NONE));
197 void testSendAutomationCompositionPrepare() {
198 var automationComposition = InstantiationUtils.getAutomationCompositionFromResource(AC_JSON, "Crud");
199 automationComposition.setLockState(LockState.NONE);
200 automationComposition.setDeployState(DeployState.UNDEPLOYED);
201 automationComposition.setSubState(SubState.PREPARING);
202 for (var element : automationComposition.getElements().values()) {
203 element.setDeployState(DeployState.UNDEPLOYED);
204 element.setSubState(SubState.NONE);
205 element.setLockState(LockState.NONE);
206 if (ELEMENT_NAME.equals(element.getDefinition().getName())) {
207 element.setSubState(SubState.PREPARING);
210 testSimpleScan(automationComposition, element -> element.setSubState(SubState.NONE));
214 void testSendAutomationCompositionUpdate() {
215 var automationComposition = InstantiationUtils.getAutomationCompositionFromResource(AC_JSON, "Crud");
216 automationComposition.setLockState(LockState.LOCKED);
217 automationComposition.setDeployState(DeployState.UPDATING);
218 for (var element : automationComposition.getElements().values()) {
219 element.setSubState(SubState.NONE);
220 element.setLockState(LockState.LOCKED);
221 if (ELEMENT_NAME.equals(element.getDefinition().getName())) {
222 element.setDeployState(DeployState.UPDATING);
224 element.setDeployState(DeployState.DEPLOYED);
227 testSimpleScan(automationComposition, element -> element.setDeployState(DeployState.DEPLOYED));
230 private void testSimpleScan(AutomationComposition automationComposition, Consumer<AutomationCompositionElement> c) {
231 automationComposition.setInstanceId(INSTANCE_ID);
232 automationComposition.setLockState(LockState.NONE);
233 automationComposition.setCompositionId(COMPOSITION_ID);
234 automationComposition.setLastMsg(TimestampHelper.now());
235 var acProvider = mock(AutomationCompositionProvider.class);
236 var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
237 var simpleScanner = new SimpleScanner(acProvider, mock(ParticipantSyncPublisher.class),
238 acRuntimeParameterGroup);
239 simpleScanner.simpleScan(automationComposition, new UpdateSync());
240 verify(acProvider, times(0)).updateAutomationComposition(any());
242 automationComposition.getElements().values().forEach(c);
243 simpleScanner.simpleScan(automationComposition, new UpdateSync());
244 verify(acProvider).updateAutomationComposition(any());