Set up Groovy & Spock Test Framework
authorToineSiebelink <toine.siebelink@est.tech>
Wed, 30 Sep 2020 15:11:55 +0000 (16:11 +0100)
committerToineSiebelink <toine.siebelink@est.tech>
Thu, 1 Oct 2020 07:41:06 +0000 (08:41 +0100)
Add basic first test (CpServiceImplSpec) for illustartion purposes

Issue-ID: CCSDK-2786
Link: https://jira.onap.org/browse/CCSDK-2786
Change-Id: Ibda81289fb5c9a57474f242e26847b8464dc6b59

cps/cps-service/pom.xml
cps/cps-service/src/test/groovy/org/onap/cps/api/impl/CpServiceImplSpec.groovy [new file with mode: 0644]
cps/pom.xml

index e0d7ebd..a59376c 100644 (file)
@@ -30,8 +30,9 @@
     </dependency>\r
 \r
     <dependency>\r
+      <!-- required for processing yang data in json format -->\r
       <groupId>org.opendaylight.yangtools</groupId>\r
-      <artifactId>yang-data-codec-xml</artifactId>\r
+      <artifactId>yang-data-codec-gson</artifactId>\r
       <version>${org.opendaylight.yangtools.version}</version>\r
     </dependency>\r
 \r
       <artifactId>gson</artifactId>\r
     </dependency>\r
 \r
+    <!-- T E S T   D E P E N D E N C I E S -->\r
+\r
+    <dependency>\r
+      <groupId>org.codehaus.groovy</groupId>\r
+      <artifactId>groovy</artifactId>\r
+      <version>${version.groovy}</version>\r
+      <scope>test</scope>\r
+    </dependency>\r
+    <dependency>\r
+      <groupId>org.spockframework</groupId>\r
+      <artifactId>spock-core</artifactId>\r
+      <version>${version.spock-core}</version>\r
+      <scope>test</scope>\r
+    </dependency>\r
+    <dependency>\r
+      <groupId>cglib</groupId>\r
+      <artifactId>cglib-nodep</artifactId>\r
+      <version>3.1</version>\r
+      <scope>test</scope>\r
+    </dependency>\r
   </dependencies>\r
 \r
-</project>
\ No newline at end of file
+</project>\r
diff --git a/cps/cps-service/src/test/groovy/org/onap/cps/api/impl/CpServiceImplSpec.groovy b/cps/cps-service/src/test/groovy/org/onap/cps/api/impl/CpServiceImplSpec.groovy
new file mode 100644 (file)
index 0000000..27f7482
--- /dev/null
@@ -0,0 +1,27 @@
+package org.onap.cps.api.impl
+
+import org.onap.cps.spi.DataPersistencyService
+import spock.lang.Specification;
+
+
+class CpServiceImplSpec extends Specification {
+
+    def dataPersistencyService = Mock(DataPersistencyService)
+    def objectUnderTest = new CpServiceImpl()
+
+    def setup() {
+        // Insert mocked dependencies
+        objectUnderTest.dataPersistencyService = dataPersistencyService;
+    }
+
+    def 'Storing a json object'() {
+        given: 'that the data persistency service returns an id of 123'
+            dataPersistencyService.storeJsonStructure(_) >> 123
+
+        when: 'a json structure is stored using the data persistency service'
+            def result = objectUnderTest.storeJsonStructure('')
+
+        then: ' the same id is returned'
+            result == 123
+    }
+}
index 7f50cb1..5435c4a 100644 (file)
@@ -23,6 +23,8 @@
     <springboot.version>2.3.3.RELEASE</springboot.version>\r
     <oparent.version>3.1.0</oparent.version>\r
     <org.opendaylight.yangtools.version>5.0.5</org.opendaylight.yangtools.version>\r
+    <version.groovy>3.0.6</version.groovy>\r
+    <version.spock-core>2.0-M2-groovy-3.0</version.spock-core>\r
   </properties>\r
 \r
   <dependencyManagement>\r
         </dependencies>\r
 \r
       </plugin>\r
+\r
+      <!-- Mandatory plugins for using Spock -->\r
+      <plugin>\r
+        <!-- The gmavenplus plugin is used to compile Groovy code. To learn more about this plugin,\r
+        visit https://github.com/groovy/GMavenPlus/wiki -->\r
+        <groupId>org.codehaus.gmavenplus</groupId>\r
+        <artifactId>gmavenplus-plugin</artifactId>\r
+        <version>1.9.0</version>\r
+        <executions>\r
+          <execution>\r
+            <goals>\r
+              <goal>compileTests</goal>\r
+            </goals>\r
+          </execution>\r
+        </executions>\r
+      </plugin>\r
+      <!-- Required because names of spec classes don't match default Surefire patterns (`*Test` etc.) -->\r
+      <plugin>\r
+        <groupId>org.apache.maven.plugins</groupId>\r
+        <artifactId>maven-surefire-plugin</artifactId>\r
+        <version>3.0.0-M5</version>\r
+        <configuration>\r
+          <useFile>false</useFile>\r
+          <includes>\r
+            <include>**/*Spec.java</include>\r
+            <include>**/*Test.java</include> <!-- Just in case of having also "normal" JUnit tests -->\r
+          </includes>\r
+        </configuration>\r
+      </plugin>\r
+\r
     </plugins>\r
   </build>\r
 \r