Merge "Produce subscription create event for subscription-topic: cm-avc-subscription"
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java
index a5bd606..59b960a 100755
--- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java
+++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java
@@ -327,9 +327,10 @@
             final List<String> tobeRemovedCmHandles) {
         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
                 new ArrayList<>(tobeRemovedCmHandles.size());
-        final List<YangModelCmHandle> yangModelCmHandles =
-                tobeRemovedCmHandles.stream().map(inventoryPersistence::getYangModelCmHandle)
-                        .collect(Collectors.toList());
+        final Map<String, YangModelCmHandle> cmHandleIdToYangModelCmHandleMap = tobeRemovedCmHandles.stream()
+            .collect(Collectors.toMap(cmHandleId -> cmHandleId, inventoryPersistence::getYangModelCmHandle));
+
+        final Collection<YangModelCmHandle> yangModelCmHandles = cmHandleIdToYangModelCmHandleMap.values();
         updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETING);
 
         for (final List<String> tobeRemovedCmHandleBatch : Lists.partition(tobeRemovedCmHandles, DELETE_BATCH_SIZE)) {
@@ -348,7 +349,15 @@
             }
         }
 
-        updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETED);
+        final Collection<YangModelCmHandle> deletedYangModelCmHandles =
+            cmHandleRegistrationResponses.stream()
+                .filter(cmHandleRegistrationResponse ->
+                    cmHandleRegistrationResponse.getStatus().equals(CmHandleRegistrationResponse.Status.SUCCESS))
+                .map(CmHandleRegistrationResponse::getCmHandle)
+                .map(cmHandleIdToYangModelCmHandleMap::get)
+                .collect(Collectors.toList());
+        updateCmHandleStateBatch(deletedYangModelCmHandles, CmHandleState.DELETED);
+
         return cmHandleRegistrationResponses;
     }
 
@@ -372,11 +381,10 @@
         }
     }
 
-    private void updateCmHandleStateBatch(final List<YangModelCmHandle> yangModelCmHandles,
+    private void updateCmHandleStateBatch(final Collection<YangModelCmHandle> yangModelCmHandles,
                                           final CmHandleState cmHandleState) {
         final Map<YangModelCmHandle, CmHandleState> cmHandleIdsToBeRemoved = new HashMap<>();
-        yangModelCmHandles.stream().forEach(yangModelCmHandle ->
-                cmHandleIdsToBeRemoved.put(yangModelCmHandle, cmHandleState));
+        yangModelCmHandles.forEach(yangModelCmHandle -> cmHandleIdsToBeRemoved.put(yangModelCmHandle, cmHandleState));
         lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleIdsToBeRemoved);
     }
 
diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplRegistrationSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplRegistrationSpec.groovy
index 5824a47..e8c5373 100644
--- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplRegistrationSpec.groovy
+++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplRegistrationSpec.groovy
@@ -29,6 +29,7 @@
 import org.onap.cps.ncmp.api.impl.event.lcm.LcmEventsCmHandleStateHandler
 import org.onap.cps.ncmp.api.impl.exception.DmiRequestException
 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations
+import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
 import org.onap.cps.ncmp.api.inventory.CmHandleQueries
 import org.onap.cps.ncmp.api.inventory.CmHandleState
 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
@@ -73,9 +74,10 @@
             dmiRegistration.setCreatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-1', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
             dmiRegistration.setUpdatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-2', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
             dmiRegistration.setRemovedCmHandles(['cmhandle-2'])
+        and: 'any cm handle is persisted'
+            mockInventoryPersistence.getYangModelCmHandle(_) >> new YangModelCmHandle()
         when: 'registration is processed'
             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiRegistration)
-            // Spock validated invocation order between multiple then blocks
         then: 'cm-handles are removed first'
             1 * objectUnderTest.parseAndRemoveCmHandlesInDmiRegistration(*_)
         and: 'de-registered cm handle entry is removed from in progress map'
@@ -104,9 +106,9 @@
         when: 'registration is processed'
             def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiRegistration)
         then: 'response has values from all operations'
