Merge "Use constants for magic numbers in perf tests"
[cps.git] / cps-service / src / test / groovy / org / onap / cps / spi / FetchDescendantsOptionSpec.groovy
index c4d3dd8..28bf38f 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2022 Nordix Foundation
+ *  Copyright (C) 2022-2023 Nordix Foundation
  *  Modifications Copyright (C) 2023 TechMahindra Ltd.
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,11 +25,12 @@ import org.onap.cps.spi.exceptions.DataValidationException
 import spock.lang.Specification
 
 class FetchDescendantsOptionSpec extends Specification {
-    def 'Check has next descendant for fetch descendant option: #scenario'() {
+
+    def 'Has next descendant for fetch descendant option: #scenario'() {
         when: 'fetch descendant option with #depth depth'
             def fetchDescendantsOption = new FetchDescendantsOption(depth)
         then: 'next level descendants available: #expectedHasNext'
-            fetchDescendantsOption.hasNext() == expectedHasNext
+            assert fetchDescendantsOption.hasNext() == expectedHasNext
         where: 'following parameters are used'
             scenario                  | depth || expectedHasNext
             'omit descendants'        | 0     || false
@@ -38,7 +39,7 @@ class FetchDescendantsOptionSpec extends Specification {
             'include all descendants' | -1    || true
     }
 
-    def 'Check has next descendant for fetch descendant option: invalid depth'() {
+    def 'Has next descendant for fetch descendant option: invalid depth'() {
         given: 'fetch descendant option with -2 depth'
             def fetchDescendantsOption = new FetchDescendantsOption(-2)
         when: 'next level descendants not available'
@@ -47,7 +48,7 @@ class FetchDescendantsOptionSpec extends Specification {
             thrown IllegalArgumentException
     }
 
-    def 'Get next descendant for fetch descendant option: #scenario'() {
+    def 'Next descendant for fetch descendant option: #scenario.'() {
         when: 'fetch descendant option with #depth depth'
             def fetchDescendantsOption = new FetchDescendantsOption(depth)
         then: 'the next level of depth is as expected'
@@ -58,14 +59,14 @@ class FetchDescendantsOptionSpec extends Specification {
             'second child'            | 2
     }
 
-    def 'Get next descendant for fetch descendant option: include all descendants'() {
+    def 'Next descendant for fetch descendant option: include all descendants.'() {
         when: 'fetch descendant option with -1 depth'
             def fetchDescendantsOption = new FetchDescendantsOption(-1)
         then: 'the next level of depth is as expected'
             fetchDescendantsOption.next().depth == -1
     }
 
-    def 'Get next descendant for fetch descendant option: omit descendants'() {
+    def 'Next descendant for fetch descendant option: omit descendants.'() {
         given: 'fetch descendant option with 0 depth'
             def fetchDescendantsOption = new FetchDescendantsOption(0)
         when: 'the next level of depth is not allowed'
@@ -74,10 +75,10 @@ class FetchDescendantsOptionSpec extends Specification {
             thrown IllegalArgumentException
     }
 
-    def 'Create fetch descendant option with  descendant using #scenario'() {
-        when: 'the next level of depth is not allowed'
-           def FetchDescendantsOption fetchDescendantsOption = FetchDescendantsOption.getFetchDescendantsOption(fetchDescendantsOptionAsString)
-        then: 'fetch descendant object created'
+    def 'Create fetch descendant option from string scenario: #scenario.'() {
+        when: 'create fetch descendant option from string'
+           def fetchDescendantsOption = FetchDescendantsOption.getFetchDescendantsOption(fetchDescendantsOptionAsString)
+        then: 'fetch descendant object created with correct depth'
             assert fetchDescendantsOption.depth == expectedDepth
         where: 'following parameters are used'
             scenario                            | fetchDescendantsOptionAsString || expectedDepth
@@ -85,6 +86,29 @@ class FetchDescendantsOptionSpec extends Specification {
             'all descendants using all'         | 'all'                          || -1
             'No descendants by default'         | ''                             || 0
             'No descendants using none'         | 'none'                         || 0
+            'No descendants using number'       | '0'                            || 0
+            'direct child using number'         | '1'                            || 1
+            'direct child using direct'         | 'direct'                       || 1
             'til 10th descendants using number' | '10'                           || 10
     }
+
+    def 'Create fetch descendant option from string with invalid string.'() {
+        when: 'attempt to create fetch descendant option from invalid string'
+            FetchDescendantsOption.getFetchDescendantsOption('invalid-string')
+        then: 'a validation exception is thrown with the invalid string in the details'
+            def thrown = thrown(DataValidationException)
+            thrown.details.contains('invalid-string')
+    }
+
+    def 'Convert to string.'() {
+        expect: 'each fetch descendant option has the correct String value'
+            assert fetchDescendantsOption.toString() == expectedStringValue
+        where: 'the following option is used'
+            fetchDescendantsOption                         || expectedStringValue
+            FetchDescendantsOption.OMIT_DESCENDANTS        || 'OmitDescendants'
+            FetchDescendantsOption.DIRECT_CHILD_ONLY       || 'DirectChildOnly'
+            FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS || 'IncludeAllDescendants'
+            new FetchDescendantsOption(2)                  || 'Depth=2'
+    }
+
 }