7e34fe282251c007649aefbea8ebad368e3565af
[cps.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2024 Nordix Foundation
4  *  Modifications Copyright (C) 2023 TechMahindra Ltd.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.impl.inventory
23
24 import org.onap.cps.api.CpsDataService
25 import org.onap.cps.api.CpsQueryService
26 import org.onap.cps.impl.utils.CpsValidator
27 import org.onap.cps.ncmp.api.inventory.models.TrustLevel
28 import org.onap.cps.ncmp.impl.inventory.models.CmHandleState
29 import org.onap.cps.spi.model.DataNode
30 import spock.lang.Shared
31 import spock.lang.Specification
32
33 import static org.onap.cps.ncmp.impl.inventory.NcmpPersistence.NCMP_DATASPACE_NAME
34 import static org.onap.cps.ncmp.impl.inventory.NcmpPersistence.NCMP_DMI_REGISTRY_ANCHOR
35 import static org.onap.cps.ncmp.impl.inventory.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT
36 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
37 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
38
39 class CmHandleQueryServiceImplSpec extends Specification {
40
41     def mockCpsQueryService = Mock(CpsQueryService)
42     def mockCpsDataService = Mock(CpsDataService)
43
44     def trustLevelPerDmiPlugin = [:]
45
46     def trustLevelPerCmHandle = [ 'PNFDemo': TrustLevel.COMPLETE, 'PNFDemo2': TrustLevel.NONE, 'PNFDemo4': TrustLevel.NONE ]
47
48     def mockCpsValidator = Mock(CpsValidator)
49
50     def objectUnderTest = new CmHandleQueryServiceImpl(mockCpsDataService, mockCpsQueryService, trustLevelPerDmiPlugin, trustLevelPerCmHandle, mockCpsValidator)
51
52     @Shared
53     def static sampleDataNodes = [new DataNode()]
54
55     def dataNodeWithPrivateField = '//additional-properties[@name=\"Contact3\" and @value=\"newemailforstore3@bookstore.com\"]/ancestor::cm-handles'
56
57     def static pnfDemo = createDataNode('PNFDemo')
58     def static pnfDemo2 = createDataNode('PNFDemo2')
59     def static pnfDemo3 = createDataNode('PNFDemo3')
60     def static pnfDemo4 = createDataNode('PNFDemo4')
61     def static pnfDemo5 = createDataNode('PNFDemo5')
62
63     def 'Query CmHandles with public properties query pair.'() {
64         given: 'the DataNodes queried for a given cpsPath are returned from the persistence service.'
65             mockResponses()
66         when: 'a query on cmhandle public properties is performed with a public property pair'
67             def result = objectUnderTest.queryCmHandlePublicProperties(publicPropertyPairs)
68         then: 'the correct cm handle data objects are returned'
69             result.containsAll(expectedCmHandleIds)
70             result.size() == expectedCmHandleIds.size()
71         where: 'the following data is used'
72             scenario                         | publicPropertyPairs                                                                      || expectedCmHandleIds
73             'single property matches'        | [Contact: 'newemailforstore@bookstore.com']                                              || ['PNFDemo', 'PNFDemo2', 'PNFDemo4']
74             'public property does not match' | [wont_match: 'wont_match']                                                               || []
75             '2 properties, only one match'   | [Contact: 'newemailforstore@bookstore.com', Contact2: 'newemailforstore2@bookstore.com'] || ['PNFDemo4']
76             '2 properties, no matches'       | [Contact: 'newemailforstore@bookstore.com', Contact2: '']                                || []
77     }
78
79     def 'Query cm handles on trust level'() {
80         given: 'query properties for trust level COMPLETE'
81             def trustLevelPropertyQueryPairs = ['trustLevel' : TrustLevel.COMPLETE.toString()]
82         and: 'the dmi cache has been initialised and "knows" about my-dmi-plugin-identifier'
83             trustLevelPerDmiPlugin.put('my-dmi-plugin-identifier', TrustLevel.COMPLETE)
84         and: 'the DataNodes queried for a given cpsPath are returned from the persistence service'
85             mockResponses()
86         when: 'the query is run'
87             def result = objectUnderTest.queryCmHandlesByTrustLevel(trustLevelPropertyQueryPairs)
88         then: 'the result contain trusted PNFDemo'
89             assert result.size() == 1
90             assert result[0] == 'PNFDemo'
91     }
92
93     def 'Query CmHandles using empty public properties query pair.'() {
94         when: 'a query on CmHandle public properties is executed using an empty map'
95             def result = objectUnderTest.queryCmHandlePublicProperties([:])
96         then: 'no cm handles are returned'
97             result.size() == 0
98     }
99
100     def 'Query CmHandles using empty private properties query pair.'() {
101         when: 'a query on CmHandle private properties is executed using an empty map'
102             def result = objectUnderTest.queryCmHandleAdditionalProperties([:])
103         then: 'no cm handles are returned'
104             result.size() == 0
105     }
106
107     def 'Query CmHandles by a private field\'s value.'() {
108         given: 'a data node exists with a certain additional-property'
109             mockCpsQueryService.queryDataNodes(_, _, dataNodeWithPrivateField, _) >> [pnfDemo5]
110         when: 'a query on CmHandle private properties is executed using a map'
111             def result = objectUnderTest.queryCmHandleAdditionalProperties(['Contact3': 'newemailforstore3@bookstore.com'])
112         then: 'one cm handle is returned'
113             result.size() == 1
114     }
115
116     def 'Get CmHandles by it\'s state.'() {
117         given: 'a cm handle state to query'
118             def cmHandleState = CmHandleState.ADVISED
119         and: 'the persistence service returns a list of data nodes'
120             mockCpsQueryService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
121                 '//state[@cm-handle-state="ADVISED"]/ancestor::cm-handles', INCLUDE_ALL_DESCENDANTS) >> sampleDataNodes
122         when: 'cm handles are fetched by state'
123             def result = objectUnderTest.queryCmHandlesByState(cmHandleState)
124         then: 'the returned result matches the result from the persistence service'
125             assert result == sampleDataNodes
126     }
127
128     def 'Check the state of a cmHandle when #scenario.'() {
129         given: 'a cm handle state to compare'
130             def cmHandleState = state
131         and: 'the persistence service returns a list of data nodes'
132             mockCpsDataService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
133                     NCMP_DMI_REGISTRY_PARENT + '/cm-handles[@id=\'some-cm-handle\']/state',
134                     OMIT_DESCENDANTS) >> [new DataNode(leaves: ['cm-handle-state': 'READY'])]
135         when: 'cm handles are compared by state'
136             def result = objectUnderTest.cmHandleHasState('some-cm-handle', cmHandleState)
137         then: 'the returned result matches the expected result from the persistence service'
138             result == expectedResult
139         where:
140             scenario                           | state                 || expectedResult
141             'the provided state matches'       | CmHandleState.READY   || true
142             'the provided state does not match'| CmHandleState.DELETED || false
143     }
144
145     def 'Get Cm Handles state by Cm-Handle Id'() {
146         given: 'a cm handle state to query'
147             def cmHandleState = CmHandleState.READY
148         and: 'cps data service returns a list of data nodes'
149             mockCpsDataService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
150                     NCMP_DMI_REGISTRY_PARENT + '/cm-handles[@id=\'some-cm-handle\']/state',
151                     OMIT_DESCENDANTS) >> [new DataNode(leaves: ['cm-handle-state': 'READY'])]
152         when: 'cm handles are fetched by state and id'
153             def result = objectUnderTest.getCmHandleState('some-cm-handle')
154         then: 'the returned result is a list of data nodes returned by cps data service'
155             assert result == new DataNode(leaves: ['cm-handle-state': 'READY'])
156     }
157
158     def 'Retrieve Cm Handles By Operational Sync State : UNSYNCHRONIZED'() {
159         given: 'a cm handle state to query'
160             def cmHandleState = CmHandleState.READY
161         and: 'cps data service returns a list of data nodes'
162             mockCpsQueryService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
163                 '//state/datastores/operational[@sync-state="'+'UNSYNCHRONIZED'+'"]/ancestor::cm-handles', OMIT_DESCENDANTS) >> sampleDataNodes
164         when: 'cm handles are fetched by the UNSYNCHRONIZED operational sync state'
165             def result = objectUnderTest.queryCmHandlesByOperationalSyncState(DataStoreSyncState.UNSYNCHRONIZED)
166         then: 'the returned result is a list of data nodes returned by cps data service'
167             assert result == sampleDataNodes
168     }
169
170     def 'Retrieve cm handle by cps path '() {
171         given: 'a cm handle state to query based on the cps path'
172             def cmHandleDataNode = new DataNode(xpath: "/dmi-registry/cm-handles[@id='ch-1']", leaves: ['id': 'ch-1'])
173             def cpsPath = "//state[@cm-handle-state='LOCKED']"
174         and: 'cps data service returns a valid data node for cm handle ancestor'
175             mockCpsQueryService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
176                 cpsPath + '/ancestor::cm-handles', INCLUDE_ALL_DESCENDANTS)
177                 >> Arrays.asList(cmHandleDataNode)
178         when: 'get cm handles by cps path is invoked'
179             def result = objectUnderTest.queryCmHandleAncestorsByCpsPath(cpsPath, INCLUDE_ALL_DESCENDANTS)
180         then: 'the returned result is a list of data nodes returned by cps data service'
181             assert result.contains(cmHandleDataNode)
182     }
183
184     def 'Retrieve cm handle by cps path querying cm handle directly'() {
185         given: 'a cm handle to query based on the cps path'
186             def cmHandleDataNode = new DataNode(xpath: "/dmi-registry/cm-handles[@id='ch-2']", leaves: ['id': 'ch-2'])
187             def cpsPath = "//cm-handles[@alternate-id='1']"
188         and: 'cps data service returns a valid data node'
189             mockCpsQueryService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
190                     cpsPath, INCLUDE_ALL_DESCENDANTS)
191                     >> Arrays.asList(cmHandleDataNode)
192         when: 'get cm handles by cps path is invoked'
193             def result = objectUnderTest.queryCmHandleAncestorsByCpsPath(cpsPath, INCLUDE_ALL_DESCENDANTS)
194         then: 'the returned result is a list of data nodes returned by cps data service'
195             assert result.contains(cmHandleDataNode)
196     }
197
198     def 'Get all cm handles by dmi plugin identifier'() {
199         given: 'the DataNodes queried for a given cpsPath are returned from the persistence service.'
200             mockResponses()
201         when: 'cm Handles are fetched for a given dmi plugin identifier'
202             def result = objectUnderTest.getCmHandleIdsByDmiPluginIdentifier('my-dmi-plugin-identifier')
203         then: 'result is the correct size'
204             assert result.size() == 3
205         and: 'result contains the correct cm handles'
206             assert result.containsAll('PNFDemo', 'PNFDemo2', 'PNFDemo4')
207     }
208
209     void mockResponses() {
210         mockCpsQueryService.queryDataNodes(_, _, '//public-properties[@name=\"Contact\" and @value=\"newemailforstore@bookstore.com\"]/ancestor::cm-handles', _) >> [pnfDemo, pnfDemo2, pnfDemo4]
211         mockCpsQueryService.queryDataNodes(_, _, '//public-properties[@name=\"wont_match\" and @value=\"wont_match\"]/ancestor::cm-handles', _) >> []
212         mockCpsQueryService.queryDataNodes(_, _, '//public-properties[@name=\"Contact2\" and @value=\"newemailforstore2@bookstore.com\"]/ancestor::cm-handles', _) >> [pnfDemo4]
213         mockCpsQueryService.queryDataNodes(_, _, '//public-properties[@name=\"Contact2\" and @value=\"\"]/ancestor::cm-handles', _) >> []
214         mockCpsQueryService.queryDataNodes(_, _, '//state[@cm-handle-state=\"READY\"]/ancestor::cm-handles', _) >> [pnfDemo, pnfDemo3]
215         mockCpsQueryService.queryDataNodes(_, _, '//state[@cm-handle-state=\"LOCKED\"]/ancestor::cm-handles', _) >> [pnfDemo2, pnfDemo4]
216         mockCpsQueryService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry/cm-handles[@dmi-service-name=\'my-dmi-plugin-identifier\']', OMIT_DESCENDANTS) >> [pnfDemo, pnfDemo2]
217         mockCpsQueryService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry/cm-handles[@dmi-data-service-name=\'my-dmi-plugin-identifier\']', OMIT_DESCENDANTS) >> [pnfDemo, pnfDemo4]
218         mockCpsQueryService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry/cm-handles[@dmi-model-service-name=\'my-dmi-plugin-identifier\']', OMIT_DESCENDANTS) >> [pnfDemo2, pnfDemo4]
219     }
220
221     def static createDataNode(dataNodeId) {
222         return new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'' + dataNodeId + '\']', leaves: ['id':dataNodeId])
223     }
224 }