-
Notifications
You must be signed in to change notification settings - Fork 6
Migrate from v1 to v2 of AWS SDK for Java #849
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
Changes from all commits
31c6457
6da9d1e
e977062
798de72
49ecd27
74d85fa
be87fec
3b8b387
fe24287
cfd35bb
5a72e5e
fe5ea1e
93112f1
ad83526
10c95ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package uk.nhs.adaptors.gp2gp.common.configuration; | ||
|
|
||
| import io.findify.s3mock.S3Mock; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
| import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; | ||
| import software.amazon.awssdk.regions.Region; | ||
| import software.amazon.awssdk.services.s3.S3Client; | ||
| import software.amazon.awssdk.services.s3.S3Configuration; | ||
| import software.amazon.awssdk.services.s3.model.CreateBucketRequest; | ||
| import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
| import java.io.File; | ||
| import java.lang.reflect.Field; | ||
| import java.net.URI; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
|
||
| class CustomTrustStoreTest { | ||
|
|
||
| public static final int PORT = 8001; | ||
| private static S3Mock s3Mock; | ||
| private static S3Client s3Client; | ||
| private static final String BUCKET_NAME = "test-bucket"; | ||
| private static final String TRUSTSTORE_PATH = "test.jks"; | ||
| private static final String TRUSTSTORE_PASSWORD = "password"; | ||
|
|
||
| private final CustomTrustStore customTrustStore = new CustomTrustStore(); | ||
|
|
||
| @BeforeAll | ||
| static void setUp() { | ||
| s3Mock = new S3Mock.Builder().withPort(PORT).withInMemoryBackend().build(); | ||
| s3Mock.start(); | ||
| System.out.println("S3Mock started at http://localhost:" + PORT); | ||
|
|
||
| s3Client = S3Client.builder() | ||
| .endpointOverride(URI.create("http://localhost:" + PORT)) | ||
| .credentialsProvider(StaticCredentialsProvider.create( | ||
| AwsBasicCredentials.create("accessKey", "secretKey"))) | ||
| .serviceConfiguration(S3Configuration.builder().pathStyleAccessEnabled(true).build()) | ||
| .region(Region.EU_WEST_2) | ||
| .build(); | ||
|
|
||
| s3Client.createBucket(CreateBucketRequest.builder().bucket(BUCKET_NAME).build()); | ||
|
|
||
| File trustStoreFile = new File("src/test/resources/test.jks"); | ||
| s3Client.putObject(PutObjectRequest.builder().bucket(BUCKET_NAME).key(TRUSTSTORE_PATH).build(), | ||
| software.amazon.awssdk.core.sync.RequestBody.fromFile(trustStoreFile)); | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void tearDown() { | ||
| s3Mock.shutdown(); | ||
| } | ||
|
|
||
| @Test | ||
| void trustManagerLoadsSuccessfullyTest() throws NoSuchFieldException, IllegalAccessException { | ||
|
|
||
| Field s3ClientField = CustomTrustStore.class.getDeclaredField("s3Client"); | ||
| s3ClientField.setAccessible(true); | ||
| s3ClientField.set(customTrustStore, s3Client); | ||
|
Comment on lines
+59
to
+61
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally not a fan of messing around with reflection to create tests. Any way to make the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking about it, I'd probably make the
|
||
|
|
||
| String s3Uri = "s3://" + BUCKET_NAME + "/" + TRUSTSTORE_PATH; | ||
|
|
||
| var trustManager = customTrustStore.getCustomDbTrustManager(s3Client.utilities().parseUri(URI.create(s3Uri)), TRUSTSTORE_PASSWORD); | ||
|
|
||
| assertNotNull(trustManager, "Custom TrustManager wasn't loaded successfully!"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package uk.nhs.adaptors.gp2gp.common.storage; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.Mock; | ||
| import org.mockito.MockitoAnnotations; | ||
| import software.amazon.awssdk.core.ResponseInputStream; | ||
| import software.amazon.awssdk.core.sync.RequestBody; | ||
| import software.amazon.awssdk.regions.Region; | ||
| import software.amazon.awssdk.services.s3.S3Client; | ||
| import software.amazon.awssdk.services.s3.model.GetObjectRequest; | ||
| import software.amazon.awssdk.services.s3.model.GetObjectResponse; | ||
| import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.InputStream; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| class S3StorageConnectorTest { | ||
|
|
||
| private static final String FILE_NAME = "test-file.txt"; | ||
| private S3StorageConnector s3StorageConnector; | ||
| private StorageConnectorConfiguration config; | ||
| private static final long STREAM_LENGTH = 100L; | ||
|
|
||
| @Mock | ||
| private S3Client mockS3Client; | ||
|
|
||
| @Mock | ||
| private InputStream is; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| MockitoAnnotations.openMocks(this); | ||
|
|
||
| config = new StorageConnectorConfiguration(); | ||
| config.setContainerName("s3Bucket"); | ||
|
|
||
| s3StorageConnector = new S3StorageConnector(mockS3Client, config); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| void expectExceptionWhenS3ClientCannotDeliverResponse() { | ||
| S3StorageConnector storageConnector = new S3StorageConnector(S3Client.builder().region(Region.EU_WEST_2).build(), config); | ||
| Exception exception = assertThrows(StorageConnectorException.class, | ||
| () -> storageConnector.downloadFromStorage("s3File")); | ||
|
|
||
| assertEquals("Error occurred downloading from S3 Bucket", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void downloadFromStorageTest() { | ||
| var mockResponse = mock(GetObjectResponse.class); | ||
| var mockInputStream = new ByteArrayInputStream("dummy-content".getBytes()); | ||
| var mockResponseInputStream = new ResponseInputStream<>(mockResponse, mockInputStream); | ||
| final var request = GetObjectRequest.builder().bucket(config.getContainerName()).key(FILE_NAME).build(); | ||
|
|
||
| when(mockS3Client.getObject(request)).thenReturn(mockResponseInputStream); | ||
|
|
||
| var result = s3StorageConnector.downloadFromStorage(FILE_NAME); | ||
|
|
||
| assertNotNull(result); | ||
| verify(mockS3Client).getObject(request); | ||
| } | ||
|
|
||
| @Test | ||
| void uploadToStorageTest() { | ||
| final var expectedRequest = PutObjectRequest.builder().bucket(config.getContainerName()).key(FILE_NAME).build(); | ||
|
|
||
| s3StorageConnector.uploadToStorage(is, STREAM_LENGTH, FILE_NAME); | ||
|
|
||
| verify(mockS3Client, times(1)).putObject(eq(expectedRequest), any(RequestBody.class)); | ||
| } | ||
|
|
||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this use the Bean defined asgetS3Clientinside ofStorageConnectorConfiguration.java? Because if so, then that bean only gets constructed when a truststore is defined. Whereas previously this was being constructed all the time.It will come from the
StorageConnectorFactorywhich is fine.