Merge "DMI Data AVC to use kafka headers"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / inventory / InventoryPersistenceImplSpec.groovy
index 0d459fd..efe6f00 100644 (file)
@@ -1,7 +1,8 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2022 Nordix Foundation
+ *  Copyright (C) 2022-2023 Nordix Foundation
  *  Modifications Copyright (C) 2022 Bell Canada
+ *  Modifications Copyright (C) 2023 TechMahindra Ltd.
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
 package org.onap.cps.ncmp.api.inventory
 
 import com.fasterxml.jackson.databind.ObjectMapper
+import org.onap.cps.api.CpsAdminService
 import org.onap.cps.api.CpsDataService
 import org.onap.cps.api.CpsModuleService
 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
 import org.onap.cps.spi.CascadeDeleteAllowed
-import org.onap.cps.spi.CpsDataPersistenceService
-import org.onap.cps.spi.CpsAdminPersistenceService
 import org.onap.cps.spi.FetchDescendantsOption
 import org.onap.cps.spi.exceptions.DataValidationException
 import org.onap.cps.spi.model.DataNode
 import org.onap.cps.spi.model.ModuleDefinition
 import org.onap.cps.spi.model.ModuleReference
 import org.onap.cps.utils.JsonObjectMapper
+import org.onap.cps.spi.utils.CpsValidator
 import spock.lang.Shared
 import spock.lang.Specification
-
 import java.time.OffsetDateTime
 import java.time.ZoneOffset
 import java.time.format.DateTimeFormatter
@@ -52,12 +52,12 @@ class InventoryPersistenceImplSpec extends Specification {
 
     def mockCpsModuleService = Mock(CpsModuleService)
 
-    def mockCpsDataPersistenceService = Mock(CpsDataPersistenceService)
+    def mockCpsAdminService = Mock(CpsAdminService)
 
-    def mockCpsAdminPersistenceService = Mock(CpsAdminPersistenceService)
+    def mockCpsValidator = Mock(CpsValidator)
 
     def objectUnderTest = new InventoryPersistenceImpl(spiedJsonObjectMapper, mockCpsDataService, mockCpsModuleService,
-            mockCpsDataPersistenceService, mockCpsAdminPersistenceService)
+            mockCpsAdminService, mockCpsValidator)
 
     def formattedDateAndTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
             .format(OffsetDateTime.of(2022, 12, 31, 20, 30, 40, 1, ZoneOffset.UTC))
@@ -66,6 +66,9 @@ class InventoryPersistenceImplSpec extends Specification {
     def leaves = ["dmi-service-name":"common service name","dmi-data-service-name":"data service name","dmi-model-service-name":"model service name"]
     def xpath = "/dmi-registry/cm-handles[@id='some-cm-handle']"
 
+    def cmHandleId2 = 'another-cm-handle'
+    def xpath2 = "/dmi-registry/cm-handles[@id='another-cm-handle']"
+
     @Shared
     def childDataNodesForCmHandleWithAllProperties = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some cm handle']/additional-properties[@name='name1']", leaves: ["name":"name1", "value":"value1"]),
                                                       new DataNode(xpath: "/dmi-registry/cm-handles[@id='some cm handle']/public-properties[@name='name2']", leaves: ["name":"name2","value":"value2"])]
