Refactoring document and image management

This commit is contained in:
Vishwas Babu AJ
2013-06-03 10:33:01 +05:30
parent 9c1f9cd912
commit 15311f6425
51 changed files with 1204 additions and 1074 deletions
@@ -1,28 +1,28 @@
package org.mifosplatform.infrastructure.core.service;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mifosplatform.infrastructure.configuration.domain.ConfigurationDomainService;
import org.mifosplatform.infrastructure.configuration.domain.ExternalServicesDomainService;
import org.mifosplatform.infrastructure.configuration.service.ExternalServicesReadPlatformServiceImpl;
import org.mifosplatform.infrastructure.documentmanagement.contentrepository.ContentRepositoryFactory;
import org.mifosplatform.infrastructure.documentmanagement.contentrepository.FileSystemContentRepository;
import org.mifosplatform.infrastructure.documentmanagement.contentrepository.S3ContentRepository;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.context.ApplicationContext;
import static junit.framework.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@SuppressWarnings("unused")
@RunWith(MockitoJUnitRunner.class)
public class DocumentStoreFactoryTest {
/**
@Test
public void shouldReturnS3DocumentStoreObjectIfS3Enabled(){
public void shouldReturnS3DocumentStoreObjectIfS3Enabled() {
S3ClientFactory s3ClientFactoryMock = mock(S3ClientFactory.class);
when(s3ClientFactoryMock.getS3Client()).thenReturn(null);
ExternalServicesDomainService externalServicesDomainServiceMock = mock(ExternalServicesDomainService.class);
ExternalServicesReadPlatformServiceImpl externalServicesDomainServiceMock = mock(ExternalServicesReadPlatformServiceImpl.class);
when(externalServicesDomainServiceMock.getValue("s3_bucket_name")).thenReturn("bucket_name");
ConfigurationDomainService configurationDomainServiceMock = mock(ConfigurationDomainService.class);
@@ -30,26 +30,27 @@ public class DocumentStoreFactoryTest {
ApplicationContext applicationContextMock = mock(ApplicationContext.class);
when(applicationContextMock.getBean("configurationDomainServiceJpa", ConfigurationDomainService.class)).thenReturn(configurationDomainServiceMock);
when(applicationContextMock.getBean("configurationDomainServiceJpa", ConfigurationDomainService.class)).thenReturn(
configurationDomainServiceMock);
when(applicationContextMock.getBean("s3ClientFactory", S3ClientFactory.class)).thenReturn(s3ClientFactoryMock);
when(applicationContextMock.getBean("externalServicesDomainService", ExternalServicesDomainService.class)).thenReturn(externalServicesDomainServiceMock);
when(applicationContextMock.getBean("externalServicesDomainService", ExternalServicesReadPlatformServiceImpl.class)).thenReturn(
externalServicesDomainServiceMock);
DocumentStoreFactory documentStoreFactory = new DocumentStoreFactory(applicationContextMock);
assertEquals(documentStoreFactory.getInstanceFromConfiguration().getClass(),S3DocumentStore.class);
ContentRepositoryFactory documentStoreFactory = new ContentRepositoryFactory(applicationContextMock);
assertEquals(documentStoreFactory.getInstanceFromConfiguration().getClass(), S3ContentRepository.class);
}
@Test
public void shouldReturnFileSystemDocumentStoreObjectIfS3IsDisabled(){
public void shouldReturnFileSystemDocumentStoreObjectIfS3IsDisabled() {
ConfigurationDomainService configurationDomainService = mock(ConfigurationDomainService.class);
when(configurationDomainService.isAmazonS3Enabled()).thenReturn(false);
ApplicationContext applicationContext = mock(ApplicationContext.class);
when(applicationContext.getBean("configurationDomainServiceJpa", ConfigurationDomainService.class)).thenReturn(configurationDomainService);
when(applicationContext.getBean("configurationDomainServiceJpa", ConfigurationDomainService.class)).thenReturn(
configurationDomainService);
DocumentStoreFactory documentStoreFactory = new DocumentStoreFactory(applicationContext);
assertEquals(documentStoreFactory.getInstanceFromConfiguration().getClass(),FileSystemDocumentStore.class);
ContentRepositoryFactory documentStoreFactory = new ContentRepositoryFactory(applicationContext);
assertEquals(documentStoreFactory.getInstanceFromConfiguration().getClass(), FileSystemContentRepository.class);
}
**/
}
@@ -1,23 +1,22 @@
package org.mifosplatform.infrastructure.core.service;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mifosplatform.infrastructure.documentmanagement.contentrepository.FileSystemContentRepository;
import org.mifosplatform.infrastructure.documentmanagement.data.DocumentData;
import org.mifosplatform.infrastructure.documentmanagement.data.FileData;
import org.mockito.runners.MockitoJUnitRunner;
import static junit.framework.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@SuppressWarnings("unused")
@RunWith(MockitoJUnitRunner.class)
public class FileDocumentStoreTest {
/**
@Test
public void shouldRetrieveDocument(){
FileSystemDocumentStore fileSystemDocumentStore = new FileSystemDocumentStore();
public void shouldRetrieveDocument() {
FileSystemContentRepository fileSystemDocumentStore = new FileSystemContentRepository();
DocumentData documentDataMock = mock(DocumentData.class);
when(documentDataMock.fileName()).thenReturn("some_file");
when(documentDataMock.fileName()).thenReturn("content_type");
@@ -28,4 +27,5 @@ public class FileDocumentStoreTest {
assertEquals(fileData.name(), documentDataMock.fileName());
assertEquals(fileData.contentType(), documentDataMock.contentType());
}
**/
}
@@ -1,27 +1,34 @@
package org.mifosplatform.infrastructure.core.service;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mifosplatform.infrastructure.documentmanagement.command.DocumentCommand;
import org.mifosplatform.infrastructure.documentmanagement.data.DocumentData;
import org.mifosplatform.infrastructure.documentmanagement.data.FileData;
import org.mifosplatform.infrastructure.documentmanagement.exception.DocumentManagementException;
import org.mifosplatform.infrastructure.documentmanagement.exception.DocumentNotFoundException;
import org.mockito.Matchers;
import org.mockito.runners.MockitoJUnitRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.io.InputStream;
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mifosplatform.infrastructure.documentmanagement.command.DocumentCommand;
import org.mifosplatform.infrastructure.documentmanagement.contentrepository.S3ContentRepository;
import org.mifosplatform.infrastructure.documentmanagement.data.DocumentData;
import org.mifosplatform.infrastructure.documentmanagement.data.FileData;
import org.mifosplatform.infrastructure.documentmanagement.exception.ContentManagementException;
import org.mifosplatform.infrastructure.documentmanagement.exception.DocumentNotFoundException;
import org.mockito.Matchers;
import org.mockito.runners.MockitoJUnitRunner;
@SuppressWarnings("unused")
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.DeleteObjectRequest;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.PutObjectResult;
import com.amazonaws.services.s3.model.S3Object;
@RunWith(MockitoJUnitRunner.class)
public class S3DocumentStoreTest {
@@ -30,17 +37,17 @@ public class S3DocumentStoreTest {
AmazonS3Client s3ClientMock;
DocumentCommand documentCommand;
DocumentData documentDataMock;
/**
@Test
public void shouldGenerateParentDirectoryFromEntityDetails(){
S3DocumentStore s3DocumentStore=new S3DocumentStore("bucketName", null);
public void shouldGenerateParentDirectoryFromEntityDetails() {
S3ContentRepository s3DocumentStore = new S3ContentRepository("bucketName", null);
String actualParentDirectory = s3DocumentStore.generateFileParentDirectory("entity_type", 1234L);
assertTrue(actualParentDirectory.startsWith("documents/entity_type/1234"));
}
@Test
public void shouldGenerateClientImageDirectoryFromResourceId(){
S3DocumentStore s3DocumentStore=new S3DocumentStore("bucketName", null);
public void shouldGenerateClientImageDirectoryFromResourceId() {
S3ContentRepository s3DocumentStore = new S3ContentRepository("bucketName", null);
String actualParentDirectory = s3DocumentStore.generateClientImageParentDirectory(1234L);
assertTrue(actualParentDirectory.startsWith("images/clients/1234"));
}
@@ -48,20 +55,20 @@ public class S3DocumentStoreTest {
@Test
public void shouldSaveADocumentToS3() throws IOException {
initUpload();
when(s3ClientMock.putObject(Matchers.<PutObjectRequest>anyObject())).thenReturn(new PutObjectResult());
S3DocumentStore s3DocumentStore = new S3DocumentStore("bucketName", s3ClientMock);
when(s3ClientMock.putObject(Matchers.<PutObjectRequest> anyObject())).thenReturn(new PutObjectResult());
S3ContentRepository s3DocumentStore = new S3ContentRepository("bucketName", s3ClientMock);
s3DocumentStore.saveDocument(inputStream, documentCommand);
verify(s3ClientMock, times(1)).putObject(Matchers.<PutObjectRequest>anyObject());
verify(s3ClientMock, times(1)).putObject(Matchers.<PutObjectRequest> anyObject());
}
@Test(expected = DocumentManagementException.class)
public void shouldCatchAmazonServiceExceptionWhenUnableToUploadDocumentToS3() throws IOException {
initUpload();
when(s3ClientMock.putObject(Matchers.<PutObjectRequest>anyObject())).thenThrow(new AmazonServiceException("serviceException"));
when(s3ClientMock.putObject(Matchers.<PutObjectRequest> anyObject())).thenThrow(new AmazonServiceException("serviceException"));
S3DocumentStore s3DocumentStore = new S3DocumentStore("bucketName", s3ClientMock);
S3ContentRepository s3DocumentStore = new S3ContentRepository("bucketName", s3ClientMock);
s3DocumentStore.saveDocument(inputStream, documentCommand);
}
@@ -69,9 +76,9 @@ public class S3DocumentStoreTest {
@Test(expected = DocumentManagementException.class)
public void shouldCatchAmazonClientExceptionWhenUnableToUploadDocumentToS3() throws IOException {
initUpload();
when(s3ClientMock.putObject(Matchers.<PutObjectRequest>anyObject())).thenThrow(new AmazonClientException("clientException"));
when(s3ClientMock.putObject(Matchers.<PutObjectRequest> anyObject())).thenThrow(new AmazonClientException("clientException"));
S3DocumentStore s3DocumentStore = new S3DocumentStore("bucketName", s3ClientMock);
S3ContentRepository s3DocumentStore = new S3ContentRepository("bucketName", s3ClientMock);
s3DocumentStore.saveDocument(inputStream, documentCommand);
}
@@ -79,12 +86,12 @@ public class S3DocumentStoreTest {
@Test
public void shouldRetrieveDocumentFromS3() throws IOException {
initRetrieve();
when(s3ClientMock.getObject(Matchers.<GetObjectRequest>anyObject())).thenReturn(new S3Object());
S3DocumentStore s3DocumentStore = new S3DocumentStore("bucketName",s3ClientMock);
when(s3ClientMock.getObject(Matchers.<GetObjectRequest> anyObject())).thenReturn(new S3Object());
S3ContentRepository s3DocumentStore = new S3ContentRepository("bucketName", s3ClientMock);
FileData fileData = s3DocumentStore.retrieveDocument(documentDataMock);
verify(s3ClientMock, times(1)).getObject(Matchers.<GetObjectRequest>anyObject());
verify(s3ClientMock, times(1)).getObject(Matchers.<GetObjectRequest> anyObject());
assertEquals(fileData.name(), "name");
assertEquals(fileData.contentType(), "type");
}
@@ -92,8 +99,8 @@ public class S3DocumentStoreTest {
@Test(expected = DocumentNotFoundException.class)
public void shouldCatchAmazonServiceExceptionWhenUnableToRetrieveDocumentFromS3() throws IOException {
initRetrieve();
when(s3ClientMock.getObject(Matchers.<GetObjectRequest>anyObject())).thenThrow(new AmazonServiceException("serviceException"));
S3DocumentStore s3DocumentStore = new S3DocumentStore("bucketName",s3ClientMock);
when(s3ClientMock.getObject(Matchers.<GetObjectRequest> anyObject())).thenThrow(new AmazonServiceException("serviceException"));
S3ContentRepository s3DocumentStore = new S3ContentRepository("bucketName", s3ClientMock);
s3DocumentStore.retrieveDocument(documentDataMock);
}
@@ -101,8 +108,8 @@ public class S3DocumentStoreTest {
@Test(expected = DocumentNotFoundException.class)
public void shouldCatchAmazonClientExceptionWhenUnableToRetrieveDocumentFromS3() throws IOException {
initRetrieve();
when(s3ClientMock.getObject(Matchers.<GetObjectRequest>anyObject())).thenThrow(new AmazonClientException("clientException"));
S3DocumentStore s3DocumentStore = new S3DocumentStore("bucketName",s3ClientMock);
when(s3ClientMock.getObject(Matchers.<GetObjectRequest> anyObject())).thenThrow(new AmazonClientException("clientException"));
S3ContentRepository s3DocumentStore = new S3ContentRepository("bucketName", s3ClientMock);
s3DocumentStore.retrieveDocument(documentDataMock);
}
@@ -110,11 +117,11 @@ public class S3DocumentStoreTest {
@Test
public void shouldDeleteImageFromS3() throws IOException {
AmazonS3Client s3ClientMock = mock(AmazonS3Client.class);
S3DocumentStore s3DocumentStore = new S3DocumentStore("bucketName",s3ClientMock);
S3ContentRepository s3DocumentStore = new S3ContentRepository("bucketName", s3ClientMock);
s3DocumentStore.deleteImage(123L,"imagekey");
s3DocumentStore.deleteImage(123L, "imagekey");
verify(s3ClientMock, times(1)).deleteObject(Matchers.<DeleteObjectRequest>anyObject());
verify(s3ClientMock, times(1)).deleteObject(Matchers.<DeleteObjectRequest> anyObject());
}
private void initRetrieve() {
@@ -126,10 +133,10 @@ public class S3DocumentStoreTest {
when(documentDataMock.contentType()).thenReturn("type");
}
private void initUpload(){
private void initUpload() {
inputStream = S3DocumentStoreTest.class.getResourceAsStream("DummyString");
s3ClientMock = mock(AmazonS3Client.class);
documentCommand = new DocumentCommand(null, -1l, "parentEntityType", 1l, null, "someFileName", 3l, null, null, null);
documentCommand = new DocumentCommand(null, -1l, "parentEntityType", 1l, null, "someFileName", 3l, null, null, null);
}
**/
}