Fix issue after fail scenario in ACM-intermediary

Issue-ID: POLICY-5173
Change-Id: I8a6c4ca0650935c5e651c5d9274a082d7707f99b
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionOutHandler.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionOutHandler.java
index 7cf83db..d9f2b7a 100644
--- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionOutHandler.java
+++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionOutHandler.java
@@ -162,10 +162,10 @@
                 LOGGER.warn("SubState has always NO_ERROR result!");
             }
         } else if (deployState != null) {
-            handleDeployState(automationComposition, element, deployState);
+            handleDeployState(automationComposition, element, deployState, stateChangeResult);
         }
         if (lockState != null) {
-            handleLockState(automationComposition, element, lockState);
+            handleLockState(automationComposition, element, lockState, stateChangeResult);
         }
 
         var automationCompositionStateChangeAck =
@@ -186,9 +186,12 @@
     }
 
     private void handleDeployState(AutomationComposition automationComposition, AutomationCompositionElement element,
-            DeployState deployState) {
+            DeployState deployState, StateChangeResult stateChangeResult) {
         element.setDeployState(deployState);
         element.setLockState(DeployState.DEPLOYED.equals(element.getDeployState()) ? LockState.LOCKED : LockState.NONE);
+        if (StateChangeResult.FAILED.equals(stateChangeResult)) {
+            return;
+        }
         var checkOpt = automationComposition.getElements().values().stream()
                 .filter(acElement -> !deployState.equals(acElement.getDeployState())).findAny();
         if (checkOpt.isEmpty()) {
@@ -209,8 +212,11 @@
     }
 
     private void handleLockState(AutomationComposition automationComposition, AutomationCompositionElement element,
-            LockState lockState) {
+            LockState lockState, StateChangeResult stateChangeResult) {
         element.setLockState(lockState);
+        if (StateChangeResult.FAILED.equals(stateChangeResult)) {
+            return;
+        }
         var checkOpt = automationComposition.getElements().values().stream()
                 .filter(acElement -> !lockState.equals(acElement.getLockState())).findAny();
         if (checkOpt.isEmpty()) {
diff --git a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionOutHandlerTest.java b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionOutHandlerTest.java
index 202f25c..ce62fcb 100644
--- a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionOutHandlerTest.java
+++ b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionOutHandlerTest.java
@@ -311,4 +311,28 @@
                 .sendAutomationCompositionAck(any(AutomationCompositionDeployAck.class));
         assertEquals(compositionTarget, automationComposition.getCompositionId());
     }
+
+    @Test
+    void updateFailMigrationTest() {
+        var cacheProvider = mock(CacheProvider.class);
+        when(cacheProvider.getParticipantId()).thenReturn(UUID.randomUUID());
+        var publisher = mock(ParticipantMessagePublisher.class);
+        var acOutHandler = new AutomationCompositionOutHandler(publisher, cacheProvider);
+
+        var automationComposition = CommonTestData.getTestAutomationCompositionMap().values().iterator().next();
+        when(cacheProvider.getAutomationComposition(automationComposition.getInstanceId()))
+                .thenReturn(automationComposition);
+
+        automationComposition.setCompositionTargetId(UUID.randomUUID());
+        automationComposition.setDeployState(DeployState.MIGRATING);
+        var compositionId = automationComposition.getCompositionId();
+        for (var element : automationComposition.getElements().values()) {
+            element.setDeployState(DeployState.MIGRATING);
+            acOutHandler.updateAutomationCompositionElementState(automationComposition.getInstanceId(),
+                    element.getId(), DeployState.DEPLOYED, null, StateChangeResult.FAILED, "");
+        }
+        verify(publisher, times(automationComposition.getElements().size()))
+                .sendAutomationCompositionAck(any(AutomationCompositionDeployAck.class));
+        assertEquals(compositionId, automationComposition.getCompositionId());
+    }
 }