Merge "Add withTrustLevel condition to CmHandle Query API"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / inventory / CmHandleQueriesImplSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2023 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.api.inventory
23
24 import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel
25
26 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DATASPACE_NAME
27 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DMI_REGISTRY_ANCHOR
28 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT
29 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
30 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
31
32 import com.hazelcast.map.IMap
33 import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueriesImpl
34 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState
35 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState
36 import org.onap.cps.spi.CpsDataPersistenceService
37 import org.onap.cps.spi.model.DataNode
38 import spock.lang.Shared
39 import spock.lang.Specification
40
41 class CmHandleQueriesImplSpec extends Specification {
42     def cpsDataPersistenceService = Mock(CpsDataPersistenceService)
43     def trustLevelPerCmHandle = [ 'my completed cm handle': TrustLevel.COMPLETE, 'my untrusted cm handle': TrustLevel.NONE ]
44
45     def objectUnderTest = new CmHandleQueriesImpl(cpsDataPersistenceService, trustLevelPerCmHandle)
46
47     @Shared
48     def static sampleDataNodes = [new DataNode()]
49
50     def dataNodeWithPrivateField = '//additional-properties[@name=\"Contact3\" and @value=\"newemailforstore3@bookstore.com\"]/ancestor::cm-handles'
51
52     def static pnfDemo = createDataNode('PNFDemo')
53     def static pnfDemo2 = createDataNode('PNFDemo2')
54     def static pnfDemo3 = createDataNode('PNFDemo3')
55     def static pnfDemo4 = createDataNode('PNFDemo4')
56     def static pnfDemo5 = createDataNode('PNFDemo5')
57
58     def 'Query CmHandles with public properties query pair.'() {
59         given: 'the DataNodes queried for a given cpsPath are returned from the persistence service.'
60             mockResponses()
61         when: 'a query on cmhandle public properties is performed with a public property pair'
62             def result = objectUnderTest.queryCmHandlePublicProperties(publicPropertyPairs)
63         then: 'the correct cm handle data objects are returned'
64             result.containsAll(expectedCmHandleIds)
65             result.size() == expectedCmHandleIds.size()
66         where: 'the following data is used'
67             scenario                         | publicPropertyPairs                                                                      || expectedCmHandleIds
68             'single property matches'        | [Contact: 'newemailforstore@bookstore.com']                                              || ['PNFDemo', 'PNFDemo2', 'PNFDemo4']
69             'public property does not match' | [wont_match: 'wont_match']                                                               || []
70             '2 properties, only one match'   | [Contact: 'newemailforstore@bookstore.com', Contact2: 'newemailforstore2@bookstore.com'] || ['PNFDemo4']
71             '2 properties, no matches'       | [Contact: 'newemailforstore@bookstore.com', Contact2: '']                                || []
72     }
73
74     def 'Query cm handles on trust level'() {
75         given: 'query properties for trustlevel COMPLETE'
76             def trustLevelPropertyQueryPairs = ['trustLevel' : TrustLevel.COMPLETE.toString()]
77         when: 'the query is executed'
78             def result = objectUnderTest.queryCmHandlesByTrustLevel(trustLevelPropertyQueryPairs)
79         then: 'the result only contains the completed cm handle'
80             assert result.size() == 1
81             assert result[0] == 'my completed cm handle'
82     }
83
84     def 'Query CmHandles using empty public properties query pair.'() {
85         when: 'a query on CmHandle public properties is executed using an empty map'
86             def result = objectUnderTest.queryCmHandlePublicProperties([:])
87         then: 'no cm handles are returned'
88             result.size() == 0
89     }
90
91     def 'Query CmHandles using empty private properties query pair.'() {
92         when: 'a query on CmHandle private properties is executed using an empty map'
93             def result = objectUnderTest.queryCmHandleAdditionalProperties([:])
94         then: 'no cm handles are returned'
95             result.size() == 0
96     }
97
98     def 'Query CmHandles by a private field\'s value.'() {
99         given: 'a data node exists with a certain additional-property'
100             cpsDataPersistenceService.queryDataNodes(_, _, dataNodeWithPrivateField, _) >> [pnfDemo5]
101         when: 'a query on CmHandle private properties is executed using a map'
102             def result = objectUnderTest.queryCmHandleAdditionalProperties(['Contact3': 'newemailforstore3@bookstore.com'])
103         then: 'one cm handle is returned'
104             result.size() == 1
105     }
106
107     def 'Get CmHandles by it\'s state.'() {
108         given: 'a cm handle state to query'
109             def cmHandleState = CmHandleState.ADVISED
110         and: 'the persistence service returns a list of data nodes'
111             cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
112                 '//state[@cm-handle-state="ADVISED"]/ancestor::cm-handles', INCLUDE_ALL_DESCENDANTS) >> sampleDataNodes
113         when: 'cm handles are fetched by state'
114             def result = objectUnderTest.queryCmHandlesByState(cmHandleState)
115         then: 'the returned result matches the result from the persistence service'
116             assert result == sampleDataNodes
117     }
118
119     def 'Check the state of a cmHandle when #scenario.'() {
120         given: 'a cm handle state to compare'
121             def cmHandleState = state
122         and: 'the persistence service returns a list of data nodes'
123             cpsDataPersistenceService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
124                     NCMP_DMI_REGISTRY_PARENT + '/cm-handles[@id=\'some-cm-handle\']/state',
125                     OMIT_DESCENDANTS) >> [new DataNode(leaves: ['cm-handle-state': 'READY'])]
126         when: 'cm handles are compared by state'
127             def result = objectUnderTest.cmHandleHasState('some-cm-handle', cmHandleState)
128         then: 'the returned result matches the expected result from the persistence service'
129             result == expectedResult
130         where:
131             scenario                           | state                 || expectedResult
132             'the provided state matches'       | CmHandleState.READY   || true
133             'the provided state does not match'| CmHandleState.DELETED || false
134     }
135
136     def 'Get Cm Handles state by Cm-Handle Id'() {
137         given: 'a cm handle state to query'
138             def cmHandleState = CmHandleState.READY
139         and: 'cps data service returns a list of data nodes'
140             cpsDataPersistenceService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
141                     NCMP_DMI_REGISTRY_PARENT + '/cm-handles[@id=\'some-cm-handle\']/state',
142                     OMIT_DESCENDANTS) >> [new DataNode(leaves: ['cm-handle-state': 'READY'])]
143         when: 'cm handles are fetched by state and id'
144             def result = objectUnderTest.getCmHandleState('some-cm-handle')
145         then: 'the returned result is a list of data nodes returned by cps data service'
146             assert result == new DataNode(leaves: ['cm-handle-state': 'READY'])
147     }
148
149     def 'Retrieve Cm Handles By Operational Sync State : UNSYNCHRONIZED'() {
150         given: 'a cm handle state to query'
151             def cmHandleState = CmHandleState.READY
152         and: 'cps data service returns a list of data nodes'
153             cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
154                 '//state/datastores/operational[@sync-state="'+'UNSYNCHRONIZED'+'"]/ancestor::cm-handles', OMIT_DESCENDANTS) >> sampleDataNodes
155         when: 'cm handles are fetched by the UNSYNCHRONIZED operational sync state'
156             def result = objectUnderTest.queryCmHandlesByOperationalSyncState(DataStoreSyncState.UNSYNCHRONIZED)
157         then: 'the returned result is a list of data nodes returned by cps data service'
158             assert result == sampleDataNodes
159     }
160
161     def 'Retrieve cm handle by cps path '() {
162         given: 'a cm handle state to query based on the cps path'
163             def cmHandleDataNode = new DataNode(xpath: 'xpath', leaves: ['cm-handle-state': 'LOCKED'])
164             def cpsPath = '//cps-path'
165         and: 'cps data service returns a valid data node'
166             cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
167                 cpsPath + '/ancestor::cm-handles', INCLUDE_ALL_DESCENDANTS)
168                 >> Arrays.asList(cmHandleDataNode)
169         when: 'get cm handles by cps path is invoked'
170             def result = objectUnderTest.queryCmHandleAncestorsByCpsPath(cpsPath, INCLUDE_ALL_DESCENDANTS)
171         then: 'the returned result is a list of data nodes returned by cps data service'
172             assert result.contains(cmHandleDataNode)
173     }
174
175     def 'Get all cm handles by dmi plugin identifier'() {
176         given: 'the DataNodes queried for a given cpsPath are returned from the persistence service.'
177             mockResponses()
178         when: 'cm Handles are fetched for a given dmi plugin identifier'
179             def result = objectUnderTest.getCmHandleIdsByDmiPluginIdentifier('my-dmi-plugin-identifier')
180         then: 'result is the correct size'
181             assert result.size() == 3
182         and: 'result contains the correct cm handles'
183             assert result.containsAll('PNFDemo', 'PNFDemo2', 'PNFDemo4')
184     }
185
186     void mockResponses() {
187         cpsDataPersistenceService.queryDataNodes(_, _, '//public-properties[@name=\"Contact\" and @value=\"newemailforstore@bookstore.com\"]/ancestor::cm-handles', _) >> [pnfDemo, pnfDemo2, pnfDemo4]
188         cpsDataPersistenceService.queryDataNodes(_, _, '//public-properties[@name=\"wont_match\" and @value=\"wont_match\"]/ancestor::cm-handles', _) >> []
189         cpsDataPersistenceService.queryDataNodes(_, _, '//public-properties[@name=\"Contact2\" and @value=\"newemailforstore2@bookstore.com\"]/ancestor::cm-handles', _) >> [pnfDemo4]
190         cpsDataPersistenceService.queryDataNodes(_, _, '//public-properties[@name=\"Contact2\" and @value=\"\"]/ancestor::cm-handles', _) >> []
191         cpsDataPersistenceService.queryDataNodes(_, _, '//state[@cm-handle-state=\"READY\"]/ancestor::cm-handles', _) >> [pnfDemo, pnfDemo3]
192         cpsDataPersistenceService.queryDataNodes(_, _, '//state[@cm-handle-state=\"LOCKED\"]/ancestor::cm-handles', _) >> [pnfDemo2, pnfDemo4]
193         cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry/cm-handles[@dmi-service-name=\'my-dmi-plugin-identifier\']', OMIT_DESCENDANTS) >> [pnfDemo, pnfDemo2]
194         cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry/cm-handles[@dmi-data-service-name=\'my-dmi-plugin-identifier\']', OMIT_DESCENDANTS) >> [pnfDemo, pnfDemo4]
195         cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry/cm-handles[@dmi-model-service-name=\'my-dmi-plugin-identifier\']', OMIT_DESCENDANTS) >> [pnfDemo2, pnfDemo4]
196     }
197
198     def static createDataNode(dataNodeId) {
199         return new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'' + dataNodeId + '\']', leaves: ['id':dataNodeId])
200     }
201 }