Merge "Distributed map setup for Module and Data Sync"
[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.CpsAdminPersistenceService
26 import org.onap.cps.spi.CpsDataPersistenceService
27 import org.onap.cps.spi.model.Anchor
28 import org.onap.cps.spi.model.CmHandleQueryServiceParameters
29 import org.onap.cps.spi.model.ConditionProperties
30 import org.onap.cps.spi.model.DataNode
31 import spock.lang.Specification
32
33 import java.util.stream.Collectors
34
35 class NetworkCmProxyCmHandlerQueryServiceSpec extends Specification {
36
37     def inventoryPersistence = Mock(InventoryPersistence)
38
39     NetworkCmProxyCmHandlerQueryService objectUnderTest = new NetworkCmProxyCmHandlerQueryServiceImpl(inventoryPersistence)
40
41     def 'Retrieve cm handles with public properties when #scenario.'() {
42         given: 'a condition property'
43             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
44             def conditionProperties = new ConditionProperties()
45             conditionProperties.conditionName = 'hasAllProperties'
46             conditionProperties.conditionParameters = publicProperties
47             cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
48         and: 'mock services'
49             mockResponses()
50         when: 'a query is execute (with and without Data)'
51             def returnedCmHandlesJustIds = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
52             def returnedCmHandlesWithData = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
53         then: 'the correct expected cm handles ids are returned'
54             returnedCmHandlesJustIds == expectedCmHandleIds as Set
55         and: 'the correct cm handle data objects are returned'
56             returnedCmHandlesWithData.stream().map(dataNode -> dataNode.cmHandleId).collect(Collectors.toSet()) == expectedCmHandleIds as Set
57         where: 'the following data is used'
58             scenario                         | publicProperties                                                                                  || expectedCmHandleIds
59             'single property matches'        | [['Contact' : 'newemailforstore@bookstore.com']]                                                  || ['PNFDemo1', 'PNFDemo2', 'PNFDemo4']
60             'public property does not match' | [['wont_match' : 'wont_match']]                                                                   || []
61             '2 properties, only one match'   | [['Contact' : 'newemailforstore@bookstore.com'], ['Contact2': 'newemailforstore2@bookstore.com']] || ['PNFDemo4']
62             '2 properties, no matches'       | [['Contact' : 'newemailforstore@bookstore.com'], ['Contact2': '']]                                || []
63     }
64
65     def 'Retrieve cm handles with module names when #scenario.'() {
66         given: 'a condition property'
67             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
68             def conditionProperties = new ConditionProperties()
69             conditionProperties.conditionName = 'hasAllModules'
70             conditionProperties.conditionParameters = moduleNames
71             cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
72         and: 'mock services'
73             mockResponses()
74         when: 'the service is invoked'
75             def returnedCmHandlesJustIds = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
76             def returnedCmHandlesWithData = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
77         then: 'the correct expected cm handles are returned'
78             returnedCmHandlesJustIds == expectedCmHandleIds as Set
79             returnedCmHandlesWithData.stream().map(dataNode -> dataNode.cmHandleId).collect(Collectors.toSet()) == expectedCmHandleIds as Set
80         where: 'the following data is used'
81             scenario                         | moduleNames                                                             || expectedCmHandleIds
82             'single matching module name'    | [['moduleName' : 'MODULE-NAME-001']]                                    || ['PNFDemo3', 'PNFDemo1', 'PNFDemo2']
83             'module name dont match'         | [['moduleName' : 'MODULE-NAME-004']]                                    || []
84             '2 module names, only one match' | [['moduleName' : 'MODULE-NAME-002'], ['moduleName': 'MODULE-NAME-003']] || ['PNFDemo4']
85             '2 module names, no matches'     | [['moduleName' : 'MODULE-NAME-002'], ['moduleName': 'MODULE-NAME-004']] || []
86     }
87
88     def 'Retrieve cm handles with combined queries when #scenario.'() {
89         given: 'condition properties'
90             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
91             def conditionProperties1 = new ConditionProperties()
92             conditionProperties1.conditionName = 'hasAllProperties'
93             conditionProperties1.conditionParameters = publicProperties
94             def conditionProperties2 = new ConditionProperties()
95             conditionProperties2.conditionName = 'hasAllModules'
96             conditionProperties2.conditionParameters = moduleNames
97             cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties1,conditionProperties2])
98         and: 'mock services'
99             mockResponses()
100         when: 'the service is invoked'
101             def returnedCmHandlesJustIds = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
102             def returnedCmHandlesWithData = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
103         then: 'the correct expected cm handles are returned'
104             returnedCmHandlesJustIds == expectedCmHandleIds as Set
105             returnedCmHandlesWithData.stream().map(d -> d.cmHandleId).collect(Collectors.toSet()) == expectedCmHandleIds as Set
106         where: 'the following data is used'
107             scenario                 | moduleNames                          | publicProperties                                   || expectedCmHandleIds
108             'particularly intersect' | [['moduleName' : 'MODULE-NAME-001']] | [['Contact' : 'newemailforstore@bookstore.com']]   || ['PNFDemo1', 'PNFDemo2']
109             'empty intersect'        | [['moduleName' : 'MODULE-NAME-004']] | [['Contact' : 'newemailforstore@bookstore.com']]   || []
110             'total intersect'        | [['moduleName' : 'MODULE-NAME-002']] | [['Contact2' : 'newemailforstore2@bookstore.com']] || ['PNFDemo4']
111     }
112
113     def 'Retrieve cm handles when the query is empty.'() {
114         given: 'mock services'
115             mockResponses()
116         when: 'the service is invoked'
117             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
118             def returnedCmHandlesJustIds = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
119             def returnedCmHandlesWithData = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
120         then: 'the correct expected cm handles are returned'
121             returnedCmHandlesJustIds == ['PNFDemo1', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4'] as Set
122             returnedCmHandlesWithData.stream().map(d -> d.cmHandleId).collect(Collectors.toSet()) == ['PNFDemo1', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4'] as Set
123     }
124
125     void mockResponses() {
126         def pNFDemo1 = new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'PNFDemo1\']', leaves: ['id':'PNFDemo1'])
127         def pNFDemo2 = new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'PNFDemo2\']', leaves: ['id':'PNFDemo2'])
128         def pNFDemo3 = new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'PNFDemo3\']', leaves: ['id':'PNFDemo3'])
129         def pNFDemo4 = new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'PNFDemo4\']', leaves: ['id':'PNFDemo4'])
130         def dmiRegistry = new DataNode(xpath: '/dmi-registry', childDataNodes: [pNFDemo1, pNFDemo2, pNFDemo3, pNFDemo4])
131
132         inventoryPersistence.queryDataNodes('//public-properties[@name=\'Contact\' and @value=\'newemailforstore@bookstore.com\']/ancestor::cm-handles')
133                 >> [pNFDemo1, pNFDemo2, pNFDemo4]
134         inventoryPersistence.queryDataNodes('//public-properties[@name=\'wont_match\' and @value=\'wont_match\']/ancestor::cm-handles')
135                 >> []
136         inventoryPersistence.queryDataNodes('//public-properties[@name=\'Contact2\' and @value=\'newemailforstore2@bookstore.com\']/ancestor::cm-handles')
137                 >> [pNFDemo4]
138         inventoryPersistence.queryDataNodes('//public-properties[@name=\'Contact2\' and @value=\'\']/ancestor::cm-handles')
139                 >> []
140         inventoryPersistence.queryDataNodes('//public-properties/ancestor::cm-handles')
141                 >> [pNFDemo1, pNFDemo2, pNFDemo3, pNFDemo4]
142
143         inventoryPersistence.queryDataNodes('//cm-handles[@id=\'PNFDemo\']') >> [pNFDemo1]
144         inventoryPersistence.queryDataNodes('//cm-handles[@id=\'PNFDemo2\']') >> [pNFDemo2]
145         inventoryPersistence.queryDataNodes('//cm-handles[@id=\'PNFDemo3\']') >> [pNFDemo3]
146         inventoryPersistence.queryDataNodes('//cm-handles[@id=\'PNFDemo4\']') >> [pNFDemo4]
147
148         inventoryPersistence.getDataNode('/dmi-registry') >> dmiRegistry
149
150         inventoryPersistence.getDataNode('/dmi-registry/cm-handles[@id=\'PNFDemo1\']') >> pNFDemo1
151         inventoryPersistence.getDataNode('/dmi-registry/cm-handles[@id=\'PNFDemo2\']') >> pNFDemo2
152         inventoryPersistence.getDataNode('/dmi-registry/cm-handles[@id=\'PNFDemo3\']') >> pNFDemo3
153         inventoryPersistence.getDataNode('/dmi-registry/cm-handles[@id=\'PNFDemo4\']') >> pNFDemo4
154
155         inventoryPersistence.queryAnchors(['MODULE-NAME-001']) >> [new Anchor(name: 'PNFDemo1'), new Anchor(name: 'PNFDemo2'), new Anchor(name: 'PNFDemo3')]
156         inventoryPersistence.queryAnchors(['MODULE-NAME-004']) >> []
157         inventoryPersistence.queryAnchors(['MODULE-NAME-003', 'MODULE-NAME-002']) >> [new Anchor(name: 'PNFDemo4')]
158         inventoryPersistence.queryAnchors(['MODULE-NAME-002', 'MODULE-NAME-003']) >> [new Anchor(name: 'PNFDemo4')]
159         inventoryPersistence.queryAnchors(['MODULE-NAME-004', 'MODULE-NAME-002']) >> []
160         inventoryPersistence.queryAnchors(['MODULE-NAME-002', 'MODULE-NAME-004']) >> []
161         inventoryPersistence.queryAnchors(['MODULE-NAME-002']) >> [new Anchor(name: 'PNFDemo2'), new Anchor(name: 'PNFDemo4')]
162         inventoryPersistence.getAnchors() >> [new Anchor(name: 'PNFDemo1'), new Anchor(name: 'PNFDemo2'), new Anchor(name: 'PNFDemo3'), new Anchor(name: 'PNFDemo4')]
163     }
164 }