-            response.getRemovedCmHandles() == removeResponses
-            response.getCreatedCmHandles() == createdResponses
-            response.getUpdatedCmHandles() == updateResponses
+            response.removedCmHandles == removeResponses
+            response.createdCmHandles == createdResponses
+            response.updatedCmHandles == updateResponses
     }
 
     def 'Create CM-handle Validation: Registration with valid Service names: #scenario'() {
@@ -156,8 +158,8 @@
         when: 'registration is updated'
             def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
         then: 'a successful response is received'
-            response.getCreatedCmHandles().size() == 1
-            with(response.getCreatedCmHandles().get(0)) {
+            response.createdCmHandles.size() == 1
+            with(response.createdCmHandles[0]) {
                 assert it.status == Status.SUCCESS
                 assert it.cmHandle == 'cmhandle'
             }
@@ -193,9 +195,9 @@
         when: 'registration is updated to create cm-handles'
             def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
         then: 'a response is received for all cm-handles'
-            response.getCreatedCmHandles().size() == 1
+            response.createdCmHandles.size() == 1
         and: 'all cm-handles creation fails'
-            response.getCreatedCmHandles().each {
+            response.createdCmHandles.each {
                 assert it.cmHandle == 'cmhandle2'
                 assert it.status == Status.FAILURE
                 assert it.registrationError == CM_HANDLE_ALREADY_EXIST
@@ -206,29 +208,28 @@
     def 'Create CM-Handle Error Handling: Registration fails: #scenario'() {
         given: 'a registration without cm-handle properties'
             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
-            dmiPluginRegistration.createdCmHandles = [new NcmpServiceCmHandle(cmHandleId: cmHandleId)]
+            dmiPluginRegistration.createdCmHandles = [new NcmpServiceCmHandle(cmHandleId: 'cmhandle')]
         and: 'cm-handler registration fails: #scenario'
             mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch(*_) >> { throw exception }
         when: 'registration is updated'
             def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
         then: 'a failure response is received'
-            response.getCreatedCmHandles().size() == 1
-            with(response.getCreatedCmHandles().get(0)) {
+            response.createdCmHandles.size() == 1
+            with(response.createdCmHandles[0]) {
                 assert it.status == Status.FAILURE
-                assert it.cmHandle ==  cmHandleId
+                assert it.cmHandle ==  'cmhandle'
                 assert it.registrationError == expectedError
                 assert it.errorText == expectedErrorText
             }
         where:
-            scenario                                        | cmHandleId             | exception                                                                  || expectedError           | expectedErrorText
-            'cm-handle already exist'                       | 'cmhandle'             | new AlreadyDefinedExceptionBatch(["path[@id='${cmHandleId}']".toString()]) || CM_HANDLE_ALREADY_EXIST | 'cm-handle already exists'
-            'unknown exception while registering cm-handle' | 'cmhandle'             | new RuntimeException('Failed')                                             || UNKNOWN_ERROR           | 'Failed'
+            scenario                                        | exception                                                              || expectedError           | expectedErrorText
+            'cm-handle already exist'                       | new AlreadyDefinedExceptionBatch(["'path[@id='cmhandle']".toString()]) || CM_HANDLE_ALREADY_EXIST | 'cm-handle already exists'
+            'unknown exception while registering cm-handle' | new RuntimeException('Failed')                                         || UNKNOWN_ERROR           | 'Failed'
     }
 
     def 'Update CM-Handle: Update Operation Response is added to the response'() {
         given: 'a registration to update CmHandles'
-            def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
-                updatedCmHandles: [{}])
+            def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server', updatedCmHandles: [{}])
         and: 'cm-handle updates can be processed successfully'
             def updateOperationResponse = [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-1'),
                                            CmHandleRegistrationResponse.createFailureResponse('cm-handle-2', new Exception("Failed")),
@@ -238,12 +239,13 @@
         when: 'registration is updated'
             def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
         then: 'the response contains updateOperationResponse'
-            assert response.getUpdatedCmHandles().size() == 4
-            assert response.getUpdatedCmHandles().containsAll(updateOperationResponse)
+            assert response.updatedCmHandles.size() == 4
+            assert response.updatedCmHandles.containsAll(updateOperationResponse)
     }
 
     def 'Remove CmHandle Successfully: #scenario'() {
         given: 'a registration'
+            mockInventoryPersistence.getYangModelCmHandle(_) >> new YangModelCmHandle()
             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
                 removedCmHandles: ['cmhandle'])
         and: '#scenario'
@@ -252,14 +254,14 @@
         when: 'registration is updated to delete cmhandle'
             def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
         then: 'the cmHandle state is updated to "DELETING"'
-        1 * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch(_)
+            1 * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch(_)
         and: 'method to delete relevant schema set is called once'
             1 * mockInventoryPersistence.deleteSchemaSetWithCascade(_)
         and: 'method to delete relevant list/list element is called once'
             1 * mockInventoryPersistence.deleteDataNodes(_)
         and: 'successful response is received'
-            assert response.getRemovedCmHandles().size() == 1
-            with(response.getRemovedCmHandles().get(0)) {
+            assert response.removedCmHandles.size() == 1
+            with(response.removedCmHandles[0]) {
                 assert it.status == Status.SUCCESS
                 assert it.cmHandle == 'cmhandle'
             }
@@ -272,42 +274,52 @@
     }
 
     def 'Remove CmHandle: Partial Success'() {
-        given: 'a registration with three cm-handles to be deleted'
+        given: 'some unique yang model cm handles'
+            addPersistedYangModelCmHandles(['cmhandle1', 'cmhandle2', 'cmhandle3'])
+        and: 'a registration with three cm-handles to be deleted'
             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
                 removedCmHandles: ['cmhandle1', 'cmhandle2', 'cmhandle3'])
         and: 'cm-handle deletion fails on batch'
             mockInventoryPersistence.deleteDataNodes(_) >> { throw new RuntimeException("Failed") }
         and: 'cm-handle deletion is successful for 1st and 3rd; failed for 2nd'
-            mockInventoryPersistence.deleteDataNode(_) >> {} >> { throw new RuntimeException("Failed") } >> {}
+            mockInventoryPersistence.deleteDataNode("/dmi-registry/cm-handles[@id='cmhandle2']") >> { throw new RuntimeException("Failed") }
         when: 'registration is updated to delete cmhandles'
             def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
-        then: 'a response is received for all cm-handles'
-            response.getRemovedCmHandles().size() == 3
+        then: 'the cmHandle states are all updated to "DELETING"'
+            1 * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch({ assert it.every { entry -> entry.value == CmHandleState.DELETING } })
+        and: 'a response is received for all cm-handles'
+            response.removedCmHandles.size() == 3
         and: 'successfully de-registered cm handle entries are removed from in progress map'
             1 * mockModuleSyncStartedOnCmHandles.remove('cmhandle1')
             1 * mockModuleSyncStartedOnCmHandles.remove('cmhandle3')
         and: 'failed de-registered cm handle entries should not be removed from in progress map'
             0 * mockModuleSyncStartedOnCmHandles.remove('cmhandle2')
         and: '1st and 3rd cm-handle deletes successfully'
-            with(response.getRemovedCmHandles().get(0)) {
+            with(response.removedCmHandles[0]) {
                 assert it.status == Status.SUCCESS
                 assert it.cmHandle == 'cmhandle1'
             }
-            with(response.getRemovedCmHandles().get(2)) {
+            with(response.removedCmHandles[2]) {
                 assert it.status == Status.SUCCESS
                 assert it.cmHandle == 'cmhandle3'
             }
         and: '2nd cm-handle deletion fails'
-            with(response.getRemovedCmHandles().get(1)) {
+            with(response.removedCmHandles[1]) {
                 assert it.status == Status.FAILURE
                 assert it.registrationError == UNKNOWN_ERROR
                 assert it.errorText == 'Failed'
                 assert it.cmHandle == 'cmhandle2'
             }
+        and: 'the cmHandle state is updated to DELETED for 1st and 3rd'
+            1 * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch({
+                assert it.size() == 2
+                assert it.every { entry -> entry.value == CmHandleState.DELETED }
+            })
     }
 
     def 'Remove CmHandle Error Handling: Schema Set Deletion failed'() {
         given: 'a registration'
+            mockInventoryPersistence.getYangModelCmHandle('cmhandle') >> new YangModelCmHandle()
             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
                 removedCmHandles: ['cmhandle'])
         and: 'schema set deletion failed with unknown error'
@@ -319,10 +331,10 @@
         and: 'cm-handle is not deleted'
             0 * mockInventoryPersistence.deleteDataNodes(_)
         and: 'the cmHandle state is not updated to "DELETED"'
-            0 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, CmHandleState.DELETED)
+            0 * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch([yangModelCmHandle: CmHandleState.DELETED])
         and: 'a failure response is received'
-            assert response.getRemovedCmHandles().size() == 1
-            with(response.getRemovedCmHandles().get(0)) {
+            assert response.removedCmHandles.size() == 1
+            with(response.removedCmHandles[0]) {
                 assert it.status == Status.FAILURE
                 assert it.cmHandle == 'cmhandle'
                 assert it.errorText == 'Failed'
@@ -332,28 +344,30 @@
 
     def 'Remove CmHandle Error Handling: #scenario'() {
         given: 'a registration'
+            mockInventoryPersistence.getYangModelCmHandle('cmhandle') >> new YangModelCmHandle()
             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
                 removedCmHandles: ['cmhandle'])
-        and: 'cm-handle deletion throws exception'
+        and: 'cm-handle deletion fails on batch'
             mockInventoryPersistence.deleteDataNodes(_) >> { throw deleteListElementException }
+        and: 'cm-handle deletion fails on individual delete'
             mockInventoryPersistence.deleteDataNode(_) >> { throw deleteListElementException }
         when: 'registration is updated to delete cmhandle'
             def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
         then: 'a failure response is received'
-            assert response.getRemovedCmHandles().size() == 1
-            with(response.getRemovedCmHandles().get(0)) {
+            assert response.removedCmHandles.size() == 1
+            with(response.removedCmHandles[0]) {
                 assert it.status == Status.FAILURE
                 assert it.cmHandle == 'cmhandle'
                 assert it.registrationError == expectedError
                 assert it.errorText == expectedErrorText
             }
         and: 'the cm handle state is not updated to "DELETED"'
-            0 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, CmHandleState.DELETED)
+            0 * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch(_, CmHandleState.DELETED)
         where:
             scenario                     | cmHandleId             | deleteListElementException                ||  expectedError           | expectedErrorText
-            'cm-handle does not exist'   | 'cmhandle'             | new DataNodeNotFoundException("", "", "") || CM_HANDLE_DOES_NOT_EXIST | 'cm-handle does not exist'
-            'cm-handle has invalid name' | 'cm handle with space' | new DataValidationException("", "")       || CM_HANDLE_INVALID_ID     | 'cm-handle has an invalid character(s) in id'
-            'an unexpected exception'    | 'cmhandle'             | new RuntimeException("Failed")            || UNKNOWN_ERROR            | 'Failed'
+            'cm-handle does not exist'   | 'cmhandle'             | new DataNodeNotFoundException('', '', '') || CM_HANDLE_DOES_NOT_EXIST | 'cm-handle does not exist'
+            'cm-handle has invalid name' | 'cm handle with space' | new DataValidationException('', '')       || CM_HANDLE_INVALID_ID     | 'cm-handle has an invalid character(s) in id'
+            'an unexpected exception'    | 'cmhandle'             | new RuntimeException('Failed')            || UNKNOWN_ERROR            | 'Failed'
     }
 
     def getObjectUnderTest() {
@@ -362,4 +376,11 @@
                 stubbedNetworkCmProxyCmHandlerQueryService, mockLcmEventsCmHandleStateHandler, mockCpsDataService,
                 mockModuleSyncStartedOnCmHandles))
     }
+
+    def addPersistedYangModelCmHandles(ids) {
+        ids.each { 
+            def yangModelCmHandle = new YangModelCmHandle(id:it)
+            mockInventoryPersistence.getYangModelCmHandle(it) >> yangModelCmHandle
+        }
+    }
 }