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