@@ -82,7 +85,7 @@ class InventoryPersistenceImplSpec extends Specification {
     def "Retrieve CmHandle using datanode with #scenario."() {
         given: 'the cps data service returns a data node from the DMI registry'
             def dataNode = new DataNode(childDataNodes:childDataNodes, leaves: leaves)
-            mockCpsDataPersistenceService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', xpath, INCLUDE_ALL_DESCENDANTS) >> dataNode
+            mockCpsDataService.getDataNodes('NCMP-Admin', 'ncmp-dmi-registry', xpath, INCLUDE_ALL_DESCENDANTS) >> [dataNode]
         when: 'retrieving the yang modelled cm handle'
             def result = objectUnderTest.getYangModelCmHandle(cmHandleId)
         then: 'the result has the correct id and service names'
@@ -95,34 +98,40 @@ class InventoryPersistenceImplSpec extends Specification {
             result.publicProperties == expectedPublicProperties
         and: 'the state details are returned'
             result.compositeState.cmHandleState == expectedCompositeState
+        and: 'the CM Handle ID is validated'
+            1 * mockCpsValidator.validateNameCharacters(cmHandleId)
         where: 'the following parameters are used'
             scenario                    | childDataNodes                                || expectedDmiProperties                               || expectedPublicProperties                              || expectedCompositeState
             'no properties'             | []                                            || []                                                  || []                                                    || null
-            'DMI and public properties' | childDataNodesForCmHandleWithAllProperties    || [new YangModelCmHandle.Property("name1", "value1")] || [new YangModelCmHandle.Property("name2", "value2")] || null
+            'DMI and public properties' | childDataNodesForCmHandleWithAllProperties    || [new YangModelCmHandle.Property("name1", "value1")] || [new YangModelCmHandle.Property("name2", "value2")]   || null
             'just DMI properties'       | childDataNodesForCmHandleWithDMIProperties    || [new YangModelCmHandle.Property("name1", "value1")] || []                                                    || null
             'just public properties'    | childDataNodesForCmHandleWithPublicProperties || []                                                  || [new YangModelCmHandle.Property("name2", "value2")]   || null
             'with state details'        | childDataNodesForCmHandleWithState            || []                                                  || []                                                    || CmHandleState.ADVISED
     }
 
-    def "Retrieve CmHandle using datanode with invalid CmHandle id."() {
-        when: 'retrieving the yang modelled cm handle with an invalid id'
-            def result = objectUnderTest.getYangModelCmHandle('cm handle id with spaces')
-        then: 'a data validation exception is thrown'
-            thrown(DataValidationException)
-        and: 'the result is not returned'
-            result == null
-    }
-
-    def "Handling missing service names as null CPS-1043."() {
+    def "Handling missing service names as null."() {
         given: 'the cps data service returns a data node from the DMI registry with empty child and leaf attributes'
             def dataNode = new DataNode(childDataNodes:[], leaves: [:])
-            mockCpsDataPersistenceService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', xpath, INCLUDE_ALL_DESCENDANTS) >> dataNode
+            mockCpsDataService.getDataNodes('NCMP-Admin', 'ncmp-dmi-registry', xpath, INCLUDE_ALL_DESCENDANTS) >> [dataNode]
         when: 'retrieving the yang modelled cm handle'
             def result = objectUnderTest.getYangModelCmHandle(cmHandleId)
-        then: 'the service names ae returned as null'
+        then: 'the service names are returned as null'
             result.dmiServiceName == null
             result.dmiDataServiceName == null
             result.dmiModelServiceName == null
+        and: 'the CM Handle ID is validated'
+            1 * mockCpsValidator.validateNameCharacters(cmHandleId)
+    }
+
+    def "Retrieve multiple YangModelCmHandles"() {
+        given: 'the cps data service returns 2 data nodes from the DMI registry'
+            def dataNodes = [new DataNode(xpath: xpath), new DataNode(xpath: xpath2)]
+            mockCpsDataService.getDataNodesForMultipleXpaths('NCMP-Admin', 'ncmp-dmi-registry', [xpath, xpath2] , INCLUDE_ALL_DESCENDANTS) >> dataNodes
+        when: 'retrieving the yang modelled cm handle'
+            def results = objectUnderTest.getYangModelCmHandles([cmHandleId, cmHandleId2])
+        then: 'verify both have returned and cmhandleIds are correct'
+            assert results.size() == 2
+            assert results.id.containsAll([cmHandleId, cmHandleId2])
     }
 
     def 'Get a Cm Handle Composite State'() {
@@ -130,12 +139,14 @@ class InventoryPersistenceImplSpec extends Specification {
             def cmHandleId = 'Some-Cm-Handle'
             def dataNode = new DataNode(leaves: ['cm-handle-state': 'ADVISED'])
         and: 'cps data service returns a valid data node'
-            mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
-                    '/dmi-registry/cm-handles[@id=\'Some-Cm-Handle\']/state', FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
+            mockCpsDataService.getDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
+                    '/dmi-registry/cm-handles[@id=\'Some-Cm-Handle\']/state', FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> [dataNode]
         when: 'get cm handle state is invoked'
             def result = objectUnderTest.getCmHandleState(cmHandleId)
         then: 'result has returned the correct cm handle state'
             result.cmHandleState == CmHandleState.ADVISED
+        and: 'the CM Handle ID is validated'
+            1 * mockCpsValidator.validateNameCharacters(cmHandleId)
     }
 
     def 'Update Cm Handle with #scenario State'() {
@@ -187,6 +198,8 @@ class InventoryPersistenceImplSpec extends Specification {
             def result = objectUnderTest.getYangResourcesModuleReferences('some-cmHandle-Id')
         then: 'the returned result is a collection of module definitions'
             assert result == moduleReferences
+        and: 'the CM Handle ID is validated'
+            1 * mockCpsValidator.validateNameCharacters('some-cmHandle-Id')
     }
 
     def 'Save Cmhandle'() {
@@ -230,22 +243,24 @@ class InventoryPersistenceImplSpec extends Specification {
             objectUnderTest.deleteSchemaSetWithCascade('validSchemaSetName')
         then: 'the module service to delete schemaSet is invoked once'
             1 * mockCpsModuleService.deleteSchemaSet('NFP-Operational', 'validSchemaSetName', CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED)
+        and: 'the schema set name is validated'
+            1 * mockCpsValidator.validateNameCharacters('validSchemaSetName')
     }
 
-    def 'Delete schema set with an invalid schema set name'() {
-        when: 'the method to delete schema set is called with an invalid schema set name'
-            objectUnderTest.deleteSchemaSetWithCascade('invalid SchemaSet name')
-        then: 'a data validation exception is thrown'
-            thrown(DataValidationException)
-        and: 'the module service to delete schemaSet is not called'
-            0 * mockCpsModuleService.deleteSchemaSet('NFP-Operational', 'sampleSchemaSetName', CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED)
+    def 'Delete multiple schema sets with valid schema set names'() {
+        when: 'the method to delete schema sets is called with valid schema set names'
+            objectUnderTest.deleteSchemaSetsWithCascade(['validSchemaSetName1', 'validSchemaSetName2'])
+        then: 'the module service to delete schema sets is invoked once'
+            1 * mockCpsModuleService.deleteSchemaSetsWithCascade('NFP-Operational', ['validSchemaSetName1', 'validSchemaSetName2'])
+        and: 'the schema set names are validated'
+            1 * mockCpsValidator.validateNameCharacters(['validSchemaSetName1', 'validSchemaSetName2'])
     }
 
     def 'Get data node via xPath'() {
         when: 'the method to get data nodes is called'
             objectUnderTest.getDataNode('sample xPath')
         then: 'the data persistence service method to get data node is invoked once'
-            1 * mockCpsDataPersistenceService.getDataNode('NCMP-Admin','ncmp-dmi-registry','sample xPath', INCLUDE_ALL_DESCENDANTS)
+            1 * mockCpsDataService.getDataNodes('NCMP-Admin','ncmp-dmi-registry','sample xPath', INCLUDE_ALL_DESCENDANTS)
     }
 
     def 'Get cmHandle data node'() {
@@ -254,21 +269,14 @@ class InventoryPersistenceImplSpec extends Specification {
         when: 'the method to get data nodes is called'
             objectUnderTest.getCmHandleDataNode('sample cmHandleId')
         then: 'the data persistence service method to get cmHandle data node is invoked once with expected xPath'
-            1 * mockCpsDataPersistenceService.getDataNode('NCMP-Admin','ncmp-dmi-registry',expectedXPath, INCLUDE_ALL_DESCENDANTS)
+            1 * mockCpsDataService.getDataNodes('NCMP-Admin','ncmp-dmi-registry',expectedXPath, INCLUDE_ALL_DESCENDANTS)
     }
 
-    def 'Query anchors'() {
-        when: 'the method to query anchors is called'
-            objectUnderTest.queryAnchors(['sample-module-name'])
+    def 'Get CM handles that has given module names'() {
+        when: 'the method to get cm handles is called'
+            objectUnderTest.getCmHandleIdsWithGivenModules(['sample-module-name'])
         then: 'the admin persistence service method to query anchors is invoked once with the same parameter'
-            1 * mockCpsAdminPersistenceService.queryAnchors('NFP-Operational',['sample-module-name'])
-    }
-
-    def 'Get anchors'() {
-        when: 'the method to get anchors with no parameters is called'
-            objectUnderTest.getAnchors()
-        then: 'the admin persistence service method to query anchors is invoked once with a specific dataspace name'
-            1 * mockCpsAdminPersistenceService.getAnchors('NFP-Operational')
+            1 * mockCpsAdminService.queryAnchorNames('NFP-Operational',['sample-module-name'])
     }
 
     def 'Replace list content'() {
@@ -287,4 +295,12 @@ class InventoryPersistenceImplSpec extends Specification {
                     'sample dataNode xpath', NO_TIMESTAMP);
     }
 
+    def 'Delete multiple data nodes via xPath'() {
+        when: 'Delete data nodes method is called with multiple xpaths as parameters'
+            objectUnderTest.deleteDataNodes(['xpath1', 'xpath2'])
+        then: 'the cps data service method to delete data nodes is invoked once with the same xPaths'
+            1 * mockCpsDataService.deleteDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
+                    ['xpath1', 'xpath2'], NO_TIMESTAMP);
+    }
+
 }