Merge "Implement merging all ncmp datastore endpoints into one"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / inventory / CmHandleQueriesImplSpec.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.inventory
22
23 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
24 import org.onap.cps.spi.CpsDataPersistenceService
25 import org.onap.cps.spi.model.DataNode
26 import spock.lang.Shared
27 import spock.lang.Specification
28
29 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
30 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
31
32 class CmHandleQueriesImplSpec extends Specification {
33     def cpsDataPersistenceService = Mock(CpsDataPersistenceService)
34
35     def objectUnderTest = new CmHandleQueriesImpl(cpsDataPersistenceService)
36
37     @Shared
38     def static sampleDataNodes = [new DataNode()]
39
40     def static pnfDemo = createDataNode('PNFDemo')
41     def static pnfDemo2 = createDataNode('PNFDemo2')
42     def static pnfDemo3 = createDataNode('PNFDemo3')
43     def static pnfDemo4 = createDataNode('PNFDemo4')
44
45     def static pnfDemoCmHandle = new NcmpServiceCmHandle(cmHandleId: 'PNFDemo')
46     def static pnfDemo2CmHandle = new NcmpServiceCmHandle(cmHandleId: 'PNFDemo2')
47     def static pnfDemo3CmHandle = new NcmpServiceCmHandle(cmHandleId: 'PNFDemo3')
48
49     def 'Query CmHandles with public properties query pair.'() {
50         given: 'the DataNodes queried for a given cpsPath are returned from the persistence service.'
51             mockResponses()
52         when: 'a query on cmhandle public properties is performed with a public property pair'
53             def returnedCmHandlesWithData = objectUnderTest.queryCmHandlePublicProperties(publicPropertyPairs)
54         then: 'the correct cm handle data objects are returned'
55             returnedCmHandlesWithData.keySet().containsAll(expectedCmHandleIds)
56             returnedCmHandlesWithData.keySet().size() == expectedCmHandleIds.size()
57         where: 'the following data is used'
58             scenario                         | publicPropertyPairs                                                                           || expectedCmHandleIds
59             'single property matches'        | ['Contact' : 'newemailforstore@bookstore.com']                                                || ['PNFDemo', '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 'Query CmHandles using empty public properties query pair.'() {
66         when: 'a query on CmHandle public properties is executed using an empty map'
67             def returnedCmHandlesWithData = objectUnderTest.queryCmHandlePublicProperties([:])
68         then: 'no cm handles are returned'
69             returnedCmHandlesWithData.keySet().size() == 0
70     }
71
72     def 'Combine two query results where #scenario.'() {
73         when: 'two query results in the form of a map of NcmpServiceCmHandles are combined into a single query result'
74             def result = objectUnderTest.combineCmHandleQueries(firstQuery, secondQuery)
75         then: 'the returned result is the same as the expected result'
76             result == expectedResult
77         where:
78             scenario                                                     | firstQuery                                                 | secondQuery                                                || expectedResult
79             'two queries with unique and non unique entries exist'       | ['PNFDemo': pnfDemoCmHandle, 'PNFDemo2': pnfDemo2CmHandle] | ['PNFDemo': pnfDemoCmHandle, 'PNFDemo3': pnfDemo3CmHandle] || ['PNFDemo': pnfDemoCmHandle]
80             'the first query contains entries and second query is empty' | ['PNFDemo': pnfDemoCmHandle, 'PNFDemo2': pnfDemo2CmHandle] | [:]                                                        || [:]
81             'the second query contains entries and first query is empty' | [:]                                                        | ['PNFDemo': pnfDemoCmHandle, 'PNFDemo3': pnfDemo3CmHandle] || [:]
82             'the first query contains entries and second query is null'  | ['PNFDemo': pnfDemoCmHandle, 'PNFDemo2': pnfDemo2CmHandle] | null                                                       || ['PNFDemo': pnfDemoCmHandle, 'PNFDemo2': pnfDemo2CmHandle]
83             'the second query contains entries and first query is null'  | null                                                       | ['PNFDemo': pnfDemoCmHandle, 'PNFDemo3': pnfDemo3CmHandle] || ['PNFDemo': pnfDemoCmHandle, 'PNFDemo3': pnfDemo3CmHandle]
84             'both queries are empty'                                     | [:]                                                        | [:]                                                        || [:]
85             'both queries are null'                                      | null                                                       | null                                                       || null
86     }
87
88     def 'Get Cm Handles By State'() {
89         given: 'a cm handle state to query'
90             def cmHandleState = CmHandleState.ADVISED
91         and: 'the persistence service returns a list of data nodes'
92             cpsDataPersistenceService.queryDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
93                 '//state[@cm-handle-state="ADVISED"]/ancestor::cm-handles', INCLUDE_ALL_DESCENDANTS) >> sampleDataNodes
94         when: 'cm handles are fetched by state'
95             def result = objectUnderTest.queryCmHandlesByState(cmHandleState)
96         then: 'the returned result matches the result from the persistence service'
97             assert result == sampleDataNodes
98     }
99
100     def 'Get Cm Handles state by Cm-Handle Id'() {
101         given: 'a cm handle state to query'
102             def cmHandleState = CmHandleState.READY
103         and: 'cps data service returns a list of data nodes'
104             cpsDataPersistenceService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
105                 '/dmi-registry/cm-handles[@id=\'some-cm-handle\']/state', OMIT_DESCENDANTS) >> new DataNode(leaves: ['cm-handle-state': 'READY'])
106         when: 'cm handles are fetched by state and id'
107             def result = objectUnderTest.getCmHandleState('some-cm-handle')
108         then: 'the returned result is a list of data nodes returned by cps data service'
109             assert result == new DataNode(leaves: ['cm-handle-state': 'READY'])
110     }
111
112     def 'Retrieve Cm Handles By Operational Sync State : UNSYNCHRONIZED'() {
113         given: 'a cm handle state to query'
114             def cmHandleState = CmHandleState.READY
115         and: 'cps data service returns a list of data nodes'
116             cpsDataPersistenceService.queryDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
117                 '//state/datastores/operational[@sync-state="'+'UNSYNCHRONIZED'+'"]/ancestor::cm-handles', OMIT_DESCENDANTS) >> sampleDataNodes
118         when: 'cm handles are fetched by the UNSYNCHRONIZED operational sync state'
119             def result = objectUnderTest.queryCmHandlesByOperationalSyncState(DataStoreSyncState.UNSYNCHRONIZED)
120         then: 'the returned result is a list of data nodes returned by cps data service'
121             assert result == sampleDataNodes
122     }
123
124     def 'Retrieve cm handle by cps path '() {
125         given: 'a cm handle state to query based on the cps path'
126             def cmHandleDataNode = new DataNode(xpath: 'xpath', leaves: ['cm-handle-state': 'LOCKED'])
127             def cpsPath = '//cps-path'
128         and: 'cps data service returns a valid data node'
129             cpsDataPersistenceService.queryDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
130                 cpsPath + '/ancestor::cm-handles', INCLUDE_ALL_DESCENDANTS)
131                 >> Arrays.asList(cmHandleDataNode)
132         when: 'get cm handles by cps path is invoked'
133             def result = objectUnderTest.queryCmHandleDataNodesByCpsPath(cpsPath, INCLUDE_ALL_DESCENDANTS)
134         then: 'the returned result is a list of data nodes returned by cps data service'
135             assert result.contains(cmHandleDataNode)
136     }
137
138     def 'Get all cm handles by dmi plugin identifier'() {
139         given: 'the DataNodes queried for a given cpsPath are returned from the persistence service.'
140             mockResponses()
141         when: 'cm Handles are fetched for a given dmi plugin identifier'
142             def result = objectUnderTest.getCmHandlesByDmiPluginIdentifier('my-dmi-plugin-identifier')
143         then: 'result is the correct size'
144             assert result.size() == 3
145         and: 'result contains the correct cm handles'
146             assert result.cmHandleId.containsAll('PNFDemo', 'PNFDemo2', 'PNFDemo4')
147     }
148
149     void mockResponses() {
150         cpsDataPersistenceService.queryDataNodes(_, _, '//public-properties[@name=\"Contact\" and @value=\"newemailforstore@bookstore.com\"]/ancestor::cm-handles', _) >> [pnfDemo, pnfDemo2, pnfDemo4]
151         cpsDataPersistenceService.queryDataNodes(_, _, '//public-properties[@name=\"wont_match\" and @value=\"wont_match\"]/ancestor::cm-handles', _) >> []
152         cpsDataPersistenceService.queryDataNodes(_, _, '//public-properties[@name=\"Contact2\" and @value=\"newemailforstore2@bookstore.com\"]/ancestor::cm-handles', _) >> [pnfDemo4]
153         cpsDataPersistenceService.queryDataNodes(_, _, '//public-properties[@name=\"Contact2\" and @value=\"\"]/ancestor::cm-handles', _) >> []
154         cpsDataPersistenceService.queryDataNodes(_, _, '//state[@cm-handle-state=\"READY\"]/ancestor::cm-handles', _) >> [pnfDemo, pnfDemo3]
155         cpsDataPersistenceService.queryDataNodes(_, _, '//state[@cm-handle-state=\"LOCKED\"]/ancestor::cm-handles', _) >> [pnfDemo2, pnfDemo4]
156         cpsDataPersistenceService.queryDataNodes('NCMP-Admin','ncmp-dmi-registry','/dmi-registry/cm-handles[@dmi-service-name=\'my-dmi-plugin-identifier\']',OMIT_DESCENDANTS) >> [pnfDemo, pnfDemo2]
157         cpsDataPersistenceService.queryDataNodes('NCMP-Admin','ncmp-dmi-registry','/dmi-registry/cm-handles[@dmi-data-service-name=\'my-dmi-plugin-identifier\']',OMIT_DESCENDANTS) >> [pnfDemo,pnfDemo4]
158         cpsDataPersistenceService.queryDataNodes('NCMP-Admin','ncmp-dmi-registry','/dmi-registry/cm-handles[@dmi-model-service-name=\'my-dmi-plugin-identifier\']',OMIT_DESCENDANTS) >> [pnfDemo2,pnfDemo4]
159     }
160
161     def static createDataNode(dataNodeId) {
162         return new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'' + dataNodeId + '\']', leaves: ['id':dataNodeId])
163     }
164 }