Merge "Define Initial Data Sync Enabled Flag and state"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / NetworkCmProxyCmHandlerQueryServiceSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl
22
23 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandlerQueryService
24 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
25 import org.onap.cps.spi.model.Anchor
26 import org.onap.cps.spi.model.CmHandleQueryServiceParameters
27 import org.onap.cps.spi.model.ConditionProperties
28 import org.onap.cps.spi.model.DataNode
29 import spock.lang.Specification
30
31 import java.util.stream.Collectors
32
33 class NetworkCmProxyCmHandlerQueryServiceSpec extends Specification {
34
35     def inventoryPersistence = Mock(InventoryPersistence)
36
37     NetworkCmProxyCmHandlerQueryService objectUnderTest = new NetworkCmProxyCmHandlerQueryServiceImpl(inventoryPersistence)
38
39     def 'Retrieve cm handles with public properties when #scenario.'() {
40         given: 'a condition property'
41             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
42             def conditionProperties = new ConditionProperties()
43             conditionProperties.conditionName = 'hasAllProperties'
44             conditionProperties.conditionParameters = publicProperties
45             cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
46         and: 'mock services'
47             mockResponses()
48         when: 'a query is execute (with and without Data)'
49             def returnedCmHandlesJustIds = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
50             def returnedCmHandlesWithData = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
51         then: 'the correct expected cm handles ids are returned'
52             returnedCmHandlesJustIds == expectedCmHandleIds as Set
53         and: 'the correct cm handle data objects are returned'
54             returnedCmHandlesWithData.stream().map(dataNode -> dataNode.cmHandleId).collect(Collectors.toSet()) == expectedCmHandleIds as Set
55         where: 'the following data is used'
56             scenario                         | publicProperties                                                                                  || expectedCmHandleIds
57             'single property matches'        | [['Contact' : 'newemailforstore@bookstore.com']]                                                  || ['PNFDemo1', 'PNFDemo2', 'PNFDemo4']
58             'public property does not match' | [['wont_match' : 'wont_match']]                                                                   || []
59             '2 properties, only one match'   | [['Contact' : 'newemailforstore@bookstore.com'], ['Contact2': 'newemailforstore2@bookstore.com']] || ['PNFDemo4']
60             '2 properties, no matches'       | [['Contact' : 'newemailforstore@bookstore.com'], ['Contact2': '']]                                || []
61     }
62
63     def 'Retrieve cm handles with module names when #scenario.'() {
64         given: 'a condition property'
65             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
66             def conditionProperties = new ConditionProperties()
67             conditionProperties.conditionName = 'hasAllModules'
68             conditionProperties.conditionParameters = moduleNames
69             cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
70         and: 'mock services'
71             mockResponses()
72         when: 'the service is invoked'
73             def returnedCmHandlesJustIds = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
74             def returnedCmHandlesWithData = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
75         then: 'the correct expected cm handles are returned'
76             returnedCmHandlesJustIds == expectedCmHandleIds as Set
77             returnedCmHandlesWithData.stream().map(dataNode -> dataNode.cmHandleId).collect(Collectors.toSet()) == expectedCmHandleIds as Set
78         where: 'the following data is used'
79             scenario                         | moduleNames                                                             || expectedCmHandleIds
80             'single matching module name'    | [['moduleName' : 'MODULE-NAME-001']]                                    || ['PNFDemo3', 'PNFDemo1', 'PNFDemo2']
81             'module name dont match'         | [['moduleName' : 'MODULE-NAME-004']]                                    || []
82             '2 module names, only one match' | [['moduleName' : 'MODULE-NAME-002'], ['moduleName': 'MODULE-NAME-003']] || ['PNFDemo4']
83             '2 module names, no matches'     | [['moduleName' : 'MODULE-NAME-002'], ['moduleName': 'MODULE-NAME-004']] || []
84     }
85
86     def 'Retrieve cm handles with combined queries when #scenario.'() {
87         given: 'condition properties'
88             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
89             def conditionProperties1 = new ConditionProperties()
90             conditionProperties1.conditionName = 'hasAllProperties'
91             conditionProperties1.conditionParameters = publicProperties
92             def conditionProperties2 = new ConditionProperties()
93             conditionProperties2.conditionName = 'hasAllModules'
94             conditionProperties2.conditionParameters = moduleNames
95             cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties1,conditionProperties2])
96         and: 'mock services'
97             mockResponses()
98         when: 'the service is invoked'
99             def returnedCmHandlesJustIds = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
100             def returnedCmHandlesWithData = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
101         then: 'the correct expected cm handles are returned'
102             returnedCmHandlesJustIds == expectedCmHandleIds as Set
103             returnedCmHandlesWithData.stream().map(d -> d.cmHandleId).collect(Collectors.toSet()) == expectedCmHandleIds as Set
104         where: 'the following data is used'
105             scenario                 | moduleNames                          | publicProperties                                   || expectedCmHandleIds
106             'particularly intersect' | [['moduleName' : 'MODULE-NAME-001']] | [['Contact' : 'newemailforstore@bookstore.com']]   || ['PNFDemo1', 'PNFDemo2']
107             'empty intersect'        | [['moduleName' : 'MODULE-NAME-004']] | [['Contact' : 'newemailforstore@bookstore.com']]   || []
108             'total intersect'        | [['moduleName' : 'MODULE-NAME-002']] | [['Contact2' : 'newemailforstore2@bookstore.com']] || ['PNFDemo4']
109     }
110
111     def 'Retrieve cm handles when the query is empty.'() {
112         given: 'mock services'
113             mockResponses()
114         when: 'the service is invoked'
115             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
116             def returnedCmHandlesJustIds = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
117             def returnedCmHandlesWithData = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
118         then: 'the correct expected cm handles are returned'
119             returnedCmHandlesJustIds == ['PNFDemo1', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4'] as Set
120             returnedCmHandlesWithData.stream().map(d -> d.cmHandleId).collect(Collectors.toSet()) == ['PNFDemo1', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4'] as Set
121     }
122
123     void mockResponses() {
124         def pNFDemo1 = new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'PNFDemo1\']', leaves: ['id':'PNFDemo1'])
125         def pNFDemo2 = new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'PNFDemo2\']', leaves: ['id':'PNFDemo2'])
126         def pNFDemo3 = new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'PNFDemo3\']', leaves: ['id':'PNFDemo3'])
127         def pNFDemo4 = new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'PNFDemo4\']', leaves: ['id':'PNFDemo4'])
128         def dmiRegistry = new DataNode(xpath: '/dmi-registry', childDataNodes: [pNFDemo1, pNFDemo2, pNFDemo3, pNFDemo4])
129
130         inventoryPersistence.queryDataNodes('//public-properties[@name=\'Contact\' and @value=\'newemailforstore@bookstore.com\']/ancestor::cm-handles')
131                 >> [pNFDemo1, pNFDemo2, pNFDemo4]
132         inventoryPersistence.queryDataNodes('//public-properties[@name=\'wont_match\' and @value=\'wont_match\']/ancestor::cm-handles')
133                 >> []
134         inventoryPersistence.queryDataNodes('//public-properties[@name=\'Contact2\' and @value=\'newemailforstore2@bookstore.com\']/ancestor::cm-handles')
135                 >> [pNFDemo4]
136         inventoryPersistence.queryDataNodes('//public-properties[@name=\'Contact2\' and @value=\'\']/ancestor::cm-handles')
137                 >> []
138         inventoryPersistence.queryDataNodes('//public-properties/ancestor::cm-handles')
139                 >> [pNFDemo1, pNFDemo2, pNFDemo3, pNFDemo4]
140
141         inventoryPersistence.queryDataNodes('//cm-handles[@id=\'PNFDemo\']') >> [pNFDemo1]
142         inventoryPersistence.queryDataNodes('//cm-handles[@id=\'PNFDemo2\']') >> [pNFDemo2]
143         inventoryPersistence.queryDataNodes('//cm-handles[@id=\'PNFDemo3\']') >> [pNFDemo3]
144         inventoryPersistence.queryDataNodes('//cm-handles[@id=\'PNFDemo4\']') >> [pNFDemo4]
145
146         inventoryPersistence.getDataNode('/dmi-registry') >> dmiRegistry
147
148         inventoryPersistence.getDataNode('/dmi-registry/cm-handles[@id=\'PNFDemo1\']') >> pNFDemo1
149         inventoryPersistence.getDataNode('/dmi-registry/cm-handles[@id=\'PNFDemo2\']') >> pNFDemo2
150         inventoryPersistence.getDataNode('/dmi-registry/cm-handles[@id=\'PNFDemo3\']') >> pNFDemo3
151         inventoryPersistence.getDataNode('/dmi-registry/cm-handles[@id=\'PNFDemo4\']') >> pNFDemo4
152
153         inventoryPersistence.queryAnchors(['MODULE-NAME-001']) >> [new Anchor(name: 'PNFDemo1'), new Anchor(name: 'PNFDemo2'), new Anchor(name: 'PNFDemo3')]
154         inventoryPersistence.queryAnchors(['MODULE-NAME-004']) >> []
155         inventoryPersistence.queryAnchors(['MODULE-NAME-003', 'MODULE-NAME-002']) >> [new Anchor(name: 'PNFDemo4')]
156         inventoryPersistence.queryAnchors(['MODULE-NAME-002', 'MODULE-NAME-003']) >> [new Anchor(name: 'PNFDemo4')]
157         inventoryPersistence.queryAnchors(['MODULE-NAME-004', 'MODULE-NAME-002']) >> []
158         inventoryPersistence.queryAnchors(['MODULE-NAME-002', 'MODULE-NAME-004']) >> []
159         inventoryPersistence.queryAnchors(['MODULE-NAME-002']) >> [new Anchor(name: 'PNFDemo2'), new Anchor(name: 'PNFDemo4')]
160         inventoryPersistence.getAnchors() >> [new Anchor(name: 'PNFDemo1'), new Anchor(name: 'PNFDemo2'), new Anchor(name: 'PNFDemo3'), new Anchor(name: 'PNFDemo4')]
161     }
162 }