Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cover AwsS3BuildCacheService#load with unit tests #27

Merged
merged 1 commit into from
Oct 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@

package ch.myniva.gradle.caching.s3.internal;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
import com.amazonaws.services.s3.model.StorageClass;
import org.gradle.caching.BuildCacheEntryReader;
import org.gradle.caching.BuildCacheEntryWriter;
import org.gradle.caching.BuildCacheKey;
import org.junit.Before;
Expand All @@ -39,6 +44,8 @@ public class AwsS3BuildCacheServiceTest {
@Mock
BuildCacheKey key;
@Mock
BuildCacheEntryReader reader;
@Mock
BuildCacheEntryWriter writer;
@Mock
PutObjectRequest putObjectRequest = mock(PutObjectRequest.class);
Expand All @@ -51,6 +58,37 @@ public void setup() {
doReturn("abcdefghijkl123456789").when(key).getHashCode();
}

@Test
public void loadGetsObjectsAndReturnsTrueIfItExistsInS3() throws Exception {
/** Setup **/
buildCacheService = new AwsS3BuildCacheService(s3, "bucketName", null, true);
doReturn(true).when(s3).doesObjectExist("bucketName", "abcdefghijkl123456789");
S3Object s3Object = mock(S3Object.class);
doReturn(s3Object).when(s3).getObject("bucketName", "abcdefghijkl123456789");
S3ObjectInputStream s3ObjectInputStream = mock(S3ObjectInputStream.class);
doReturn(s3ObjectInputStream).when(s3Object).getObjectContent();

/** Run **/
boolean result = buildCacheService.load(key, reader);

/** Check **/
assertTrue(result);
verify(reader).readFrom(s3ObjectInputStream);
}

@Test
public void loadReturnsFalseIfItDoesntExistInS3() throws Exception {
/** Setup **/
buildCacheService = new AwsS3BuildCacheService(s3, "bucketName", null, true);
doReturn(false).when(s3).doesObjectExist("bucketName", "abcdefghijkl123456789");

/** Run **/
boolean result = buildCacheService.load(key, reader);

/** Check **/
assertFalse(result);
}

@Test
public void storePutsObjectAndUsesReducedRedundancyWhenConfigured() throws IOException {
/** Setup **/
Expand Down