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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.spi
24 import org.onap.cps.spi.exceptions.DataValidationException
25 import spock.lang.Specification
27 class FetchDescendantsOptionSpec extends Specification {
29 def 'Has next descendant for fetch descendant option: #scenario'() {
30 when: 'fetch descendant option with #depth depth'
31 def fetchDescendantsOption = new FetchDescendantsOption(depth)
32 then: 'next level descendants available: #expectedHasNext'
33 assert fetchDescendantsOption.hasNext() == expectedHasNext
34 where: 'following parameters are used'
35 scenario | depth || expectedHasNext
36 'omit descendants' | 0 || false
37 'first child' | 1 || true
38 'second child' | 2 || true
39 'include all descendants' | -1 || true
42 def 'Has next descendant for fetch descendant option: invalid depth'() {
43 given: 'fetch descendant option with -2 depth'
44 def fetchDescendantsOption = new FetchDescendantsOption(-2)
45 when: 'next level descendants not available'
46 fetchDescendantsOption.hasNext()
47 then: 'exception thrown'
48 thrown IllegalArgumentException
51 def 'Next descendant for fetch descendant option: #scenario.'() {
52 when: 'fetch descendant option with #depth depth'
53 def fetchDescendantsOption = new FetchDescendantsOption(depth)
54 then: 'the next level of depth is as expected'
55 fetchDescendantsOption.next().depth == depth - 1
56 where: 'following parameters are used'
62 def 'Next descendant for fetch descendant option: include all descendants.'() {
63 when: 'fetch descendant option with -1 depth'
64 def fetchDescendantsOption = new FetchDescendantsOption(-1)
65 then: 'the next level of depth is as expected'
66 fetchDescendantsOption.next().depth == -1
69 def 'Next descendant for fetch descendant option: omit descendants.'() {
70 given: 'fetch descendant option with 0 depth'
71 def fetchDescendantsOption = new FetchDescendantsOption(0)
72 when: 'the next level of depth is not allowed'
73 fetchDescendantsOption.next()
74 then: 'exception thrown'
75 thrown IllegalArgumentException
78 def 'Create fetch descendant option from string scenario: #scenario.'() {
79 when: 'create fetch descendant option from string'
80 def fetchDescendantsOption = FetchDescendantsOption.getFetchDescendantsOption(fetchDescendantsOptionAsString)
81 then: 'fetch descendant object created with correct depth'
82 assert fetchDescendantsOption.depth == expectedDepth
83 where: 'following parameters are used'
84 scenario | fetchDescendantsOptionAsString || expectedDepth
85 'all descendants using number' | '-1' || -1
86 'all descendants using all' | 'all' || -1
87 'No descendants by default' | '' || 0
88 'No descendants using none' | 'none' || 0
89 'No descendants using number' | '0' || 0
90 'direct child using number' | '1' || 1
91 'direct child using direct' | 'direct' || 1
92 'til 10th descendants using number' | '10' || 10
95 def 'Create fetch descendant option from string with invalid string.'() {
96 when: 'attempt to create fetch descendant option from invalid string'
97 FetchDescendantsOption.getFetchDescendantsOption('invalid-string')
98 then: 'a validation exception is thrown with the invalid string in the details'
99 def thrown = thrown(DataValidationException)
100 thrown.details.contains('invalid-string')
103 def 'Convert to string.'() {
104 expect: 'each fetch descendant option has the correct String value'
105 assert fetchDescendantsOption.toString() == expectedStringValue
106 where: 'the following option is used'
107 fetchDescendantsOption || expectedStringValue
108 FetchDescendantsOption.OMIT_DESCENDANTS || 'OmitDescendants'
109 FetchDescendantsOption.DIRECT_CHILD_ONLY || 'DirectChildOnly'
110 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS || 'IncludeAllDescendants'
111 new FetchDescendantsOption(2) || 'Depth=2'