Merge "Update write performance test timings"
[cps.git] / cps-rest / src / test / groovy / org / onap / cps / rest / utils / MultipartFileUtilSpec.groovy
index 2c3d46e..e9d559c 100644 (file)
@@ -1,6 +1,7 @@
 /*
  *  ============LICENSE_START=======================================================
  *  Copyright (C) 2020 Pantheon.tech
+ *  Modifications Copyright (C) 2023 Nordix Foundation.
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -21,24 +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
-import spock.lang.Unroll
 
 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())
@@ -52,7 +41,7 @@ class MultipartFileUtilSpec extends Specification {
     def 'Extract yang resources from zip archive.'() {
         given: 'uploaded zip archive containing 2 yang files and 1 not yang (json) file'
             def multipartFile = new MockMultipartFile("file", "TEST.ZIP", "application/zip",
-                    getClass().getResource("/yang-files-set.zip").getBytes())
+                getClass().getResource("/yang-files-set.zip").getBytes())
         when: 'resources are extracted from zip file'
             def result = MultipartFileUtil.extractYangResourcesMap(multipartFile)
         then: 'information from yang files is extracted, not yang file (json) is ignored'
@@ -61,7 +50,32 @@ class MultipartFileUtilSpec extends Specification {
             assert result["component.yang"] == "fake component content 1\n"
     }
 
-    @Unroll
+    def 'Yang file limits in zip archive: #scenario for the bug reported in CPS-1477'() {
+        given: 'a yang file size (uncompressed) limit of #threshold bytes'
+            ZipFileSizeValidator.thresholdSize = threshold
+        and: 'an archive with a yang file of 1083 bytes'
+            def multipartFile = multipartZipFileFromResource('/yang-files-set-total-1083-bytes.zip')
+        when: 'attempt to extract yang files'
+            def thrownException = null
+            try {
+                MultipartFileUtil.extractYangResourcesMap(multipartFile)
+            } catch (Exception e) {
+                thrownException  = e
+            }
+        then: 'ModelValidationException indicating size limit is only thrown when threshold exceeded'
+            if (thresholdExceeded) {
+                assert thrownException instanceof ModelValidationException
+                assert thrownException.details.contains('limit of ' + threshold + ' bytes')
+            } else {
+                assert thrownException == null
+            }
+        where:
+            scenario          | threshold || thresholdExceeded
+            'exceed limit'    | 1082      || true
+            'equals to limit' | 1083      || false
+            'within limit'    | 1084      || false
+    }
+
     def 'Extract resources from zip archive having #caseDescriptor.'() {
         when: 'attempt to extract resources from zip file is performed'
             MultipartFileUtil.extractYangResourcesMap(multipartFile)
@@ -82,7 +96,6 @@ class MultipartFileUtilSpec extends Specification {
             thrown(ModelValidationException)
     }
 
-    @Unroll
     def 'IOException thrown during yang resources extraction from #fileType file.'() {
         when: 'attempt to extract resources from the file is performed'
             MultipartFileUtil.extractYangResourcesMap(multipartFileForIOException(fileType))
@@ -92,9 +105,35 @@ 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())
+            getClass().getResource(resourcePath).getBytes())
     }
 
     def multipartFileForIOException(extension) {