Add 'direct' keyword to descendants option to query direct children (ep1)
[cps.git] / integration-test / src / test / groovy / org / onap / cps / integration / performance / cps / CpsDataServiceLimitsPerfTest.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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.integration.performance.cps
22
23 import java.time.OffsetDateTime
24 import org.onap.cps.api.CpsDataService
25 import org.onap.cps.integration.performance.base.CpsPerfTestBase
26
27 import static org.onap.cps.spi.FetchDescendantsOption.DIRECT_CHILDREN_ONLY
28 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
29
30 class CpsDataServiceLimitsPerfTest extends CpsPerfTestBase {
31
32     CpsDataService objectUnderTest
33
34     def setup() { objectUnderTest = cpsDataService }
35
36     def 'Create 33,000 books (note further tests depend on this running first).'() {
37         given: 'an anchor containing a bookstore with one category'
38             cpsAdminService.createAnchor(CPS_PERFORMANCE_TEST_DATASPACE, BOOKSTORE_SCHEMA_SET, 'limitsAnchor')
39             def parentNodeData = '{"bookstore": { "categories": [{ "code": 1, "name": "Test", "books" : [] }] }}'
40             cpsDataService.saveData(CPS_PERFORMANCE_TEST_DATASPACE, 'limitsAnchor', parentNodeData, OffsetDateTime.now())
41         when: '33,000 books are added'
42             stopWatch.start()
43             for (int i = 1; i <= 33_000; i+=100) {
44                 def booksData = '{"books":[' + (i..<i+100).collect {'{ "title": "' + it + '" }' }.join(',') + ']}'
45                 cpsDataService.saveData(CPS_PERFORMANCE_TEST_DATASPACE, 'limitsAnchor', '/bookstore/categories[@code=1]', booksData, OffsetDateTime.now())
46             }
47             stopWatch.stop()
48             def durationInMillis = stopWatch.getTotalTimeMillis()
49         then: 'the operation completes within 10 seconds'
50             recordAndAssertPerformance("Creating 33,000 books", 10_000, durationInMillis)
51     }
52
53     def 'Get data nodes from multiple xpaths 32K (2^15) limit exceeded.'() {
54         given: '33,000 xpaths'
55             def xpaths = (1..33_000).collect { "/bookstore/categories[@code=1]/books[@title='${it}']".toString() }
56         when: 'a single operation is executed to get all datanodes with given xpaths'
57             def results = objectUnderTest.getDataNodesForMultipleXpaths(CPS_PERFORMANCE_TEST_DATASPACE, 'limitsAnchor', xpaths, OMIT_DESCENDANTS)
58         then: '33,000 data nodes are returned'
59             assert results.size() == 33_000
60     }
61
62     def 'Delete multiple data nodes 32K (2^15) limit exceeded.'() {
63         given: 'existing data nodes'
64             def countOfDataNodesBeforeDelete = countDataNodes()
65         and: 'a list of 33,000 xpaths'
66             def xpaths = (1..33_000).collect { "/bookstore/categories[@code=1]/books[@title='${it}']".toString() }
67         when: 'a single operation is executed to delete all datanodes with given xpaths'
68             objectUnderTest.deleteDataNodes(CPS_PERFORMANCE_TEST_DATASPACE, 'limitsAnchor', xpaths, OffsetDateTime.now())
69         then: '33,000 data nodes are deleted'
70             def countOfDataNodesAfterDelete = countDataNodes()
71             assert countOfDataNodesBeforeDelete - countOfDataNodesAfterDelete == 33_000
72     }
73
74     def 'Delete data nodes from multiple anchors 32K (2^15) limit exceeded.'() {
75         given: '33,000 anchor names'
76             def anchorNames = (1..33_000).collect { "size-of-this-name-does-not-matter-for-limit-" + it }
77         when: 'a single operation is executed to delete all datanodes in given anchors'
78             objectUnderTest.deleteDataNodes(CPS_PERFORMANCE_TEST_DATASPACE, anchorNames, OffsetDateTime.now())
79         then: 'a database exception is not thrown'
80             noExceptionThrown()
81     }
82
83     def 'Clean up test data.'() {
84         when:
85             stopWatch.start()
86             cpsDataService.deleteDataNodes(CPS_PERFORMANCE_TEST_DATASPACE, 'limitsAnchor', OffsetDateTime.now())
87             cpsAdminService.deleteAnchor(CPS_PERFORMANCE_TEST_DATASPACE, 'limitsAnchor')
88             stopWatch.stop()
89             def durationInMillis = stopWatch.getTotalTimeMillis()
90         then: 'test data is deleted in 10 seconds'
91             recordAndAssertPerformance("Deleting test data", 10_000, durationInMillis)
92     }
93
94     def countDataNodes() {
95         def results = objectUnderTest.getDataNodes(CPS_PERFORMANCE_TEST_DATASPACE, 'limitsAnchor', '/bookstore/categories[@code=1]', DIRECT_CHILDREN_ONLY)
96         return results[0].childDataNodes.size()
97     }
98
99 }