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
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
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))
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'
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'
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
+ '//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'
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
+ '//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'
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
+ '//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)
+ def result = objectUnderTest.getCmHandlesByOperationalSyncState(DataStoreSyncState.UNSYNCHRONIZED)
then: 'the returned result is a list of data nodes returned by cps data service'
assert result == sampleDataNodes
}
def cpsPath = '//cps-path'
and: 'cps data service returns a valid data node'
mockCpsDataPersistenceService.queryDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
- cpsPath, OMIT_DESCENDANTS)
+ cpsPath, INCLUDE_ALL_DESCENDANTS)
>> Arrays.asList(cmHandleDataNode)
when: 'get cm handles by cps path is invoked'
- def result = objectUnderTest.getCmHandleDataNodesByCpsPath(cpsPath)
+ 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 list elements'() {
+ when: 'the method to save list elements is called'
+ objectUnderTest.saveListElements('sample Json data')
+ then: 'the data service method to save list elements is called once'
+ 1 * mockCpsDataService.saveListElements('NCMP-Admin','ncmp-dmi-registry','/dmi-registry','sample Json data',null)
+ }
+
+ 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 'Query data nodes via cpsPath'() {
+ when: 'the method to query data nodes is called'
+ objectUnderTest.queryDataNodes('sample cpsPath')
+ then: 'the data persistence service method to query data nodes is invoked once'
+ 1 * mockCpsDataPersistenceService.queryDataNodes('NCMP-Admin','ncmp-dmi-registry','sample cpsPath', INCLUDE_ALL_DESCENDANTS)
+ }
+
+ 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);
+ }
+
}