/*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2022-2023 Nordix Foundation
+ *  Copyright (C) 2022-2024 Nordix Foundation
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
     private void handleTaskCompletion(final Object response, final Throwable throwable) {
         if (throwable != null) {
             if (throwable instanceof TimeoutException) {
-                log.warn("Async task didn't completed within the required time.");
+                log.error("Async task didn't completed within the required time.", throwable);
             } else {
-                log.debug("Watchdog async batch failed. caused by : {}", throwable.getMessage());
+                log.error("Watchdog async batch failed.", throwable);
             }
         }
     }
 
 import org.onap.cps.ncmp.impl.inventory.models.CmHandleState
 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle
 import org.onap.cps.ncmp.impl.inventory.sync.lcm.LcmEventsCmHandleStateHandler
+import org.onap.cps.spi.exceptions.DataNodeNotFoundException
 import org.onap.cps.spi.model.DataNode
 import org.slf4j.LoggerFactory
 import spock.lang.Specification
             'module upgrade' | MODULE_UPGRADE        | 'Upgrade in progress'                          || MODULE_UPGRADE_FAILED
     }
 
+    // TODO Update this test once the bug CPS-2474 is fixed
+    def 'Module sync fails if a handle gets deleted during module sync.'() {
+        given: 'cm handles in an ADVISED state'
+            def cmHandle1 = cmHandleAsDataNodeByIdAndState('cm-handle-1', CmHandleState.ADVISED)
+            def cmHandle2 = cmHandleAsDataNodeByIdAndState('cm-handle-2', CmHandleState.ADVISED)
+        and: 'inventory persistence returns the first handle with ADVISED state'
+            mockInventoryPersistence.getCmHandleState('cm-handle-1') >> new CompositeState(cmHandleState: CmHandleState.ADVISED)
+        and: 'inventory persistence cannot find the second handle'
+            mockInventoryPersistence.getCmHandleState('cm-handle-2') >> { throw new DataNodeNotFoundException('dataspace', 'anchor', 'xpath') }
+        when: 'module sync poll is executed'
+            objectUnderTest.performModuleSync([cmHandle1, cmHandle2], batchCount)
+        then: 'an exception is thrown'
+            thrown(DataNodeNotFoundException)
+        and: 'even though the existing cm-handle did sync'
+            1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(_) >> { args -> assert args[0].id == 'cm-handle-1' }
+        and: 'logs report the cm-handle is in READY state'
+            assert getLoggingEvent().formattedMessage == 'cm-handle-1 is now in READY state'
+        and: 'this is impossible as the state handler was not called at all'
+            0 * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch(_)
+    }
 
     def 'Reset failed CM Handles #scenario.'() {
         given: 'cm handles in an locked state'
 
            END
     END
 
-Delete cm handle
-    ${uri}=              Set Variable       ${ncmpInventoryBasePath}/v1/ch
-    ${headers}=          Create Dictionary  Content-Type=application/json   Authorization=${auth}
-    ${response}=         POST On Session    CPS_URL   ${uri}   headers=${headers}   data=${deletePayload}
-    Should Be Equal As Strings              ${response.status_code}   200
-
-Get cm handle details and confirm it has been deleted
-    ${uri}=              Set Variable       ${ncmpBasePath}/v1/ch/CmHandleForDelete
-    ${headers}=          Create Dictionary  Authorization=${auth}
-    ${response}=         GET On Session     CPS_URL   ${uri}   headers=${headers}   expected_status=404
-
 Check if ietfYang-PNFDemo is READY
     ${uri}=        Set Variable       ${ncmpBasePath}/v1/ch/ietfYang-PNFDemo
     ${headers}=    Create Dictionary  Authorization=${auth}
             END
     END
 
+Delete cm handle
+    ${uri}=              Set Variable       ${ncmpInventoryBasePath}/v1/ch
+    ${headers}=          Create Dictionary  Content-Type=application/json   Authorization=${auth}
+    ${response}=         POST On Session    CPS_URL   ${uri}   headers=${headers}   data=${deletePayload}
+    Should Be Equal As Strings              ${response.status_code}   200
+
+Get cm handle details and confirm it has been deleted
+    ${uri}=              Set Variable       ${ncmpBasePath}/v1/ch/CmHandleForDelete
+    ${headers}=          Create Dictionary  Authorization=${auth}
+    ${response}=         GET On Session     CPS_URL   ${uri}   headers=${headers}   expected_status=404
+
 *** Keywords ***
 
 Is CM Handle READY
     [Arguments]    ${response}
     ${json_data}=    Evaluate    json.loads('${response.content.decode("utf-8")}')   json
     ${number_of_items}=    Get Length    ${json_data}
-    RETURN    ${number_of_items}
\ No newline at end of file
+    RETURN    ${number_of_items}