diff --git a/src/main/groovy/com/causecode/fileuploader/BaseTestSetup.groovy b/src/main/groovy/com/causecode/fileuploader/BaseTestSetup.groovy index 237215b..064e0ed 100644 --- a/src/main/groovy/com/causecode/fileuploader/BaseTestSetup.groovy +++ b/src/main/groovy/com/causecode/fileuploader/BaseTestSetup.groovy @@ -7,6 +7,8 @@ */ package com.causecode.fileuploader +import org.apache.commons.fileupload.disk.DiskFileItem + /** * This class contains common setup that can be used in unit, functional and integration test cases. * @@ -22,4 +24,12 @@ trait BaseTestSetup { file.createNewFile() file << 'This is a test document.' } + + DiskFileItem getDiskFileItemInstance(File fileInstance) { + DiskFileItem fileItem = new DiskFileItem('file', 'text/plain', false, fileInstance.name, + (int) fileInstance.length() , fileInstance.parentFile) + fileItem.outputStream + + return fileItem + } } diff --git a/src/test/groovy/com/causecode/fileuploader/FileGroupSpec.groovy b/src/test/groovy/com/causecode/fileuploader/FileGroupSpec.groovy index 533f985..f50ab66 100644 --- a/src/test/groovy/com/causecode/fileuploader/FileGroupSpec.groovy +++ b/src/test/groovy/com/causecode/fileuploader/FileGroupSpec.groovy @@ -10,8 +10,10 @@ package com.causecode.fileuploader import grails.buildtestdata.mixin.Build import grails.test.mixin.TestMixin import grails.test.mixin.support.GrailsUnitTestMixin +import org.apache.commons.fileupload.disk.DiskFileItem import org.springframework.context.MessageSource import org.springframework.context.i18n.LocaleContextHolder +import org.springframework.web.multipart.commons.CommonsMultipartFile import org.springframework.web.multipart.MultipartFile import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile import spock.lang.Specification @@ -129,15 +131,27 @@ class FileGroupSpec extends Specification implements BaseTestSetup { e.message == 'file too big' } - void "test getFileNameAndExtensions method when file belongs to StandardMultipartFile class"() { - given: 'Instances of StandardMultipartFile and FileGroup class' + void "test getFileNameAndExtensions method when file belongs to MultipartFile"() { + given: 'Instances of StandardMultipartFile, CommonsMultipartFile and FileGroup class' File fileInstance = getFileInstance('./temp/test.txt') + DiskFileItem fileItem = getDiskFileItemInstance(fileInstance) + CommonsMultipartFile commonsMultipartFileInstance = new CommonsMultipartFile(fileItem) + MultipartFile standardMultipartFile = new StandardMultipartFile(Mock(Part), 'test.txt') FileGroup fileGroupInstance = new FileGroup('testLocal') - when: 'getFileNameAndExtensions method is called' - Map result = fileGroupInstance.getFileNameAndExtensions(standardMultipartFile, 'testLocal.txt') + when: 'getFileNameAndExtensions method is called for CommonsMultipartFile' + Map result = fileGroupInstance.getFileNameAndExtensions(commonsMultipartFileInstance, 'testLocal.txt') + + then: 'Method returns a valid map' + result.fileName == 'testLocal.txt' + result.customFileName == 'testLocal.txt' + result.empty == true + result.fileSize == 0L + + when: 'getFileNameAndExtensions method is called for StandardMultipartFile' + result = fileGroupInstance.getFileNameAndExtensions(standardMultipartFile, 'testLocal.txt') then: 'Method returns a valid map' result.fileName == 'testLocal.txt' diff --git a/src/test/groovy/com/causecode/fileuploader/FileUploaderServiceSpec.groovy b/src/test/groovy/com/causecode/fileuploader/FileUploaderServiceSpec.groovy index a95fd8f..fb2f1a4 100644 --- a/src/test/groovy/com/causecode/fileuploader/FileUploaderServiceSpec.groovy +++ b/src/test/groovy/com/causecode/fileuploader/FileUploaderServiceSpec.groovy @@ -12,10 +12,12 @@ import grails.test.mixin.TestFor import grails.test.runtime.DirtiesRuntime import grails.util.Holders import groovy.json.JsonBuilder +import org.apache.commons.fileupload.disk.DiskFileItem import org.apache.commons.validator.UrlValidator import org.grails.plugins.codecs.HTMLCodec import org.springframework.context.MessageSource import org.springframework.context.i18n.LocaleContextHolder +import org.springframework.web.multipart.commons.CommonsMultipartFile import org.springframework.web.multipart.MultipartFile import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile import spock.lang.Unroll @@ -571,8 +573,12 @@ class FileUploaderServiceSpec extends BaseFileUploaderServiceSpecSetup { } void "test saveFile method for various cases"() { - given: 'An instance of File' + given: 'Instances of CommonsMultipartFile and StandardMultipartFile' File fileInstance = getFileInstance('./temp/test.txt') + + DiskFileItem fileItem = getDiskFileItemInstance(fileInstance) + CommonsMultipartFile commonsMultipartFileInstance = new CommonsMultipartFile(fileItem) + MultipartFile standardMultipartFile = new StandardMultipartFile(Mock(Part), 'test.txt') and: 'Mocked methods' @@ -582,7 +588,7 @@ class FileUploaderServiceSpec extends BaseFileUploaderServiceSpecSetup { mockGetFileNameAndExtensions() mockUploadFileMethod(true) mockGetProviderInstance('google') - 3 * fileGroupMock.cdnProvider >> { + 5 * fileGroupMock.cdnProvider >> { return } >> { return CDNProvider.GOOGLE @@ -590,7 +596,7 @@ class FileUploaderServiceSpec extends BaseFileUploaderServiceSpecSetup { when: 'saveFile is called and provider is not specified' mockGetFileNameAndExtensions() - service.saveFile('testGoogle', standardMultipartFile, 'test') + service.saveFile('testGoogle', commonsMultipartFileInstance, 'test') then: 'Method should throw StorageConfigurationException' StorageConfigurationException e = thrown() @@ -598,7 +604,15 @@ class FileUploaderServiceSpec extends BaseFileUploaderServiceSpecSetup { when: 'saveFile method is hit' mockUploadFileMethod(true) - def result = service.saveFile('testGoogle', standardMultipartFile, 'test') + def result = service.saveFile('testGoogle', commonsMultipartFileInstance, 'test') + + then: 'Method should return instance of UFile' + result.fileGroup == 'testGoogle' + result.type == UFileType.CDN_PUBLIC + + when: 'saveFile method is hit and file belongs to StandardMultiartFile' + mockUploadFileMethod(true) + result = service.saveFile('testGoogle', standardMultipartFile, 'test') then: 'Method should return instance of UFile' result.fileGroup == 'testGoogle' @@ -611,7 +625,9 @@ class FileUploaderServiceSpec extends BaseFileUploaderServiceSpecSetup { void "test saveFile when provider is LOCAL"() { given: 'File instance' File fileInstance = getFileInstance('./temp/test.txt') - MultipartFile standardMultipartFile = new StandardMultipartFile(Mock(Part), 'test.txt') + + DiskFileItem fileItem = getDiskFileItemInstance(fileInstance) + CommonsMultipartFile commonsMultipartFileInstance = new CommonsMultipartFile(fileItem) and: 'Mocked methods' mockFileGroupConstructor('LOCAL') @@ -632,7 +648,7 @@ class FileUploaderServiceSpec extends BaseFileUploaderServiceSpecSetup { result.id != null when: 'saveFile method is called and error occurs while saving file' - result = service.saveFile('testLocal', standardMultipartFile, 'test') + result = service.saveFile('testLocal', commonsMultipartFileInstance, 'test') then: 'File would not be saved' result.id == null