Skip to content

Instantly share code, notes, and snippets.

@jaypatel512
Created November 1, 2018 18:40
Show Gist options
  • Save jaypatel512/adba77ba0ab3171d5e906740603a2f64 to your computer and use it in GitHub Desktop.
Save jaypatel512/adba77ba0ab3171d5e906740603a2f64 to your computer and use it in GitHub Desktop.
package com.blinkai.server.service;
import static com.amazonaws.services.s3.model.CannedAccessControlList.PublicRead;
import static com.blinkai.server.util.Constants.S3_BUCKET_NAME;
import static com.blinkai.server.util.StringUtils.isEmpty;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.transfer.MultipleFileDownload;
import com.amazonaws.services.s3.transfer.TransferManager;
import java.io.File;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class AWSServiceTest {
@Mock
private AmazonS3 client;
@Mock
private TransferManager transferManager;
@Mock
private MultipleFileDownload multipleFileDownload;
@Spy
private AWSService awsService;
@BeforeMethod
public void init() {
MockitoAnnotations.initMocks(this);
doReturn(client).when(awsService).getClient();
doReturn(transferManager).when(awsService).getTransferManager();
doReturn(multipleFileDownload).when(transferManager).downloadDirectory(any(), any(), any());
}
// @Test
public void uploadImage_CallsPutObjectWithPublicReadAccess() {
awsService.secretKey = null;
awsService.accessKey = null;
assertNotNull(awsService);
String result = awsService.uploadImage("/tmp/abc.jpg", "profile/abc.jpg");
assertNotNull(result);
assertTrue(!isEmpty(result));
ArgumentCaptor<PutObjectRequest> putObjectRequestArgumentCaptor = ArgumentCaptor
.forClass(PutObjectRequest.class);
verify(client, times(1)).putObject(putObjectRequestArgumentCaptor.capture());
PutObjectRequest request = putObjectRequestArgumentCaptor.getValue();
assertNotNull(request);
assertEquals(request.getCannedAcl(), PublicRead);
assertEquals(request.getBucketName(), S3_BUCKET_NAME);
assertEquals(request.getFile().getPath(), "/tmp/abc.jpg");
assertEquals(request.getKey(), "profile/abc.jpg");
}
// @Test
public void uploadImage_CallsPutObjectWithSetContentType() {
awsService.secretKey = null;
awsService.accessKey = null;
assertNotNull(awsService);
String result = awsService.uploadImage("/tmp/abc.jpg", "profile/abc.jpg", "application/xml");
assertNotNull(result);
assertTrue(!isEmpty(result));
ArgumentCaptor<PutObjectRequest> putObjectRequestArgumentCaptor = ArgumentCaptor
.forClass(PutObjectRequest.class);
verify(client, times(1)).putObject(putObjectRequestArgumentCaptor.capture());
PutObjectRequest request = putObjectRequestArgumentCaptor.getValue();
assertNotNull(request);
assertEquals(request.getMetadata().getContentType(), "application/xml");
assertEquals(request.getCannedAcl(), PublicRead);
}
// @Test
public void uploadImage_CallsPutObjectWithoutSettingContentType() {
awsService.secretKey = null;
awsService.accessKey = null;
assertNotNull(awsService);
String result = awsService.uploadImage("/tmp/abc.jpg", "profile/abc.jpg", null);
assertNotNull(result);
assertTrue(!isEmpty(result));
ArgumentCaptor<PutObjectRequest> putObjectRequestArgumentCaptor = ArgumentCaptor
.forClass(PutObjectRequest.class);
verify(client, times(1)).putObject(putObjectRequestArgumentCaptor.capture());
PutObjectRequest request = putObjectRequestArgumentCaptor.getValue();
assertNotNull(request);
assertNull(request.getMetadata().getContentType());
assertEquals(request.getCannedAcl(), PublicRead);
}
@Test
public void downloadDirectory_passesCorrectArgumentsToDownloadPath() {
awsService.secretKey = null;
awsService.accessKey = null;
assertNotNull(awsService);
File destination = new File("/tmp");
boolean result = awsService.downloadDirectory("store/abc123/pass/", destination);
assertTrue(result);
verify(transferManager, times(1))
.downloadDirectory(eq(S3_BUCKET_NAME), eq("store/abc123/pass/"), eq(destination));
}
@Test
public void downloadDirectory_returnsFalseIfInterruptionException() throws InterruptedException {
awsService.secretKey = null;
awsService.accessKey = null;
assertNotNull(awsService);
File destination = new File("/tmp");
doThrow(new InterruptedException("Test Interruption thrown")).when(multipleFileDownload)
.waitForCompletion();
boolean result = awsService.downloadDirectory("store/abc123/pass/", destination);
assertFalse(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment