Skip to content

Commit

Permalink
Modified test cases. #6
Browse files Browse the repository at this point in the history
  • Loading branch information
ankit-agrawal11 committed Feb 13, 2017
1 parent 0ccaa98 commit 70309f6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/main/groovy/com/causecode/fileuploader/BaseTestSetup.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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
}
}
22 changes: 18 additions & 4 deletions src/test/groovy/com/causecode/fileuploader/FileGroupSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand All @@ -582,23 +588,31 @@ class FileUploaderServiceSpec extends BaseFileUploaderServiceSpecSetup {
mockGetFileNameAndExtensions()
mockUploadFileMethod(true)
mockGetProviderInstance('google')
3 * fileGroupMock.cdnProvider >> {
5 * fileGroupMock.cdnProvider >> {
return
} >> {
return CDNProvider.GOOGLE
}

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()
e.message == 'Provider not defined in the Config. Please define one.'

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'
Expand All @@ -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')
Expand All @@ -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
Expand Down

0 comments on commit 70309f6

Please sign in to comment.