Fix Id-searches endpoint performance degradation
[cps.git] / cps-service / src / test / groovy / org / onap / cps / spi / FetchDescendantsOptionSpec.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.spi
22
23
24 import spock.lang.Specification
25
26 class FetchDescendantsOptionSpec extends Specification {
27     def 'Check has next descendant for fetch descendant option: #scenario'() {
28         when: 'fetch descendant option with #depth depth'
29             def fetchDescendantsOption = new FetchDescendantsOption(depth)
30         then: 'next level descendants available: #expectedHasNext'
31             fetchDescendantsOption.hasNext() == expectedHasNext
32         where: 'following parameters are used'
33             scenario                  | depth || expectedHasNext
34             'omit descendants'        | 0     || false
35             'first child'             | 1     || true
36             'second child'            | 2     || true
37             'include all descendants' | -1    || true
38     }
39
40     def 'Check has next descendant for fetch descendant option: invalid depth'() {
41         given: 'fetch descendant option with -2 depth'
42             def fetchDescendantsOption = new FetchDescendantsOption(-2)
43         when: 'next level descendants not available'
44             fetchDescendantsOption.hasNext()
45         then: 'exception thrown'
46             thrown IllegalArgumentException
47     }
48
49     def 'Get next descendant for fetch descendant option: #scenario'() {
50         when: 'fetch descendant option with #depth depth'
51             def fetchDescendantsOption = new FetchDescendantsOption(depth)
52         then: 'the next level of depth is as expected'
53             fetchDescendantsOption.next().depth == depth - 1
54         where: 'following parameters are used'
55             scenario                  | depth
56             'first child'             | 1
57             'second child'            | 2
58     }
59
60     def 'Get next descendant for fetch descendant option: include all descendants'() {
61         when: 'fetch descendant option with -1 depth'
62             def fetchDescendantsOption = new FetchDescendantsOption(-1)
63         then: 'the next level of depth is as expected'
64             fetchDescendantsOption.next().depth == -1
65     }
66
67     def 'Get next descendant for fetch descendant option: omit descendants'() {
68         given: 'fetch descendant option with 0 depth'
69             def fetchDescendantsOption = new FetchDescendantsOption(0)
70         when: 'the next level of depth is not allowed'
71             fetchDescendantsOption.next()
72         then: 'exception thrown'
73             thrown IllegalArgumentException
74     }
75 }