Merge "Fix release notes"
[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 com.fasterxml.jackson.databind.ObjectMapper
24 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandlerQueryService
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 org.onap.cps.utils.JsonObjectMapper
32 import spock.lang.Specification
33
34 import java.util.stream.Collectors
35
36 class NetworkCmProxyCmHandlerQueryServiceSpec extends Specification {
37
38     def cpsDataPersistenceService = Mock(CpsDataPersistenceService)
39     def cpsAdminPersistenceService = Mock(CpsAdminPersistenceService)
40
41     NetworkCmProxyCmHandlerQueryService objectUnderTest = new NetworkCmProxyCmHandlerQueryServiceImpl(
42         cpsDataPersistenceService, cpsAdminPersistenceService, new JsonObjectMapper(new ObjectMapper())
43     )
44
45     def 'Retrieve cm handles with public properties when #scenario.'() {
46         given: 'a condition property'
47             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
48             def conditionProperties = new ConditionProperties()
49             conditionProperties.conditionName = 'hasAllProperties'
50             conditionProperties.conditionParameters = publicProperties
51             cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
52         and: 'mock services'
53             mockResponses()
54         when: 'the service is invoked'
55             def returnedCmHandles = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
56         then: 'the correct expected cm handles are returned'
57             returnedCmHandles.stream().map(d -> d.leaves.get('id').toString()).collect(Collectors.toList()) == expectedCmHandleIds
58         where: 'the following data is used'
59             scenario                                       | publicProperties                                                                                  || expectedCmHandleIds
60             'single matching property'                     | [['Contact' : 'newemailforstore@bookstore.com']]                                                  || ['PNFDemo', 'PNFDemo2', 'PNFDemo4']
61             'public property dont match'                   | [['wont_match' : 'wont_match']]                                                                   || []
62             '2 properties, only one match (and)'           | [['Contact' : 'newemailforstore@bookstore.com'], ['Contact2': 'newemailforstore2@bookstore.com']] || ['PNFDemo4']
63             '2 properties, no match (and)'                 | [['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 returnedCmHandles = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
77         then: 'the correct expected cm handles are returned'
78             returnedCmHandles.stream().map(d -> d.leaves.get('id').toString()).collect(Collectors.toList()) == expectedCmHandleIds
79         where: 'the following data is used'
80             scenario                               | moduleNames                                                             || expectedCmHandleIds
81             'single matching module name'          | [['moduleName' : 'MODULE-NAME-001']]                                    || ['PNFDemo2', 'PNFDemo3', 'PNFDemo']
82             'module name dont match'               | [['moduleName' : 'MODULE-NAME-004']]                                    || []
83             '2 module names, only one match (and)' | [['moduleName' : 'MODULE-NAME-002'], ['moduleName': 'MODULE-NAME-003']] || ['PNFDemo4']
84             '2 module names, no match (and)'       | [['moduleName' : 'MODULE-NAME-002'], ['moduleName': 'MODULE-NAME-004']] || []
85     }
86
87     def 'Retrieve cm handles with combined queries when #scenario.'() {
88         given: 'condition properties'
89             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
90             def conditionProperties1 = new ConditionProperties()
91             conditionProperties1.conditionName = 'hasAllProperties'
92             conditionProperties1.conditionParameters = publicProperties
93             def conditionProperties2 = new ConditionProperties()
94             conditionProperties2.conditionName = 'hasAllModules'
95             conditionProperties2.conditionParameters = moduleNames
96             cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties1,conditionProperties2])
97         and: 'mock services'
98             mockResponses()
99         when: 'the service is invoked'
100             def returnedCmHandles = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
101         then: 'the correct expected cm handles are returned'
102             returnedCmHandles.stream().map(d -> d.leaves.get('id').toString()).collect(Collectors.toList()) == expectedCmHandleIds
103         where: 'the following data is used'
104             scenario                 | moduleNames                          | publicProperties                                   || expectedCmHandleIds
105             'particularly intersect' | [['moduleName' : 'MODULE-NAME-001']] | [['Contact' : 'newemailforstore@bookstore.com']]   || ['PNFDemo2', 'PNFDemo']
106             'empty intersect'        | [['moduleName' : 'MODULE-NAME-004']] | [['Contact' : 'newemailforstore@bookstore.com']]   || []
107             'total intersect'        | [['moduleName' : 'MODULE-NAME-002']] | [['Contact2' : 'newemailforstore2@bookstore.com']] || ['PNFDemo4']
108     }
109
110     def 'Retrieve cm handles when the query is empty.'() {
111         given: 'mock services'
112             mockResponses()
113         when: 'the service is invoked'
114             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
115             def returnedCmHandles = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
116         then: 'the correct expected cm handles are returned'
117             returnedCmHandles.stream().map(d -> d.leaves.get('id').toString()).collect(Collectors.toList()) == ['PNFDemo', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4']
118     }
119
120     void mockResponses() {
121         def pNFDemo = new DataNode(xpath: 'cmHandle/id[\'PNFDemo\']', leaves: ['id':'PNFDemo'])
122         def pNFDemo2 = new DataNode(xpath: 'cmHandle/id[\'PNFDemo2\']', leaves: ['id':'PNFDemo2'])
123         def pNFDemo3 = new DataNode(xpath: 'cmHandle/id[\'PNFDemo3\']', leaves: ['id':'PNFDemo3'])
124         def pNFDemo4 = new DataNode(xpath: 'cmHandle/id[\'PNFDemo4\']', leaves: ['id':'PNFDemo4'])
125
126         cpsDataPersistenceService.queryDataNodes(_, _, '//public-properties[@name=\'Contact\' and @value=\'newemailforstore@bookstore.com\']/ancestor::cm-handles', _)
127                 >> [pNFDemo, pNFDemo2, pNFDemo4]
128         cpsDataPersistenceService.queryDataNodes(_, _, '//public-properties[@name=\'wont_match\' and @value=\'wont_match\']/ancestor::cm-handles', _)
129                 >> []
130         cpsDataPersistenceService.queryDataNodes(_, _, '//public-properties[@name=\'Contact2\' and @value=\'newemailforstore2@bookstore.com\']/ancestor::cm-handles', _)
131                 >> [pNFDemo4]
132         cpsDataPersistenceService.queryDataNodes(_, _, '//public-properties[@name=\'Contact2\' and @value=\'\']/ancestor::cm-handles', _)
133                 >> []
134         cpsDataPersistenceService.queryDataNodes(_, _, '//public-properties/ancestor::cm-handles', _)
135                 >> [pNFDemo, pNFDemo2, pNFDemo3, pNFDemo4]
136         cpsDataPersistenceService.queryDataNodes(_, _, '//cm-handles[@id=\'PNFDemo\']', _) >> [pNFDemo]
137         cpsDataPersistenceService.queryDataNodes(_, _, '//cm-handles[@id=\'PNFDemo2\']', _) >> [pNFDemo2]
138         cpsDataPersistenceService.queryDataNodes(_, _, '//cm-handles[@id=\'PNFDemo3\']', _) >> [pNFDemo3]
139         cpsDataPersistenceService.queryDataNodes(_, _, '//cm-handles[@id=\'PNFDemo4\']', _) >> [pNFDemo4]
140
141         cpsAdminPersistenceService.queryAnchors(_, ['MODULE-NAME-001']) >> [new Anchor(name: 'PNFDemo2'), new Anchor(name: 'PNFDemo3'), new Anchor(name: 'PNFDemo')]
142         cpsAdminPersistenceService.queryAnchors(_, ['MODULE-NAME-004']) >> []
143         cpsAdminPersistenceService.queryAnchors(_, ['MODULE-NAME-003', 'MODULE-NAME-002']) >> [new Anchor(name: 'PNFDemo4')]
144         cpsAdminPersistenceService.queryAnchors(_, ['MODULE-NAME-002', 'MODULE-NAME-003']) >> [new Anchor(name: 'PNFDemo4')]
145         cpsAdminPersistenceService.queryAnchors(_, ['MODULE-NAME-004', 'MODULE-NAME-002']) >> []
146         cpsAdminPersistenceService.queryAnchors(_, ['MODULE-NAME-002', 'MODULE-NAME-004']) >> []
147         cpsAdminPersistenceService.queryAnchors(_, ['MODULE-NAME-002']) >> [new Anchor(name: 'PNFDemo2'), new Anchor(name: 'PNFDemo4')]
148     }
149 }