Skip to content

Commit

Permalink
Removed metaClass mocks. #6
Browse files Browse the repository at this point in the history
  • Loading branch information
ankit-agrawal11 committed Feb 10, 2017
1 parent 41fe543 commit e36693c
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 128 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ buildscript {
}
}

version "3.0.3"
version "3.0.4"
group "com.causecode.plugins"

apply plugin: "idea"
Expand Down
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package com.causecode.fileuploader
import com.causecode.fileuploader.cdn.amazon.AmazonCDNFileUploaderImpl
import com.causecode.fileuploader.cdn.google.GoogleCDNFileUploaderImpl
import com.causecode.fileuploader.cdn.google.GoogleCredentials
import org.apache.commons.fileupload.disk.DiskFileItem
import spock.lang.Specification

/**
Expand All @@ -19,43 +18,35 @@ import spock.lang.Specification
*/
class BaseFileUploaderServiceSpecSetup extends Specification implements BaseTestSetup {

void setup() {
GoogleCredentials.metaClass.getStorage = { ->
return
}
AmazonCDNFileUploaderImpl amazonCDNFileUploaderInstance

AmazonCDNFileUploaderImpl.metaClass.close = { ->
return true
}
GoogleCDNFileUploaderImpl googleCDNFileUploaderImplMock
AmazonCDNFileUploaderImpl amazonCDNFileUploaderImplMock
GoogleCredentials googleCredentialsMock
FileGroup fileGroupMock

GoogleCDNFileUploaderImpl.metaClass.close = { ->
return true
}
void setup() {
amazonCDNFileUploaderInstance = new AmazonCDNFileUploaderImpl()
googleCDNFileUploaderImplMock = GroovyMock(GoogleCDNFileUploaderImpl, global: true)
amazonCDNFileUploaderImplMock = GroovyMock(AmazonCDNFileUploaderImpl, global: true)
googleCredentialsMock = GroovyMock(GoogleCredentials, global: true)
fileGroupMock = GroovyMock(FileGroup, global: true)

Closure getTemporaryURL = { String containerName, String fileName, long expiration ->
return 'http://fixedURL.com'
googleCredentialsMock.storage >> {
return
}

Closure uploadFile = { String containerName, File file, String fileName, boolean makePublic, long maxAge ->
return true
amazonCDNFileUploaderImplMock.close() >> {
return
}

AmazonCDNFileUploaderImpl.metaClass.getTemporaryURL = getTemporaryURL
GoogleCDNFileUploaderImpl.metaClass.getTemporaryURL = getTemporaryURL

AmazonCDNFileUploaderImpl.metaClass.uploadFile = uploadFile
GoogleCDNFileUploaderImpl.metaClass.uploadFile = uploadFile
}

DiskFileItem getFileItem(File fileInstance) {
DiskFileItem fileItem = new DiskFileItem('file', 'text/plain', false, fileInstance.name,
(int) fileInstance.length() , fileInstance.parentFile)
fileItem.outputStream
return fileItem
googleCredentialsMock.close() >> {
return
}
}

void mockGetFileNameAndExtensions() {
FileGroup.metaClass.getFileNameAndExtensions = { def file, String customFileName ->
fileGroupMock.getFileNameAndExtensions(_, _) >> {
return [fileName: 'test.txt', fileExtension: 'txt', customFileName: 'unit-test', empty: false,
fileSize: 38L]
}
Expand All @@ -67,18 +58,48 @@ class BaseFileUploaderServiceSpecSetup extends Specification implements BaseTest
}
}

void mockFileDeleteMethod(boolean boolResult) {
File.metaClass.delete = {
return boolResult
}
}

boolean mockAuthenticateMethod() {
AmazonCDNFileUploaderImpl.metaClass.authenticate = {
new AmazonCDNFileUploaderImpl() >> amazonCDNFileUploaderImplMock
amazonCDNFileUploaderImplMock.authenticate() >> {
return true
}
}

void mockGetPermanentURL() {
Closure getPermanentURL = { String containerName, String fileName ->
return 'http://fixedURL.com'
amazonCDNFileUploaderImplMock.getPermanentURL(_, _) >> 'http://fixedURL.com'
googleCDNFileUploaderImplMock.getPermanentURL(_, _) >> 'http://fixedURL.com'
}

void mockGetTemporaryURL() {
amazonCDNFileUploaderImplMock.getTemporaryURL(_, _, _) >> 'http://fixedURL.com'
googleCDNFileUploaderImplMock.getTemporaryURL(_, _, _) >> 'http://fixedURL.com'
amazonCDNFileUploaderImplMock.getPermanentURL(_, _) >> 'http://fixedURL.com'
googleCDNFileUploaderImplMock.getPermanentURL(_, _) >> 'http://fixedURL.com'
}

void mockUploadFileMethod(boolean value) {
amazonCDNFileUploaderImplMock.uploadFile(_, _, _, _, _) >> value
googleCDNFileUploaderImplMock.uploadFile(_, _, _, _, _) >> value
}

void mockGetProviderInstance(String provider) {
service.metaClass.getProviderInstance = { String providerName ->
provider == 'google' ? googleCDNFileUploaderImplMock : amazonCDNFileUploaderImplMock
}
}

void mockFileGroupConstructor(String storageTypes) {
new FileGroup(_) >> { String group ->
fileGroupMock.groupName = group
fileGroupMock.groupConfig >> [storageTypes: storageTypes]

AmazonCDNFileUploaderImpl.metaClass.getPermanentURL = getPermanentURL
GoogleCDNFileUploaderImpl.metaClass.getPermanentURL = getPermanentURL
return fileGroupMock
}
}
}
43 changes: 43 additions & 0 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 spock.lang.Specification

/**
Expand Down Expand Up @@ -124,4 +126,45 @@ class FileGroupSpec extends Specification implements BaseTestSetup {
StorageConfigurationException e = thrown()
e.message == 'file too big'
}

void "test getFileNameAndExtensions method when file belongs to CommonsMultipartFile class"() {
given: 'Instances of CommonsMultipartFile and FileGroup class'
File fileInstance = getFileInstance('./temp/test.txt')
DiskFileItem fileItem = getDiskFileItemInstance(fileInstance)
CommonsMultipartFile commonsMultipartFileInstance = new CommonsMultipartFile(fileItem)

FileGroup fileGroupInstance = new FileGroup('testLocal')

when: 'getFileNameAndExtensions method is called'
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

cleanup:
fileInstance.delete()
}

void "test getCdnProvider method to return provider name"() {
given: 'An instance of FileGroup class'
FileGroup fileGroupInstance = new FileGroup('testGoogle')

expect: 'Method to return correct CDn provider for this group'
fileGroupInstance.cdnProvider == CDNProvider.GOOGLE
}

void "test getLocalSystemPath method to return localPath"() {
given: 'An instance of FileGroup class'
FileGroup fileGroupInstance = new FileGroup('testLocal')
Map fileProperties = [fileName: 'test', fileExtension: 'txt']

when: 'getLocalSystemPath method is called'
String localPath = fileGroupInstance.getLocalSystemPath('', fileProperties, 0L)

then: 'Method returns a valid local path'
localPath == './temp/0/test.txt'
}
}
Loading

0 comments on commit e36693c

Please sign in to comment.