diff options
author | Ruslan Kashapov <ruslan.kashapov@pantheon.tech> | 2021-01-19 17:57:00 +0200 |
---|---|---|
committer | Ruslan Kashapov <ruslan.kashapov@pantheon.tech> | 2021-01-21 09:41:37 +0200 |
commit | 2006f67db860e755b599acda36cfc85a49d8c8f3 (patch) | |
tree | 0e548b2124df1d7f1c8121f3d6375c3eba2faaf8 /cps-rest/src/test/groovy | |
parent | dce4e316da66ede214977491c712b0a4ab457644 (diff) |
ZIP archive support for multiple YANG files delivery on Schema Set creation using REST
Issue-ID: CPS-180
Change-Id: I7e78a595593b170b981746e9aed1a7e5a45b202a
Signed-off-by: Ruslan Kashapov <ruslan.kashapov@pantheon.tech>
Diffstat (limited to 'cps-rest/src/test/groovy')
-rw-r--r-- | cps-rest/src/test/groovy/org/onap/cps/rest/controller/AdminRestControllerSpec.groovy | 69 | ||||
-rw-r--r-- | cps-rest/src/test/groovy/org/onap/cps/rest/utils/MultipartFileUtilSpec.groovy | 67 |
2 files changed, 118 insertions, 18 deletions
diff --git a/cps-rest/src/test/groovy/org/onap/cps/rest/controller/AdminRestControllerSpec.groovy b/cps-rest/src/test/groovy/org/onap/cps/rest/controller/AdminRestControllerSpec.groovy index a95d606a35..60f54bfa7b 100644 --- a/cps-rest/src/test/groovy/org/onap/cps/rest/controller/AdminRestControllerSpec.groovy +++ b/cps-rest/src/test/groovy/org/onap/cps/rest/controller/AdminRestControllerSpec.groovy @@ -37,6 +37,7 @@ import org.springframework.test.web.servlet.MockMvc import org.springframework.util.LinkedMultiValueMap import org.springframework.util.MultiValueMap import spock.lang.Specification +import spock.lang.Unroll import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete @@ -85,29 +86,66 @@ class AdminRestControllerSpec extends Specification { response.status == HttpStatus.BAD_REQUEST.value() } - def 'Create schema set from yang file'() { + def 'Create schema set from yang file.'() { def yangResourceMapCapture - given: + given: 'single yang file' def multipartFile = createMultipartFile("filename.yang", "content") - when: + when: 'file uploaded with schema set create request' def response = performCreateSchemaSetRequest(multipartFile) - then: 'Service method is invoked with expected parameters' + then: 'associated service method is invoked with expected parameters' 1 * mockCpsModuleService.createSchemaSet('test-dataspace', 'test-schema-set', _) >> { args -> yangResourceMapCapture = args[2] } yangResourceMapCapture['filename.yang'] == 'content' - and: 'Response code indicates success' + and: 'response code indicates success' response.status == HttpStatus.CREATED.value() } - def 'Create schema set from file with invalid filename extension'() { - given: + def 'Create schema set from zip archive.'() { + def yangResourceMapCapture + given: 'zip archive with multiple .yang files inside' + def multipartFile = createZipMultipartFileFromResource("/yang-files-set.zip") + when: 'file uploaded with schema set create request' + def response = performCreateSchemaSetRequest(multipartFile) + then: 'associated service method is invoked with expected parameters' + 1 * mockCpsModuleService.createSchemaSet('test-dataspace', 'test-schema-set', _) >> + { args -> yangResourceMapCapture = args[2] } + yangResourceMapCapture['assembly.yang'] == "fake assembly content 1\n" + yangResourceMapCapture['component.yang'] == "fake component content 1\n" + and: 'response code indicates success' + response.status == HttpStatus.CREATED.value() + } + + @Unroll + def 'Create schema set from zip archive having #caseDescriptor.'() { + when: 'zip archive having #caseDescriptor is uploaded with create schema set request' + def response = performCreateSchemaSetRequest(multipartFile) + then: 'create schema set rejected' + response.status == HttpStatus.BAD_REQUEST.value() + where: 'following cases are tested' + caseDescriptor | multipartFile + 'no .yang files inside' | createZipMultipartFileFromResource("/no-yang-files.zip") + 'multiple .yang files with same name' | createZipMultipartFileFromResource("/yang-files-multiple-sets.zip") + } + + def 'Create schema set from file with unsupported filename extension.'() { + given: 'file with unsupported filename extension (.doc)' def multipartFile = createMultipartFile("filename.doc", "content") - when: + when: 'file uploaded with schema set create request' def response = performCreateSchemaSetRequest(multipartFile) - then: 'Create schema fails' + then: 'create schema set rejected' response.status == HttpStatus.BAD_REQUEST.value() } + @Unroll + def 'Create schema set from #fileType file with IOException occurrence on processing.'() { + when: 'file uploaded with schema set create request' + def response = performCreateSchemaSetRequest(createMultipartFileForIOException(fileType)) + then: 'the error response returned indicating internal server error occurrence' + response.status == HttpStatus.INTERNAL_SERVER_ERROR.value() + where: 'following file types are used' + fileType << ['YANG', 'ZIP'] + } + def 'Delete schema set.'() { when: 'delete schema set endpoint is invoked' def response = performDeleteRequest(schemaSetEndpoint) @@ -138,6 +176,19 @@ class AdminRestControllerSpec extends Specification { return new MockMultipartFile("file", filename, "text/plain", content.getBytes()) } + def createZipMultipartFileFromResource(resourcePath) { + return new MockMultipartFile("file", "test.zip", "application/zip", + getClass().getResource(resourcePath).getBytes()) + } + + def createMultipartFileForIOException(extension) { + def multipartFile = Mock(MockMultipartFile) + multipartFile.getOriginalFilename() >> "TEST." + extension + multipartFile.getBytes() >> { throw new IOException() } + multipartFile.getInputStream() >> { throw new IOException() } + return multipartFile + } + def performCreateSchemaSetRequest(multipartFile) { return mvc.perform( multipart(schemaSetsEndpoint) diff --git a/cps-rest/src/test/groovy/org/onap/cps/rest/utils/MultipartFileUtilSpec.groovy b/cps-rest/src/test/groovy/org/onap/cps/rest/utils/MultipartFileUtilSpec.groovy index ba5aa4cac0..3e2bdec37e 100644 --- a/cps-rest/src/test/groovy/org/onap/cps/rest/utils/MultipartFileUtilSpec.groovy +++ b/cps-rest/src/test/groovy/org/onap/cps/rest/utils/MultipartFileUtilSpec.groovy @@ -19,30 +19,79 @@ package org.onap.cps.rest.utils +import org.onap.cps.spi.exceptions.CpsException import org.onap.cps.spi.exceptions.ModelValidationException 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 'Extract yang resource from multipart file'() { - given: + def 'Extract yang resource from yang file.'() { + given: 'uploaded yang file' def multipartFile = new MockMultipartFile("file", "filename.yang", "text/plain", "content".getBytes()) - when: + when: 'resources are extracted from the file' def result = MultipartFileUtil.extractYangResourcesMap(multipartFile) - then: - assert result != null + then: 'the expected name and content are extracted as result' assert result.size() == 1 assert result.get("filename.yang") == "content" } - def 'Extract yang resource from file with invalid filename extension'() { - given: + 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()) + 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' + assert result.size() == 2 + assert result["assembly.yang"] == "fake assembly content 1\n" + assert result["component.yang"] == "fake component content 1\n" + } + + @Unroll + def 'Extract resources from zip archive having #caseDescriptor.'() { + when: 'attempt to extract resources from zip file is performed' + MultipartFileUtil.extractYangResourcesMap(multipartFile) + then: 'the validation exception is thrown indicating invalid zip file content' + thrown(ModelValidationException) + where: 'following cases are tested' + caseDescriptor | multipartFile + 'text files only' | multipartZipFileFromResource("/no-yang-files.zip") + 'multiple yang file with same name' | multipartZipFileFromResource("/yang-files-multiple-sets.zip") + } + + def 'Extract yang resource from a file with invalid filename extension.'() { + given: 'uploaded file with unsupported (.doc) exception' def multipartFile = new MockMultipartFile("file", "filename.doc", "text/plain", "content".getBytes()) - when: + when: 'attempt to extract resources from the file is performed' MultipartFileUtil.extractYangResourcesMap(multipartFile) - then: + then: 'validation exception is thrown indicating the file type is not supported' 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)) + then: 'CpsException is thrown indicating the internal error occurrence' + thrown(CpsException) + where: 'following file types are used' + fileType << ['YANG', 'ZIP'] + } + + def multipartZipFileFromResource(resourcePath) { + return new MockMultipartFile("file", "TEST.ZIP", "application/zip", + getClass().getResource(resourcePath).getBytes()) + } + + def multipartFileForIOException(extension) { + def multipartFile = Mock(MultipartFile) + multipartFile.getOriginalFilename() >> "TEST." + extension + multipartFile.getBytes() >> { throw new IOException() } + multipartFile.getInputStream() >> { throw new IOException() } + return multipartFile + } + } |