/*
* ============LICENSE_START=======================================================
* Copyright (C) 2022 Nordix Foundation
+ * 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.impl
-import org.onap.cps.ncmp.api.NetworkCmProxyCmHandlerQueryService
+import org.onap.cps.cpspath.parser.PathParsingException
+import org.onap.cps.ncmp.api.inventory.CmHandleQueries
+import org.onap.cps.ncmp.api.inventory.CmHandleQueriesImpl
import org.onap.cps.ncmp.api.inventory.InventoryPersistence
-import org.onap.cps.spi.model.Anchor
-import org.onap.cps.spi.model.CmHandleQueryServiceParameters
+import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters
+import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
+import org.onap.cps.spi.FetchDescendantsOption
+import org.onap.cps.spi.exceptions.DataInUseException
+import org.onap.cps.spi.exceptions.DataValidationException
import org.onap.cps.spi.model.ConditionProperties
import org.onap.cps.spi.model.DataNode
import spock.lang.Specification
-
import java.util.stream.Collectors
class NetworkCmProxyCmHandlerQueryServiceSpec extends Specification {
- def inventoryPersistence = Mock(InventoryPersistence)
+ def cmHandleQueries = Mock(CmHandleQueries)
+ def partiallyMockedCmHandleQueries = Spy(CmHandleQueriesImpl)
+ def mockInventoryPersistence = Mock(InventoryPersistence)
+
+ def static someCmHandleDataNode = [new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'some-cmhandle-id\']', leaves: ['id':'some-cmhandle-id'])]
+ def dmiRegistry = new DataNode(xpath: '/dmi-registry', childDataNodes: createDataNodeList(['PNFDemo1', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4']))
+
+ static def queryResultCmHandleMap = createCmHandleMap(['H1', 'H2'])
- NetworkCmProxyCmHandlerQueryService objectUnderTest = new NetworkCmProxyCmHandlerQueryServiceImpl(inventoryPersistence)
+ def objectUnderTest = new NetworkCmProxyCmHandlerQueryServiceImpl(cmHandleQueries, mockInventoryPersistence)
+ def objectUnderTestSpy = new NetworkCmProxyCmHandlerQueryServiceImpl(partiallyMockedCmHandleQueries, mockInventoryPersistence)
- def 'Retrieve cm handles with public properties when #scenario.'() {
- given: 'a condition property'
+ def 'Retrieve cm handles with cpsPath when combined with no Module Query.'() {
+ given: 'a cmHandleWithCpsPath condition property'
def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
- def conditionProperties = new ConditionProperties()
- conditionProperties.conditionName = 'hasAllProperties'
- conditionProperties.conditionParameters = publicProperties
+ def conditionProperties = createConditionProperties('cmHandleWithCpsPath', [['cpsPath' : '/some/cps/path']])
cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
- and: 'mock services'
- mockResponses()
- when: 'a query is execute (with and without Data)'
+ and: 'cmHandleQueries returns a non null query result'
+ cmHandleQueries.queryCmHandleDataNodesByCpsPath('/some/cps/path', FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> [new DataNode(leaves: ['id':'some-cmhandle-id'])]
+ and: 'CmHandleQueries returns cmHandles with the relevant query result'
+ cmHandleQueries.combineCmHandleQueries(*_) >> ['PNFDemo1': new NcmpServiceCmHandle(cmHandleId: 'PNFDemo1'), 'PNFDemo3': new NcmpServiceCmHandle(cmHandleId: 'PNFDemo3')]
+ when: 'the query is executed for both cm handle ids and details'
def returnedCmHandlesJustIds = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
def returnedCmHandlesWithData = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
then: 'the correct expected cm handles ids are returned'
- returnedCmHandlesJustIds == expectedCmHandleIds as Set
- and: 'the correct cm handle data objects are returned'
- returnedCmHandlesWithData.stream().map(dataNode -> dataNode.cmHandleId).collect(Collectors.toSet()) == expectedCmHandleIds as Set
+ returnedCmHandlesJustIds == ['PNFDemo1', 'PNFDemo3'] as Set
+ and: 'the correct ncmp service cm handles are returned'
+ returnedCmHandlesWithData.stream().map(CmHandle -> CmHandle.cmHandleId).collect(Collectors.toSet()) == ['PNFDemo1', 'PNFDemo3'] as Set
+ }
+
+ def 'Retrieve cm handles with cpsPath where #scenario.'() {
+ given: 'a cmHandleWithCpsPath condition property'
+ def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
+ def conditionProperties = createConditionProperties('cmHandleWithCpsPath', [['cpsPath' : '/some/cps/path']])
+ cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
+ and: 'cmHandleQueries throws a path parsing exception'
+ cmHandleQueries.queryCmHandleDataNodesByCpsPath('/some/cps/path', FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> { throw thrownException }
+ when: 'the query is executed for both cm handle ids and details'
+ objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
+ objectUnderTest.queryCmHandles(cmHandleQueryParameters)
+ then: 'a data validation exception is thrown'
+ thrown(expectedException)
where: 'the following data is used'
- scenario | publicProperties || expectedCmHandleIds
- 'single property matches' | [['Contact' : 'newemailforstore@bookstore.com']] || ['PNFDemo1', 'PNFDemo2', 'PNFDemo4']
- 'public property does not match' | [['wont_match' : 'wont_match']] || []
- '2 properties, only one match' | [['Contact' : 'newemailforstore@bookstore.com'], ['Contact2': 'newemailforstore2@bookstore.com']] || ['PNFDemo4']
- '2 properties, no matches' | [['Contact' : 'newemailforstore@bookstore.com'], ['Contact2': '']] || []
+ scenario | thrownException || expectedException
+ 'a PathParsingException is thrown' | new PathParsingException('some message', 'some details') || DataValidationException
+ 'any other Exception is thrown' | new DataInUseException('some message', 'some details') || DataInUseException
}
- def 'Retrieve cm handles with module names when #scenario.'() {
- given: 'a condition property'
+ def 'Query cm handles with public properties when combined with empty modules query result.'() {
+ given: 'a public properties condition property'
def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
- def conditionProperties = new ConditionProperties()
- conditionProperties.conditionName = 'hasAllModules'
- conditionProperties.conditionParameters = moduleNames
+ def conditionProperties = createConditionProperties('hasAllProperties', [['some-property-key': 'some-property-value']])
cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
- and: 'mock services'
- mockResponses()
- when: 'the service is invoked'
+ and: 'CmHandleQueries returns cmHandles with the relevant query result'
+ cmHandleQueries.combineCmHandleQueries(*_) >> ['PNFDemo1': new NcmpServiceCmHandle(cmHandleId: 'PNFDemo1'), 'PNFDemo3': new NcmpServiceCmHandle(cmHandleId: 'PNFDemo3')]
+ when: 'the query is executed for both cm handle ids and details'
def returnedCmHandlesJustIds = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
def returnedCmHandlesWithData = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
- then: 'the correct expected cm handles are returned'
- returnedCmHandlesJustIds == expectedCmHandleIds as Set
- returnedCmHandlesWithData.stream().map(dataNode -> dataNode.cmHandleId).collect(Collectors.toSet()) == expectedCmHandleIds as Set
+ then: 'the correct expected cm handles ids are returned'
+ returnedCmHandlesJustIds == ['PNFDemo1', 'PNFDemo3'] as Set
+ and: 'the correct cm handle data objects are returned'
+ returnedCmHandlesWithData.stream().map(dataNode -> dataNode.cmHandleId).collect(Collectors.toSet()) == ['PNFDemo1', 'PNFDemo3'] as Set
+ }
+
+ def 'Retrieve cm handles with module names when #scenario from query.'() {
+ given: 'a modules condition property'
+ def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
+ def conditionProperties = createConditionProperties('hasAllModules', [['moduleName': 'some-module-name']])
+ cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
+ and: 'null is returned from the state and public property queries'
+ cmHandleQueries.combineCmHandleQueries(*_) >> null
+ and: '#scenario from the modules query'
+ mockInventoryPersistence.getCmHandleIdsWithGivenModules(*_) >> cmHandleIdsFromService
+ and: 'the same cmHandles are returned from the persistence service layer'
+ cmHandleIdsFromService.size() * mockInventoryPersistence.getDataNode(*_) >> returnedCmHandles
+ when: 'the query is executed for both cm handle ids and details'
+ def returnedCmHandlesJustIds = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
+ def returnedCmHandlesWithData = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
+ then: 'the correct expected cm handles ids are returned'
+ returnedCmHandlesJustIds == cmHandleIdsFromService as Set
+ and: 'the correct cm handle data objects are returned'
+ returnedCmHandlesWithData.stream().map(dataNode -> dataNode.cmHandleId).collect(Collectors.toSet()) == cmHandleIdsFromService as Set
where: 'the following data is used'
- scenario | moduleNames || expectedCmHandleIds
- 'single matching module name' | [['moduleName' : 'MODULE-NAME-001']] || ['PNFDemo3', 'PNFDemo1', 'PNFDemo2']
- 'module name dont match' | [['moduleName' : 'MODULE-NAME-004']] || []
- '2 module names, only one match' | [['moduleName' : 'MODULE-NAME-002'], ['moduleName': 'MODULE-NAME-003']] || ['PNFDemo4']
- '2 module names, no matches' | [['moduleName' : 'MODULE-NAME-002'], ['moduleName': 'MODULE-NAME-004']] || []
+ scenario | cmHandleIdsFromService | returnedCmHandles
+ 'One anchor returned' | ['some-cmhandle-id'] | someCmHandleDataNode
+ 'No anchors are returned' | [] | null
}
def 'Retrieve cm handles with combined queries when #scenario.'() {
- given: 'condition properties'
+ given: 'all condition properties used'
def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
- def conditionProperties1 = new ConditionProperties()
- conditionProperties1.conditionName = 'hasAllProperties'
- conditionProperties1.conditionParameters = publicProperties
- def conditionProperties2 = new ConditionProperties()
- conditionProperties2.conditionName = 'hasAllModules'
- conditionProperties2.conditionParameters = moduleNames
- cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties1,conditionProperties2])
- and: 'mock services'
- mockResponses()
- when: 'the service is invoked'
+ def conditionPubProps = createConditionProperties('hasAllProperties', [['some-property-key': 'some-property-value']])
+ def conditionModules = createConditionProperties('hasAllModules', [['moduleName': 'some-module-name']])
+ def conditionState = createConditionProperties('cmHandleWithCpsPath', [['cpsPath' : '/some/cps/path']])
+ cmHandleQueryParameters.setCmHandleQueryParameters([conditionPubProps, conditionModules, conditionState])
+ and: 'cmHandles are returned from the state and public property combined queries'
+ cmHandleQueries.combineCmHandleQueries(*_) >> combinedQueryMap
+ and: 'cmHandles are returned from the module names query'
+ mockInventoryPersistence.getCmHandleIdsWithGivenModules(['some-module-name']) >> anchorsForModuleQuery
+ and: 'cmHandleQueries returns a datanode result'
+ 2 * cmHandleQueries.queryCmHandleDataNodesByCpsPath(*_) >> someCmHandleDataNode
+ when: 'the query is executed for both cm handle ids and details'
def returnedCmHandlesJustIds = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
def returnedCmHandlesWithData = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
- then: 'the correct expected cm handles are returned'
+ then: 'the correct expected cm handles ids are returned'
returnedCmHandlesJustIds == expectedCmHandleIds as Set
- returnedCmHandlesWithData.stream().map(d -> d.cmHandleId).collect(Collectors.toSet()) == expectedCmHandleIds as Set
+ and: 'the correct cm handle data objects are returned'
+ returnedCmHandlesWithData.stream().map(dataNode -> dataNode.cmHandleId).collect(Collectors.toSet()) == expectedCmHandleIds as Set
where: 'the following data is used'
- scenario | moduleNames | publicProperties || expectedCmHandleIds
- 'particularly intersect' | [['moduleName' : 'MODULE-NAME-001']] | [['Contact' : 'newemailforstore@bookstore.com']] || ['PNFDemo1', 'PNFDemo2']
- 'empty intersect' | [['moduleName' : 'MODULE-NAME-004']] | [['Contact' : 'newemailforstore@bookstore.com']] || []
- 'total intersect' | [['moduleName' : 'MODULE-NAME-002']] | [['Contact2' : 'newemailforstore2@bookstore.com']] || ['PNFDemo4']
+ scenario | combinedQueryMap | anchorsForModuleQuery || expectedCmHandleIds
+ 'combined and modules queries intersect' | ['PNFDemo1': new NcmpServiceCmHandle(cmHandleId: 'PNFDemo1')] | ['PNFDemo1', 'PNFDemo2'] || ['PNFDemo1']
+ 'only module query results exist' | [:] | ['PNFDemo1', 'PNFDemo2'] || []
+ 'only combined query results exist' | ['PNFDemo1': new NcmpServiceCmHandle(cmHandleId: 'PNFDemo1'), 'PNFDemo2': new NcmpServiceCmHandle(cmHandleId: 'PNFDemo2')] | [] || []
+ 'neither queries return results' | [:] | [] || []
+ 'none intersect' | ['PNFDemo1': new NcmpServiceCmHandle(cmHandleId: 'PNFDemo1')] | ['PNFDemo2'] || []
}
def 'Retrieve cm handles when the query is empty.'() {
- given: 'mock services'
- mockResponses()
- when: 'the service is invoked'
+ given: 'We use an empty query'
def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
+ and: 'the inventory persistence returns the dmi registry datanode with just ids'
+ mockInventoryPersistence.getDataNode("/dmi-registry", FetchDescendantsOption.FETCH_DIRECT_CHILDREN_ONLY) >> [dmiRegistry]
+ and: 'the inventory persistence returns the dmi registry datanode with data'
+ mockInventoryPersistence.getDataNode("/dmi-registry") >> [dmiRegistry]
+ when: 'the query is executed for both cm handle ids and details'
def returnedCmHandlesJustIds = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
def returnedCmHandlesWithData = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
then: 'the correct expected cm handles are returned'
returnedCmHandlesWithData.stream().map(d -> d.cmHandleId).collect(Collectors.toSet()) == ['PNFDemo1', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4'] as Set
}
- void mockResponses() {
- def pNFDemo1 = new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'PNFDemo1\']', leaves: ['id':'PNFDemo1'])
- def pNFDemo2 = new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'PNFDemo2\']', leaves: ['id':'PNFDemo2'])
- def pNFDemo3 = new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'PNFDemo3\']', leaves: ['id':'PNFDemo3'])
- def pNFDemo4 = new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'PNFDemo4\']', leaves: ['id':'PNFDemo4'])
- def dmiRegistry = new DataNode(xpath: '/dmi-registry', childDataNodes: [pNFDemo1, pNFDemo2, pNFDemo3, pNFDemo4])
-
- inventoryPersistence.queryDataNodes('//public-properties[@name=\'Contact\' and @value=\'newemailforstore@bookstore.com\']/ancestor::cm-handles')
- >> [pNFDemo1, pNFDemo2, pNFDemo4]
- inventoryPersistence.queryDataNodes('//public-properties[@name=\'wont_match\' and @value=\'wont_match\']/ancestor::cm-handles')
- >> []
- inventoryPersistence.queryDataNodes('//public-properties[@name=\'Contact2\' and @value=\'newemailforstore2@bookstore.com\']/ancestor::cm-handles')
- >> [pNFDemo4]
- inventoryPersistence.queryDataNodes('//public-properties[@name=\'Contact2\' and @value=\'\']/ancestor::cm-handles')
- >> []
- inventoryPersistence.queryDataNodes('//public-properties/ancestor::cm-handles')
- >> [pNFDemo1, pNFDemo2, pNFDemo3, pNFDemo4]
-
- inventoryPersistence.queryDataNodes('//cm-handles[@id=\'PNFDemo\']') >> [pNFDemo1]
- inventoryPersistence.queryDataNodes('//cm-handles[@id=\'PNFDemo2\']') >> [pNFDemo2]
- inventoryPersistence.queryDataNodes('//cm-handles[@id=\'PNFDemo3\']') >> [pNFDemo3]
- inventoryPersistence.queryDataNodes('//cm-handles[@id=\'PNFDemo4\']') >> [pNFDemo4]
-
- inventoryPersistence.getDataNode('/dmi-registry') >> dmiRegistry
-
- inventoryPersistence.getDataNode('/dmi-registry/cm-handles[@id=\'PNFDemo1\']') >> pNFDemo1
- inventoryPersistence.getDataNode('/dmi-registry/cm-handles[@id=\'PNFDemo2\']') >> pNFDemo2
- inventoryPersistence.getDataNode('/dmi-registry/cm-handles[@id=\'PNFDemo3\']') >> pNFDemo3
- inventoryPersistence.getDataNode('/dmi-registry/cm-handles[@id=\'PNFDemo4\']') >> pNFDemo4
-
- inventoryPersistence.queryAnchors(['MODULE-NAME-001']) >> [new Anchor(name: 'PNFDemo1'), new Anchor(name: 'PNFDemo2'), new Anchor(name: 'PNFDemo3')]
- inventoryPersistence.queryAnchors(['MODULE-NAME-004']) >> []
- inventoryPersistence.queryAnchors(['MODULE-NAME-003', 'MODULE-NAME-002']) >> [new Anchor(name: 'PNFDemo4')]
- inventoryPersistence.queryAnchors(['MODULE-NAME-002', 'MODULE-NAME-003']) >> [new Anchor(name: 'PNFDemo4')]
- inventoryPersistence.queryAnchors(['MODULE-NAME-004', 'MODULE-NAME-002']) >> []
- inventoryPersistence.queryAnchors(['MODULE-NAME-002', 'MODULE-NAME-004']) >> []
- inventoryPersistence.queryAnchors(['MODULE-NAME-002']) >> [new Anchor(name: 'PNFDemo2'), new Anchor(name: 'PNFDemo4')]
- inventoryPersistence.getAnchors() >> [new Anchor(name: 'PNFDemo1'), new Anchor(name: 'PNFDemo2'), new Anchor(name: 'PNFDemo3'), new Anchor(name: 'PNFDemo4')]
+
+ def 'Retrieve all CMHandleIds for empty query parameters' () {
+ given: 'We query without any parameters'
+ def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
+ and: 'the inventoryPersistence returns all four CmHandleIds'
+ mockInventoryPersistence.getDataNode(*_) >> [dmiRegistry]
+ when: 'the query executed'
+ def resultSet = objectUnderTest.queryCmHandleIdsForInventory(cmHandleQueryParameters)
+ then: 'the size of the result list equals the size of all cmHandleIds.'
+ resultSet.size() == 4
+ }
+
+ def 'Retrieve CMHandleIds when #scenario.' () {
+ given: 'a query object created with #condition'
+ def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
+ def conditionProperties = createConditionProperties(conditionName, [['some-key': 'some-value']])
+ cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
+ and: 'the inventoryPersistence returns different CmHandleIds'
+ partiallyMockedCmHandleQueries.queryCmHandlePublicProperties(*_) >> cmHandlesWithMatchingPublicProperties
+ partiallyMockedCmHandleQueries.queryCmHandleAdditionalProperties(*_) >> cmHandlesWithMatchingPrivateProperties
+ when: 'the query executed'
+ def result = objectUnderTestSpy.queryCmHandleIdsForInventory(cmHandleQueryParameters)
+ then: 'the expected number of results are returned.'
+ assert result.size() == expectedCmHandleIdsSize
+ where: 'the following data is used'
+ scenario | conditionName | cmHandlesWithMatchingPublicProperties | cmHandlesWithMatchingPrivateProperties || expectedCmHandleIdsSize
+ 'all properties, only public matching' | 'hasAllProperties' | queryResultCmHandleMap | null || 2
+ 'all properties, no matching cm handles' | 'hasAllProperties' | [:] | [:] || 0
+ 'additional properties, some matching cm handles' | 'hasAllAdditionalProperties' | [:] | queryResultCmHandleMap || 2
+ 'additional properties, no matching cm handles' | 'hasAllAdditionalProperties' | null | [:] || 0
+ }
+
+ def 'Retrieve CMHandleIds by different DMI properties with #scenario.' () {
+ given: 'a query object created with dmi plugin as condition'
+ def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
+ def conditionProperties = createConditionProperties('cmHandleWithDmiPlugin', [['some-key': 'some-value']])
+ cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
+ and: 'the inventoryPersistence returns different CmHandleIds'
+ partiallyMockedCmHandleQueries.getCmHandlesByDmiPluginIdentifier(*_) >> cmHandleQueryResult
+ when: 'the query executed'
+ def result = objectUnderTestSpy.queryCmHandleIdsForInventory(cmHandleQueryParameters)
+ then: 'the expected number of results are returned.'
+ assert result.size() == expectedCmHandleIdsSize
+ where: 'the following data is used'
+ scenario | cmHandleQueryResult || expectedCmHandleIdsSize
+ 'some matches' | queryResultCmHandleMap.values() || 2
+ 'no matches' | [] || 0
+ }
+
+ static def createCmHandleMap(cmHandleIds) {
+ def cmHandleMap = [:]
+ cmHandleIds.each{ cmHandleMap[it] = new NcmpServiceCmHandle(cmHandleId : it) }
+ return cmHandleMap
+ }
+
+ def createConditionProperties(String conditionName, List<Map<String, String>> conditionParameters) {
+ return new ConditionProperties(conditionName : conditionName, conditionParameters : conditionParameters)
+ }
+
+ def static createDataNodeList(dataNodeIds) {
+ def dataNodes =[]
+ dataNodeIds.each{ dataNodes << new DataNode(xpath: "/dmi-registry/cm-handles[@id='${it}']", leaves: ['id':it]) }
+ return dataNodes
}
}