Use constants for magic numbers in perf tests
[cps.git] / cps-service / src / test / groovy / org / onap / cps / spi / FetchDescendantsOptionSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.spi
23
24 import spock.lang.Specification
25
26 class FetchDescendantsOptionSpec extends Specification {
27
28     def 'Has next descendant for fetch descendant option: #scenario'() {
29         when: 'fetch descendant option with #depth depth'
30             def fetchDescendantsOption = new FetchDescendantsOption(depth)
31         then: 'next level descendants available: #expectedHasNext'
32             assert fetchDescendantsOption.hasNext() == expectedHasNext
33         where: 'following parameters are used'
34             scenario                  | depth || expectedHasNext
35             'omit descendants'        | 0     || false
36             'first child'             | 1     || true
37             'second child'            | 2     || true
38             'include all descendants' | -1    || true
39     }
40
41     def 'Has next descendant for fetch descendant option: invalid depth'() {
42         given: 'fetch descendant option with -2 depth'
43             def fetchDescendantsOption = new FetchDescendantsOption(-2)
44         when: 'next level descendants not available'
45             fetchDescendantsOption.hasNext()
46         then: 'exception thrown'
47             thrown IllegalArgumentException
48     }
49
50     def 'Next descendant for fetch descendant option: #scenario.'() {
51         when: 'fetch descendant option with #depth depth'
52             def fetchDescendantsOption = new FetchDescendantsOption(depth)
53         then: 'the next level of depth is as expected'
54             fetchDescendantsOption.next().depth == depth - 1
55         where: 'following parameters are used'
56             scenario                  | depth
57             'first child'             | 1
58             'second child'            | 2
59     }
60
61     def 'Next descendant for fetch descendant option: include all descendants.'() {
62         when: 'fetch descendant option with -1 depth'
63             def fetchDescendantsOption = new FetchDescendantsOption(-1)
64         then: 'the next level of depth is as expected'
65             fetchDescendantsOption.next().depth == -1
66     }
67
68     def 'Next descendant for fetch descendant option: omit descendants.'() {
69         given: 'fetch descendant option with 0 depth'
70             def fetchDescendantsOption = new FetchDescendantsOption(0)
71         when: 'the next level of depth is not allowed'
72             fetchDescendantsOption.next()
73         then: 'exception thrown'
74             thrown IllegalArgumentException
75     }
76
77     def 'Create fetch descendant option with  descendant using #scenario.'() {
78         when: 'the next level of depth is not allowed'
79            def FetchDescendantsOption fetchDescendantsOption = FetchDescendantsOption.getFetchDescendantsOption(fetchDescendantsOptionAsString)
80         then: 'fetch descendant object created'
81             assert fetchDescendantsOption.depth == expectedDepth
82         where: 'following parameters are used'
83             scenario                            | fetchDescendantsOptionAsString || expectedDepth
84             'all descendants using number'      | '-1'                           || -1
85             'all descendants using all'         | 'all'                          || -1
86             'No descendants by default'         | ''                             || 0
87             'No descendants using none'         | 'none'                         || 0
88             'direct child using number'         | '1'                            || 1
89             'direct child using direct'         | 'direct'                       || 1
90             'til 10th descendants using number' | '10'                           || 10
91     }
92
93     def 'String values.'() {
94         expect: 'each fetch descendant option has the correct String value'
95             assert fetchDescendantsOption.toString() == expectedStringValue
96         where: 'the following option is used'
97             fetchDescendantsOption                         || expectedStringValue
98             FetchDescendantsOption.OMIT_DESCENDANTS        || 'OmitDescendants'
99             FetchDescendantsOption.DIRECT_CHILD_ONLY       || 'DirectChildOnly'
100             FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS || 'IncludeAllDescendants'
101             new FetchDescendantsOption(2)                  || 'Depth=2'
102     }
103
104 }