2 * ============LICENSE_START=======================================================
3 * Copyright (C) 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.integration.functional
24 import org.onap.cps.api.CpsDataService
25 import org.onap.cps.integration.base.FunctionalSpecBase
26 import org.onap.cps.spi.FetchDescendantsOption
27 import org.onap.cps.spi.exceptions.AlreadyDefinedExceptionBatch
28 import org.onap.cps.spi.exceptions.AnchorNotFoundException
29 import org.onap.cps.spi.exceptions.CpsAdminException
30 import org.onap.cps.spi.exceptions.CpsPathException
31 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
32 import org.onap.cps.spi.exceptions.DataNodeNotFoundExceptionBatch
33 import org.onap.cps.spi.exceptions.DataValidationException
34 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
36 import java.time.OffsetDateTime
38 import static org.onap.cps.spi.FetchDescendantsOption.DIRECT_CHILDREN_ONLY
39 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
40 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
42 class CpsDataServiceIntegrationSpec extends FunctionalSpecBase {
44 CpsDataService objectUnderTest
45 def originalCountBookstoreChildNodes
46 def now = OffsetDateTime.now()
49 objectUnderTest = cpsDataService
50 originalCountBookstoreChildNodes = countDataNodesInBookstore()
53 def 'Read bookstore top-level container(s) using #fetchDescendantsOption.'() {
54 when: 'get data nodes for bookstore container'
55 def result = objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', fetchDescendantsOption)
56 then: 'the tree consist ouf of #expectNumberOfDataNodes data nodes'
57 assert countDataNodesInTree(result) == expectNumberOfDataNodes
58 and: 'the top level data node has the expected attribute and value'
59 assert result.leaves['bookstore-name'] == ['Easons']
60 and: 'they are from the correct dataspace'
61 assert result.dataspace == [FUNCTIONAL_TEST_DATASPACE_1]
62 and: 'they are from the correct anchor'
63 assert result.anchorName == [BOOKSTORE_ANCHOR_1]
64 where: 'the following option is used'
65 fetchDescendantsOption || expectNumberOfDataNodes
67 DIRECT_CHILDREN_ONLY || 6
68 INCLUDE_ALL_DESCENDANTS || 17
69 new FetchDescendantsOption(2) || 17
72 def 'Read bookstore top-level container(s) using "root" path variations.'() {
73 when: 'get data nodes for bookstore container'
74 def result = objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, root, OMIT_DESCENDANTS)
75 then: 'the tree consist ouf of one data node'
76 assert countDataNodesInTree(result) == 1
77 and: 'the top level data node has the expected attribute and value'
78 assert result.leaves['bookstore-name'] == ['Easons']
79 where: 'the following variations of "root" are used'
83 def 'Read data nodes with error: #cpsPath'() {
84 when: 'attempt to get data nodes using invalid path'
85 objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, DIRECT_CHILDREN_ONLY)
86 then: 'a #expectedException is thrown'
87 thrown(expectedException)
89 cpsPath || expectedException
90 'invalid path' || CpsPathException
91 '/non-existing-path' || DataNodeNotFoundException
94 def 'Read (multiple) data nodes (batch) with #cpsPath'() {
95 when: 'attempt to get data nodes using invalid path'
96 objectUnderTest.getDataNodesForMultipleXpaths(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, [ cpsPath ], DIRECT_CHILDREN_ONLY)
97 then: 'no exception is thrown'
100 cpsPath << [ 'invalid path', '/non-existing-path' ]
103 def 'Delete root data node.'() {
104 when: 'the "root" is deleted'
105 objectUnderTest.deleteDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, [ '/' ], now)
106 and: 'attempt to get the top level data node'
107 objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', DIRECT_CHILDREN_ONLY)
108 then: 'an datanode not found exception is thrown'
109 thrown(DataNodeNotFoundException)
111 restoreBookstoreDataAnchor(1)
114 def 'Add and Delete a (container) data node using #scenario.'() {
115 when: 'the new datanode is saved'
116 objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , parentXpath, json, now)
117 then: 'it can be retrieved by its normalized xpath'
118 def result = objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, normalizedXpathToNode, DIRECT_CHILDREN_ONLY)
119 assert result.size() == 1
120 assert result[0].xpath == normalizedXpathToNode
121 and: 'there is now one extra datanode'
122 assert originalCountBookstoreChildNodes + 1 == countDataNodesInBookstore()
123 when: 'the new datanode is deleted'
124 objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, normalizedXpathToNode, now)
125 then: 'the original number of data nodes is restored'
126 assert originalCountBookstoreChildNodes == countDataNodesInBookstore()
128 scenario | parentXpath | json || normalizedXpathToNode
129 'normalized parent xpath' | '/bookstore' | '{"webinfo": {"domain-name":"ourbookstore.com", "contact-email":"info@ourbookstore.com" }}' || "/bookstore/webinfo"
130 'non-normalized parent xpath' | '/bookstore/categories[ @code="1"]' | '{"books": {"title":"new" }}' || "/bookstore/categories[@code='1']/books[@title='new']"
133 def 'Attempt to create a top level data node using root.'() {
134 given: 'a new anchor'
135 cpsAdminService.createAnchor(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_SCHEMA_SET, 'newAnchor1');
136 when: 'attempt to save new top level datanode'
137 def json = '{"bookstore": {"bookstore-name": "New Store"} }'
138 objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, 'newAnchor1' , '/', json, now)
139 then: 'since there is no data a data node not found exception is thrown'
140 thrown(DataNodeNotFoundException)
143 def 'Attempt to save top level data node that already exist'() {
144 when: 'attempt to save already existing top level node'
145 def json = '{"bookstore": {} }'
146 objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, json, now)
147 then: 'an exception that (one cps paths is) already defined is thrown '
148 def exceptionThrown = thrown(AlreadyDefinedExceptionBatch)
149 exceptionThrown.alreadyDefinedXpaths == [ '/bookstore' ] as Set
151 restoreBookstoreDataAnchor(1)
154 def 'Delete a single datanode with invalid path.'() {
155 when: 'attempt to delete a single datanode with invalid path'
156 objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/invalid path', now)
157 then: 'a cps path parser exception is thrown'
158 thrown(CpsPathException)
161 def 'Delete multiple data nodes with invalid path.'() {
162 when: 'attempt to delete datanode collection with invalid path'
163 objectUnderTest.deleteDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, ['/invalid path'], now)
164 then: 'the error is silently ignored'
168 def 'Delete single data node with non-existing path.'() {
169 when: 'attempt to delete a single datanode non-existing invalid path'
170 objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/does/not/exist', now)
171 then: 'a datanode not found exception is thrown'
172 thrown(DataNodeNotFoundException)
175 def 'Delete multiple data nodes with non-existing path(s).'() {
176 when: 'attempt to delete a single datanode non-existing invalid path'
177 objectUnderTest.deleteDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, ['/does/not/exist'], now)
178 then: 'a datanode not found (batch) exception is thrown'
179 thrown(DataNodeNotFoundExceptionBatch)
182 def 'Add and Delete list (element) data nodes.'() {
183 given: 'two new (categories) data nodes'
184 def json = '{"categories": [ {"code":"new1"}, {"code":"new2" } ] }'
185 when: 'the new list elements are saved'
186 objectUnderTest.saveListElements(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
187 then: 'they can be retrieved by their xpaths'
188 objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', DIRECT_CHILDREN_ONLY).size() == 1
189 objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new2"]', DIRECT_CHILDREN_ONLY).size() == 1
190 and: 'there are now two extra data nodes'
191 assert originalCountBookstoreChildNodes + 2 == countDataNodesInBookstore()
192 when: 'the new elements are deleted'
193 objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', now)
194 objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new2"]', now)
195 then: 'the original number of data nodes is restored'
196 assert originalCountBookstoreChildNodes == countDataNodesInBookstore()
199 def 'Add list (element) data nodes that already exist.'() {
200 given: 'two (categories) data nodes, one new and one existing'
201 def json = '{"categories": [ {"code":"1"}, {"code":"new1"} ] }'
202 when: 'attempt to save the list element'
203 objectUnderTest.saveListElements(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
204 then: 'an exception that (one cps paths is) already defined is thrown '
205 def exceptionThrown = thrown(AlreadyDefinedExceptionBatch)
206 exceptionThrown.alreadyDefinedXpaths == [ '/bookstore/categories[@code=\'1\']' ] as Set
207 and: 'there is now one extra data nodes'
208 assert originalCountBookstoreChildNodes + 1 == countDataNodesInBookstore()
210 restoreBookstoreDataAnchor(1)
213 def 'Add and Delete list (element) data nodes using lists specific method.'() {
214 given: 'a new (categories) data nodes'
215 def json = '{"categories": [ {"code":"new1"} ] }'
216 and: 'the new list element is saved'
217 objectUnderTest.saveListElements(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
218 when: 'the new element is deleted'
219 objectUnderTest.deleteListOrListElement(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', now)
220 then: 'the original number of data nodes is restored'
221 assert originalCountBookstoreChildNodes == countDataNodesInBookstore()
224 def 'Add and Delete a batch of lists (element) data nodes.'() {
225 given: 'two new (categories) data nodes in two separate batches'
226 def json1 = '{"categories": [ {"code":"new1"} ] }'
227 def json2 = '{"categories": [ {"code":"new2"} ] } '
228 when: 'the batches of new list element(s) are saved'
229 objectUnderTest.saveListElementsBatch(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', [json1, json2], now)
230 then: 'they can be retrieved by their xpaths'
231 assert objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', DIRECT_CHILDREN_ONLY).size() == 1
232 assert objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new2"]', DIRECT_CHILDREN_ONLY).size() == 1
233 and: 'there are now two extra data nodes'
234 assert originalCountBookstoreChildNodes + 2 == countDataNodesInBookstore()
235 when: 'the new elements are deleted'
236 objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', now)
237 objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new2"]', now)
238 then: 'the original number of data nodes is restored'
239 assert originalCountBookstoreChildNodes == countDataNodesInBookstore()
242 def 'Add and Delete a batch of lists (element) data nodes with partial success.'() {
243 given: 'two new (categories) data nodes in two separate batches'
244 def jsonNewElement = '{"categories": [ {"code":"new1"} ] }'
245 def jsonExistingElement = '{"categories": [ {"code":"1"} ] } '
246 when: 'the batches of new list element(s) are saved'
247 objectUnderTest.saveListElementsBatch(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', [jsonNewElement, jsonExistingElement], now)
248 then: 'an already defined (batch) exception is thrown for the existing path'
249 def exceptionThrown = thrown(AlreadyDefinedExceptionBatch)
250 assert exceptionThrown.alreadyDefinedXpaths == [ '/bookstore/categories[@code=\'1\']' ] as Set
251 and: 'there is now one extra data node'
252 assert originalCountBookstoreChildNodes + 1 == countDataNodesInBookstore()
254 restoreBookstoreDataAnchor(1)
257 def 'Attempt to add empty lists.'() {
258 when: 'the batches of new list element(s) are saved'
259 objectUnderTest.replaceListContent(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', [ ], now)
260 then: 'an admin exception is thrown'
261 thrown(CpsAdminException)
264 def 'Add child error scenario: #scenario.'() {
265 when: 'attempt to add a child data node with #scenario'
266 objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, parentXpath, json, now)
267 then: 'a #expectedException is thrown'
268 thrown(expectedException)
269 where: 'the following data is used'
270 scenario | parentXpath | json || expectedException
271 'parent does not exist' | '/bookstore/categories[@code="unknown"]' | '{"books": [ {"title":"new"} ] } ' || DataNodeNotFoundException
272 'already existing child' | '/bookstore' | '{"categories": [ {"code":"1"} ] }' || AlreadyDefinedExceptionBatch
275 def 'Add multiple child data nodes with partial success.'() {
276 given: 'one existing and one new list element'
277 def json = '{"categories": [ {"code":"1"}, {"code":"new"} ] }'
278 when: 'attempt to add the elements'
279 objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', json, now)
280 then: 'an already defined (batch) exception is thrown for the existing path'
281 def thrown = thrown(AlreadyDefinedExceptionBatch)
282 assert thrown.alreadyDefinedXpaths == [ "/bookstore/categories[@code='1']" ] as Set
283 and: 'the new data node has been added i.e. can be retrieved'
284 assert objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new"]', DIRECT_CHILDREN_ONLY).size() == 1
287 def 'Replace list content #scenario.'() {
288 given: 'the bookstore categories 1 and 2 exist and have at least 1 child each '
289 assert countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="1"]', DIRECT_CHILDREN_ONLY)) > 1
290 assert countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="2"]', DIRECT_CHILDREN_ONLY)) > 1
291 when: 'the categories list is replaced with just category "1" and without child nodes (books)'
292 def json = '{"categories": [ {"code":"' +categoryCode + '"' + childJson + '} ] }'
293 objectUnderTest.replaceListContent(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', json, now)
294 then: 'the new replaced category can be retrieved but has no children anymore'
295 assert expectedNumberOfDataNodes == countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="' +categoryCode + '"]', DIRECT_CHILDREN_ONLY))
296 when: 'attempt to retrieve a category (code) not in the new list'
297 objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="2"]', DIRECT_CHILDREN_ONLY)
298 then: 'a datanode not found exception occurs'
299 thrown(DataNodeNotFoundException)
301 restoreBookstoreDataAnchor(1)
302 where: 'the following data is used'
303 scenario | categoryCode | childJson || expectedNumberOfDataNodes
304 'existing code, no children' | '1' | '' || 1
305 'existing code, new child' | '1' | ', "books" : [ { "title": "New Book" } ]' || 2
306 'existing code, existing child' | '1' | ', "books" : [ { "title": "Matilda" } ]' || 2
307 'new code, new child' | 'new' | ', "books" : [ { "title": "New Book" } ]' || 2
310 def 'Update multiple data node leaves.'() {
311 given: 'Updated json for bookstore data'
312 def jsonData = "{'book-store:books':{'lang':'English/French','price':100,'title':'Matilda'}}"
313 when: 'update is performed for leaves'
314 objectUnderTest.updateNodeLeaves(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_2, "/bookstore/categories[@code='1']", jsonData, now)
315 then: 'the updated data nodes are retrieved'
316 def result = cpsDataService.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_2, "/bookstore/categories[@code=1]/books[@title='Matilda']", INCLUDE_ALL_DESCENDANTS)
317 and: 'the leaf values are updated as expected'
318 assert result.leaves['lang'] == ['English/French']
319 assert result.leaves['price'] == [100]
321 restoreBookstoreDataAnchor(2)
324 def 'Update data node leaves for node that has no leaves (yet).'() {
325 given: 'new (webinfo) datanode without leaves'
326 def json = '{"webinfo": {} }'
327 objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
328 when: 'update is performed to add a leaf'
329 def updatedJson = '{"webinfo": {"domain-name":"new leaf data"}}'
330 objectUnderTest.updateNodeLeaves(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, "/bookstore", updatedJson, now)
331 then: 'the updated data nodes are retrieved'
332 def result = cpsDataService.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, "/bookstore/webinfo", INCLUDE_ALL_DESCENDANTS)
333 and: 'the leaf value is updated as expected'
334 assert result.leaves['domain-name'] == ['new leaf data']
336 restoreBookstoreDataAnchor(1)
339 def 'Update multiple data leaves error scenario: #scenario.'() {
340 when: 'attempt to update data node for #scenario'
341 objectUnderTest.updateNodeLeaves(dataspaceName, anchorName, xpath, 'irrelevant json data', now)
342 then: 'a #expectedException is thrown'
343 thrown(expectedException)
344 where: 'the following data is used'
345 scenario | dataspaceName | anchorName | xpath || expectedException
346 'invalid dataspace name' | 'Invalid Dataspace' | 'not-relevant' | '/not relevant' || DataValidationException
347 'invalid anchor name' | FUNCTIONAL_TEST_DATASPACE_1 | 'INVALID ANCHOR' | '/not relevant' || DataValidationException
348 'non-existing dataspace' | 'non-existing-dataspace' | 'not-relevant' | '/not relevant' || DataspaceNotFoundException
349 'non-existing anchor' | FUNCTIONAL_TEST_DATASPACE_1 | 'non-existing-anchor' | '/not relevant' || AnchorNotFoundException
350 'non-existing-xpath' | FUNCTIONAL_TEST_DATASPACE_1 | BOOKSTORE_ANCHOR_1 | '/non-existing' || DataValidationException
353 def 'Update data nodes and descendants.'() {
354 given: 'some web info for the bookstore'
355 def json = '{"webinfo": {"domain-name":"ourbookstore.com" ,"contact-email":"info@ourbookstore.com" }}'
356 objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
357 when: 'the webinfo (container) is updated'
358 json = '{"webinfo": {"domain-name":"newdomain.com" ,"contact-email":"info@newdomain.com" }}'
359 objectUnderTest.updateDataNodeAndDescendants(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', json, now)
360 then: 'webinfo has been updated with teh new details'
361 def result = objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/webinfo', DIRECT_CHILDREN_ONLY)
362 result.leaves.'domain-name'[0] == 'newdomain.com'
363 result.leaves.'contact-email'[0] == 'info@newdomain.com'
365 restoreBookstoreDataAnchor(1)
368 def countDataNodesInBookstore() {
369 return countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', INCLUDE_ALL_DESCENDANTS))