CpsPath Query Optimization
[cps.git] / cps-path-parser / src / test / groovy / org / onap / cps / cpspath / parser / CpsPathUtilSpec.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.cpspath.parser
22
23 import org.springframework.util.StopWatch
24 import spock.lang.Specification
25
26 class CpsPathUtilSpec extends Specification {
27
28     def 'Normalized xpaths for list index values using #scenario'() {
29         when: 'xpath with #scenario is parsed'
30             def result = CpsPathUtil.getNormalizedXpath(xpath)
31         then: 'normalized path uses single quotes for leave values'
32             result == "/parent/child[@common-leaf-name='123']"
33         where: 'the following xpaths are used'
34             scenario        | xpath
35             'no quotes'     | '/parent/child[@common-leaf-name=123]'
36             'double quotes' | '/parent/child[@common-leaf-name="123"]'
37             'single quotes' | "/parent/child[@common-leaf-name='123']"
38     }
39
40     def 'Normalized parent xpaths'() {
41         when: 'a given xpath with #scenario is parsed'
42             def result = CpsPathUtil.getNormalizedParentXpath(xpath)
43         then: 'the result is the expected parent path'
44             result == expectedParentPath
45         where: 'the following xpaths are used'
46             scenario                         | xpath                                 || expectedParentPath
47             'no child'                       | '/parent'                             || ''
48             'child and parent'               | '/parent/child'                       || '/parent'
49             'grand child'                    | '/parent/child/grandChild'            || '/parent/child'
50             'parent & top is list element'   | '/parent[@id=1]/child'                || "/parent[@id='1']"
51             'parent is list element'         | '/parent/child[@id=1]/grandChild'     || "/parent/child[@id='1']"
52             'parent is list element with /'  | "/parent/child[@id='a/b']/grandChild" || "/parent/child[@id='a/b']"
53             'parent is list element with ['  | "/parent/child[@id='a[b']/grandChild" || "/parent/child[@id='a[b']"
54             'parent is list element using "' | '/parent/child[@id="x"]/grandChild'   || "/parent/child[@id='x']"
55     }
56
57     def 'Recognizing (absolute) xpaths to List elements'() {
58         expect: 'check for list returns the correct values'
59             assert CpsPathUtil.isPathToListElement(xpath) == expectList
60         where: 'the following xpaths are used'
61             xpath                  || expectList
62             '/parent[@id=1]'       || true
63             '/parent[@id=1]/child' || false
64             '/parent/child[@id=1]' || true
65             '//child[@id=1]'       || false
66     }
67
68     def 'Parsing Exception'() {
69         when: 'a invalid xpath is parsed'
70             CpsPathUtil.getNormalizedXpath('///')
71         then: 'a path parsing exception is thrown'
72             thrown(PathParsingException)
73     }
74
75     def 'CPS Path Processing Performance Test.'() {
76         when: '200,000 paths are processed'
77             def setupStopWatch = new StopWatch()
78             setupStopWatch.start()
79             (1..100000).each {
80                 CpsPathUtil.getNormalizedXpath('/long/path/to/see/if/it/adds/paring/time/significantly/parent/child[@common-leaf-name="123"]')
81                 CpsPathUtil.getNormalizedXpath('//child[@other-leaf=1]/leaf-name[text()="search"]/ancestor::parent')
82             }
83             setupStopWatch.stop()
84         then: 'it takes less then 10,000 milliseconds'
85             // In CI this actually takes about 3-5 sec  which  is approx. 50+ parser executions per millisecond!
86             assert setupStopWatch.getTotalTimeMillis() < 10000
87     }
88
89 }