Improved code coverage (branches) around multipart file utils 44/135244/1
authorToineSiebelink <toine.siebelink@est.tech>
Thu, 29 Jun 2023 09:42:23 +0000 (10:42 +0100)
committerToineSiebelink <toine.siebelink@est.tech>
Thu, 29 Jun 2023 09:47:38 +0000 (10:47 +0100)
- added test
- moved wrongly placed test (from MultipartFileUtilSpec to DataMapUtilsSpec)
- applied curent conventions to DataMapUtilsSpec

Issue-ID: CPS-475

Signed-off-by: ToineSiebelink <toine.siebelink@est.tech>
Change-Id: Ie8dc7f6802bb6ff0a256dc05df73da6af75409f3

cps-rest/src/test/groovy/org/onap/cps/rest/utils/MultipartFileUtilSpec.groovy
cps-service/src/test/groovy/org/onap/cps/utils/DataMapUtilsSpec.groovy

index 572db00..e9d559c 100644 (file)
@@ -22,23 +22,12 @@ package org.onap.cps.rest.utils
 
 import org.onap.cps.spi.exceptions.CpsException
 import org.onap.cps.spi.exceptions.ModelValidationException
-import org.onap.cps.spi.model.DataNodeBuilder
-import org.onap.cps.utils.DataMapUtils
 import org.springframework.mock.web.MockMultipartFile
 import org.springframework.web.multipart.MultipartFile
 import spock.lang.Specification
 
 class MultipartFileUtilSpec extends Specification {
 
-    def 'Data node without leaves and without children.'() {
-        given: 'a datanode with no leaves and no children'
-            def dataNodeWithoutData = new DataNodeBuilder().withXpath('some xpath').build()
-        when: 'it is converted to a map'
-            def result = DataMapUtils.toDataMap(dataNodeWithoutData)
-        then: 'an empty object map is returned'
-            result.isEmpty()
-    }
-
     def 'Extract yang resource from yang file.'() {
         given: 'uploaded yang file'
             def multipartFile = new MockMultipartFile("file", "filename.yang", "text/plain", "content".getBytes())
@@ -116,6 +105,32 @@ class MultipartFileUtilSpec extends Specification {
             fileType << ['YANG', 'ZIP']
     }
 
+    def 'Resource name extension checks, with #scenario.'() {
+        expect: 'extension check returns expected result'
+            assert MultipartFileUtil.resourceNameEndsWithExtension(resourceName, '.test') == expectedResult
+        where: 'following resource names are tested'
+            scenario           | resourceName  || expectedResult
+            'correct extension'| 'file.test'   || true
+            'mixed case'       | 'file.TesT'   || true
+            'other extension'  | 'file.other'  || false
+            'no extension'     | 'file'        || false
+            'null'             | null          || false
+    }
+
+    def 'Extract resourcename, with #scenario.'() {
+        expect: 'extension check returns expected result'
+            assert MultipartFileUtil.extractResourceNameFromPath(path) == expectedResoureName
+        where: 'following resource names are tested'
+            scenario           | path                || expectedResoureName
+            'no folder'        | 'file.test'         || 'file.test'
+            'single folder'    | 'folder/file.test'  || 'file.test'
+            'multiple folders' | 'f1/f2/file.test'   || 'file.test'
+            'with root'        | '/f1/f2/file.test'  || 'file.test'
+            'windows notation' | 'c:\\f2\\file.test' || 'file.test'
+            'empty path'       | ''                  || ''
+            'null path'        | null                || ''
+    }
+
     def multipartZipFileFromResource(resourcePath) {
         return new MockMultipartFile("file", "TEST.ZIP", "application/zip",
             getClass().getResource(resourcePath).getBytes())
index e27b437..c636f4b 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  ============LICENSE_START=======================================================
  *  Copyright (C) 2021 Pantheon.tech
- *  Modifications Copyright (C) 2020-2022 Nordix Foundation
+ *  Modifications Copyright (C) 2020-2023 Nordix Foundation
  *  Modifications Copyright (C) 2022 Bell Canada.
  *  Modifications Copyright (C) 2023 TechMahindra Ltd.
  *  ================================================================================
@@ -29,50 +29,19 @@ class DataMapUtilsSpec extends Specification {
 
     def noChildren = []
 
-    def dataNode = buildDataNode(
-            "/parent",[parentLeaf:'parentLeafValue', parentLeafList:['parentLeafListEntry1','parentLeafListEntry2']],[
-            buildDataNode('/parent/child-list[@id=1/2]',[listElementLeaf:'listElement1leafValue'],noChildren),
-            buildDataNode('/parent/child-list[@id=2]',[listElementLeaf:'listElement2leafValue'],noChildren),
-            buildDataNode('/parent/child-object',[childLeaf:'childLeafValue'],
-                    [buildDataNode('/parent/child-object/grand-child-object',[grandChildLeaf:'grandChildLeafValue'],noChildren)]
-            ),
-    ])
-
-    def dataNodeWithAnchor = buildDataNodeWithAnchor(
-            "/parent", 'anchor01',[parentLeaf:'parentLeafValue', parentLeafList:['parentLeafListEntry1','parentLeafListEntry2']],[
-            buildDataNode('/parent/child-list[@id=1/2]',[listElementLeaf:'listElement1leafValue'],noChildren),
-            buildDataNode('/parent/child-list[@id=2]',[listElementLeaf:'listElement2leafValue'],noChildren),
-            buildDataNode('/parent/child-object',[childLeaf:'childLeafValue'],
-                    [buildDataNode('/parent/child-object/grand-child-object',[grandChildLeaf:'grandChildLeafValue'],noChildren)]
-            ),
-    ])
-
-    static def buildDataNode(xpath,  leaves,  children) {
-        return new DataNodeBuilder().withXpath(xpath).withLeaves(leaves).withChildDataNodes(children).build()
-    }
-
-    static def buildDataNodeWithAnchor(xpath, anchorName, leaves,  children) {
-        return new DataNodeBuilder().withXpath(xpath).withAnchor(anchorName).withLeaves(leaves).withChildDataNodes(children).build()
-    }
-
     def 'Data node structure conversion to map.'() {
         when: 'data node structure is converted to a map'
             def result = DataMapUtils.toDataMap(dataNode)
-
         then: 'root node identifier is null'
             result.parent == null
-
         then: 'root node leaves are top level elements'
             result.parentLeaf == 'parentLeafValue'
             result.parentLeafList == ['parentLeafListEntry1','parentLeafListEntry2']
-
         and: 'leaves of child list element are listed as structures under common identifier'
             result.'child-list'.collect().containsAll(['listElementLeaf': 'listElement1leafValue'],
                                                       ['listElementLeaf': 'listElement2leafValue'])
-
         and: 'leaves for child element is populated under its node identifier'
             result.'child-object'.childLeaf == 'childLeafValue'
-
         and: 'leaves for grandchild element is populated under its node identifier'
             result.'child-object'.'grand-child-object'.grandChildLeaf == 'grandChildLeafValue'
     }
@@ -84,10 +53,8 @@ class DataMapUtilsSpec extends Specification {
             def parentNode = result.parent
             parentNode.parentLeaf == 'parentLeafValue'
             parentNode.parentLeafList == ['parentLeafListEntry1','parentLeafListEntry2']
-
         and: 'leaves for child element is populated under its node identifier'
             parentNode.'child-object'.childLeaf == 'childLeafValue'
-
         and: 'leaves for grandchild element is populated under its node identifier'
             parentNode.'child-object'.'grand-child-object'.grandChildLeaf == 'grandChildLeafValue'
     }
@@ -112,15 +79,48 @@ class DataMapUtilsSpec extends Specification {
             def parentNode = result.get("dataNode").parent
             parentNode.parentLeaf == 'parentLeafValue'
             parentNode.parentLeafList == ['parentLeafListEntry1','parentLeafListEntry2']
-
         and: 'leaves for child element is populated under its node identifier'
             assert parentNode.'child-object'.childLeaf == 'childLeafValue'
-
         and: 'leaves for grandchild element is populated under its node identifier'
             assert parentNode.'child-object'.'grand-child-object'.grandChildLeaf == 'grandChildLeafValue'
-
         and: 'data node is associated with anchor name'
             assert result.get('anchorName') == 'anchor01'
     }
+
+    def 'Data node without leaves and without children.'() {
+        given: 'a datanode with no leaves and no children'
+            def dataNodeWithoutData = new DataNodeBuilder().withXpath('some xpath').build()
+        when: 'it is converted to a map'
+            def result = DataMapUtils.toDataMap(dataNodeWithoutData)
+        then: 'an empty object map is returned'
+            result.isEmpty()
+    }
+
+    def dataNode = buildDataNode(
+        "/parent",[parentLeaf:'parentLeafValue', parentLeafList:['parentLeafListEntry1','parentLeafListEntry2']],[
+        buildDataNode('/parent/child-list[@id=1/2]',[listElementLeaf:'listElement1leafValue'],noChildren),
+        buildDataNode('/parent/child-list[@id=2]',[listElementLeaf:'listElement2leafValue'],noChildren),
+        buildDataNode('/parent/child-object',[childLeaf:'childLeafValue'],
+            [buildDataNode('/parent/child-object/grand-child-object',[grandChildLeaf:'grandChildLeafValue'],noChildren)]
+        ),
+    ])
+
+    def dataNodeWithAnchor = buildDataNodeWithAnchor(
+        "/parent", 'anchor01',[parentLeaf:'parentLeafValue', parentLeafList:['parentLeafListEntry1','parentLeafListEntry2']],[
+        buildDataNode('/parent/child-list[@id=1/2]',[listElementLeaf:'listElement1leafValue'],noChildren),
+        buildDataNode('/parent/child-list[@id=2]',[listElementLeaf:'listElement2leafValue'],noChildren),
+        buildDataNode('/parent/child-object',[childLeaf:'childLeafValue'],
+            [buildDataNode('/parent/child-object/grand-child-object',[grandChildLeaf:'grandChildLeafValue'],noChildren)]
+        ),
+    ])
+
+    def buildDataNode(xpath,  leaves,  children) {
+        return new DataNodeBuilder().withXpath(xpath).withLeaves(leaves).withChildDataNodes(children).build()
+    }
+
+    def buildDataNodeWithAnchor(xpath, anchorName, leaves,  children) {
+        return new DataNodeBuilder().withXpath(xpath).withAnchor(anchorName).withLeaves(leaves).withChildDataNodes(children).build()
+    }
+
 }