Query CmHandles using CPS path
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / inventory / InventoryPersistenceSpec.groovy
index 46bd047..f9ca676 100644 (file)
@@ -23,11 +23,16 @@ package org.onap.cps.ncmp.api.inventory
 
 import com.fasterxml.jackson.databind.ObjectMapper
 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 spock.lang.Shared
 import spock.lang.Specification
@@ -36,6 +41,7 @@ import java.time.OffsetDateTime
 import java.time.ZoneOffset
 import java.time.format.DateTimeFormatter
 
+import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NO_TIMESTAMP
 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
 
@@ -45,10 +51,14 @@ class InventoryPersistenceSpec extends Specification {
 
     def mockCpsDataService = Mock(CpsDataService)
 
+    def mockCpsModuleService = Mock(CpsModuleService)
+
     def mockCpsDataPersistenceService = Mock(CpsDataPersistenceService)
 
+    def mockCpsAdminPersistenceService = Mock(CpsAdminPersistenceService)
 
-    def objectUnderTest = new InventoryPersistence(spiedJsonObjectMapper, mockCpsDataService, mockCpsDataPersistenceService)
+    def objectUnderTest = new InventoryPersistence(spiedJsonObjectMapper, mockCpsDataService, mockCpsModuleService,
+            mockCpsDataPersistenceService, mockCpsAdminPersistenceService)
 
     def formattedDateAndTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
             .format(OffsetDateTime.of(2022, 12, 31, 20, 30, 40, 1, ZoneOffset.UTC))
@@ -70,13 +80,10 @@ class InventoryPersistenceSpec extends Specification {
     @Shared
     def childDataNodesForCmHandleWithState = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some-cm-handle']/state", leaves: ['cm-handle-state': 'ADVISED'])]
 
-    @Shared
-    def static sampleDataNodes = [new DataNode()]
-
     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)
-            mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', xpath, INCLUDE_ALL_DESCENDANTS) >> dataNode
+            mockCpsDataPersistenceService.getDataNode('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'
@@ -110,7 +117,7 @@ class InventoryPersistenceSpec extends Specification {
     def "Handling missing service names as null CPS-1043."() {
         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: [:])
-            mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', xpath, INCLUDE_ALL_DESCENDANTS) >> dataNode
+            mockCpsDataPersistenceService.getDataNode('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'
@@ -147,54 +154,106 @@ class InventoryPersistenceSpec extends Specification {
             'DELETING'  | CmHandleState.DELETING || '{"state":{"cm-handle-state":"DELETING","last-update-time":"2022-12-31T20:30:40.000+0000"}}'
     }
 
-    def 'Get Cm Handles By State'() {
-        given: 'a cm handle state to query'
-            def cmHandleState = CmHandleState.ADVISED
-        and: 'cps data service returns a list of data nodes'
-            mockCpsDataPersistenceService.queryDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
-                '//state[@cm-handle-state="ADVISED"]/ancestor::cm-handles', OMIT_DESCENDANTS) >> sampleDataNodes
-        when: 'get cm handles by state is invoked'
-            def result = objectUnderTest.getCmHandlesByState(cmHandleState)
-        then: 'the returned result is a list of data nodes returned by cps data service'
-            assert result == sampleDataNodes
-    }
-
-    def 'Get Cm Handles By State and Cm-Handle Id'() {
-        given: 'a cm handle state to query'
-            def cmHandleState = CmHandleState.READY
-        and: 'cps data service returns a list of data nodes'
-            mockCpsDataPersistenceService.queryDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
-                '//cm-handles[@id=\'some-cm-handle\']/state[@cm-handle-state="'+ 'READY'+'"]/ancestor::cm-handles', OMIT_DESCENDANTS) >> sampleDataNodes
-        when: 'get cm handles by state and id is invoked'
-            def result = objectUnderTest.getCmHandlesByIdAndState(cmHandleId, cmHandleState)
-        then: 'the returned result is a list of data nodes returned by cps data service'
-            assert result == sampleDataNodes
-    }
-
-    def 'Get Cm Handles By Operational Sync State : UNSYNCHRONIZED'() {
-        given: 'a cm handle state to query'
-            def cmHandleState = CmHandleState.READY
-        and: 'cps data service returns a list of data nodes'
-            mockCpsDataPersistenceService.queryDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
-                '//state/datastores/operational[@sync-state="'+'UNSYNCHRONIZED'+'"]/ancestor::cm-handles', OMIT_DESCENDANTS) >> sampleDataNodes
-        when: 'get cm handles by operational sync state as UNSYNCHRONIZED is invoked'
-            def result = objectUnderTest.getCmHandlesByOperationalSyncState(SyncState.UNSYNCHRONIZED)
-        then: 'the returned result is a list of data nodes returned by cps data service'
-            assert result == sampleDataNodes
-    }
-
-    def 'Retrieve cm handle by cps path '() {
-        given: 'a cm handle state to query based on the cps path'
-            def cmHandleDataNode = new DataNode(xpath: 'xpath', leaves: ['cm-handle-state': 'LOCKED'])
-            def cpsPath = '//cps-path'
-        and: 'cps data service returns a valid data node'
-            mockCpsDataPersistenceService.queryDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
-                    cpsPath, INCLUDE_ALL_DESCENDANTS)
-                    >> Arrays.asList(cmHandleDataNode)
-        when: 'get cm handles by cps path is invoked'
-            def result = objectUnderTest.getCmHandleDataNodesByCpsPath(cpsPath, INCLUDE_ALL_DESCENDANTS)
-        then: 'the returned result is a list of data nodes returned by cps data service'
-            assert result.contains(cmHandleDataNode)
+    def 'Get module definitions'() {
+        given: 'cps module service returns a collection of module definitions'
+            def moduleDefinitions = [new ModuleDefinition('moduleName','revision','content')]
+            mockCpsModuleService.getModuleDefinitionsByAnchorName('NFP-Operational','some-cmHandle-Id') >> moduleDefinitions
+        when: 'get module definitions by cmHandle is invoked'
+            def result = objectUnderTest.getModuleDefinitionsByCmHandleId('some-cmHandle-Id')
+        then: 'the returned result are the same module definitions as returned from the module service'
+            assert result == moduleDefinitions
+    }
+
+    def 'Get module references'() {
+        given: 'cps module service returns a collection of module references'
+            def moduleReferences = [new ModuleReference('moduleName','revision','namespace')]
+            mockCpsModuleService.getYangResourcesModuleReferences('NFP-Operational','some-cmHandle-Id') >> moduleReferences
+        when: 'get yang resources module references by cmHandle is invoked'
+            def result = objectUnderTest.getYangResourcesModuleReferences('some-cmHandle-Id')
+        then: 'the returned result is a collection of module definitions'
+            assert result == moduleReferences
+    }
+
+    def 'Save Cmhandle'() {
+        given: 'cmHandle represented as Yang Model'
+            def yangModelCmHandle = new YangModelCmHandle(id: 'cmhandle', dmiProperties: [], publicProperties: [])
+        when: 'the method to save cmhandle is called'
+            objectUnderTest.saveCmHandle(yangModelCmHandle)
+        then: 'the data service method to save list elements is called once'
+            1 * mockCpsDataService.saveListElements('NCMP-Admin','ncmp-dmi-registry','/dmi-registry',_,null) >> {
+                args -> {
+                    assert args[3].startsWith('{"cm-handles":[{"id":"cmhandle","additional-properties":[],"public-properties":[]}]}')
+                }
+            }
+    }
+
+    def 'Delete list or list elements'() {
+        when: 'the method to delete list or list elements is called'
+            objectUnderTest.deleteListOrListElement('sample xPath')
+        then: 'the data service method to save list elements is called once'
+            1 * mockCpsDataService.deleteListOrListElement('NCMP-Admin','ncmp-dmi-registry','sample xPath',null)
+    }
+
+    def 'Delete schema set with a valid schema set name'() {
+        when: 'the method to delete schema set is called with valid schema set name'
+            objectUnderTest.deleteSchemaSetWithCascade('validSchemaSetName')
+        then: 'the module service to delete schemaSet is invoked once'
+            1 * mockCpsModuleService.deleteSchemaSet('NFP-Operational', 'validSchemaSetName', CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED)
+    }
+
+    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 '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)
+    }
+
+    def 'Get cmHandle data node'() {
+        given: 'expected xPath to get cmHandle data node'
+            def expectedXPath = '/dmi-registry/cm-handles[@id=\'sample cmHandleId\']';
+        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)
+    }
+
+    def 'Query anchors'() {
+        when: 'the method to query anchors is called'
+            objectUnderTest.queryAnchors(['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')
+    }
+
+    def 'Replace list content'() {
+        when: 'replace list content method is called with xpath and data nodes collection'
+            objectUnderTest.replaceListContent('sample xpath', [new DataNode()])
+        then: 'the cps data service method to replace list content is invoked once with same parameters'
+            1 * mockCpsDataService.replaceListContent('NCMP-Admin', 'ncmp-dmi-registry',
+                    'sample xpath', [new DataNode()], NO_TIMESTAMP);
+    }
+
+    def 'Delete data node via xPath'() {
+        when: 'Delete data node method is called with xpath as parameter'
+            objectUnderTest.deleteDataNode('sample dataNode xpath')
+        then: 'the cps data service method to delete data node is invoked once with the same xPath'
+            1 * mockCpsDataService.deleteDataNode('NCMP-Admin', 'ncmp-dmi-registry',
+                    'sample dataNode xpath', NO_TIMESTAMP);
     }
 
 }