3bbae2d08c04c8548d4ce5e7e638fe8149c3f9ba
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsPersistencePerfSpecBase.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.spi.impl
22
23 import org.onap.cps.spi.model.DataNode
24 import org.onap.cps.spi.model.DataNodeBuilder
25
26 class CpsPersistencePerfSpecBase extends CpsPersistenceSpecBase {
27
28     static final String PERF_TEST_DATA = '/data/perf-test.sql'
29     static final String PERF_DATASPACE = 'PERF-DATASPACE'
30     static final String PERF_ANCHOR = 'PERF-ANCHOR'
31     static final String PERF_TEST_PARENT = '/perf-parent-1'
32
33     static def xpathsToAllGrandChildren = []
34
35     def createLineage(cpsDataPersistenceService, numberOfChildren, numberOfGrandChildren, createLists) {
36         xpathsToAllGrandChildren = []
37         (1..numberOfChildren).each {
38             if (createLists) {
39                 def xpathFormat = "${PERF_TEST_PARENT}/perf-test-list-${it}[@key='%d']"
40                 def listElements = goForthAndMultiply(xpathFormat, numberOfGrandChildren)
41                 cpsDataPersistenceService.addListElements(PERF_DATASPACE, PERF_ANCHOR, PERF_TEST_PARENT, listElements)
42             } else {
43                 def xpathFormat = "${PERF_TEST_PARENT}/perf-test-child-${it}/perf-test-grand-child-%d"
44                 def grandChildren = goForthAndMultiply(xpathFormat, numberOfGrandChildren)
45                 def child = new DataNodeBuilder()
46                     .withXpath("${PERF_TEST_PARENT}/perf-test-child-${it}")
47                     .withChildDataNodes(grandChildren)
48                     .build()
49                 cpsDataPersistenceService.addChildDataNode(PERF_DATASPACE, PERF_ANCHOR, PERF_TEST_PARENT, child)
50             }
51         }
52     }
53
54     def goForthAndMultiply(xpathFormat, numberOfGrandChildren) {
55         def grandChildren = []
56         (1..numberOfGrandChildren).each {
57             def xpath = String.format(xpathFormat as String, it)
58             def grandChild = new DataNodeBuilder().withXpath(xpath).build()
59             xpathsToAllGrandChildren.add(grandChild.xpath)
60             grandChildren.add(grandChild)
61         }
62         return grandChildren
63     }
64
65     def countDataNodes(dataNodes) {
66         int nodeCount = 1
67         for (DataNode parent : dataNodes) {
68             for (DataNode child : parent.childDataNodes) {
69                 nodeCount = nodeCount + (countDataNodes(child))
70             }
71         }
72         return nodeCount
73     }
74 }