11package uk .nhs .adaptors .gp2gp .common .storage ;
22
3- import org .junit .jupiter .api .BeforeEach ;
3+ import io .findify .s3mock .S3Mock ;
4+ import org .junit .jupiter .api .BeforeAll ;
45import org .junit .jupiter .api .Test ;
5- import org . mockito . Mock ;
6- import org . mockito . MockitoAnnotations ;
6+ import software . amazon . awssdk . auth . credentials . AwsBasicCredentials ;
7+ import software . amazon . awssdk . auth . credentials . StaticCredentialsProvider ;
78import software .amazon .awssdk .core .ResponseInputStream ;
89import software .amazon .awssdk .core .sync .RequestBody ;
910import software .amazon .awssdk .regions .Region ;
1011import software .amazon .awssdk .services .s3 .S3Client ;
12+ import software .amazon .awssdk .services .s3 .S3Configuration ;
13+ import software .amazon .awssdk .services .s3 .model .CreateBucketRequest ;
1114import software .amazon .awssdk .services .s3 .model .GetObjectRequest ;
1215import software .amazon .awssdk .services .s3 .model .GetObjectResponse ;
1316import software .amazon .awssdk .services .s3 .model .PutObjectRequest ;
1417
1518import java .io .ByteArrayInputStream ;
19+ import java .io .IOException ;
1620import java .io .InputStream ;
21+ import java .net .URI ;
22+ import java .nio .charset .StandardCharsets ;
1723
1824import static org .junit .Assert .assertEquals ;
1925import static org .junit .Assert .assertThrows ;
2026import static org .junit .jupiter .api .Assertions .assertNotNull ;
21- import static org .mockito .ArgumentMatchers .any ;
22- import static org .mockito .ArgumentMatchers .eq ;
23- import static org .mockito .Mockito .mock ;
24- import static org .mockito .Mockito .times ;
25- import static org .mockito .Mockito .verify ;
26- import static org .mockito .Mockito .when ;
27+
2728
2829class S3StorageConnectorTest {
2930
31+ public static final int PORT = 9090 ;
32+ private static final String BUCKET_NAME = "s3bucket" ;
3033 private static final String FILE_NAME = "test-file.txt" ;
31- private S3StorageConnector s3StorageConnector ;
32- private StorageConnectorConfiguration config ;
33- private static final long STREAM_LENGTH = 100L ;
3434
35- @ Mock
36- private S3Client mockS3Client ;
35+ private static S3Mock s3Mock ;
36+ private static S3StorageConnector s3StorageConnector ;
37+ private static StorageConnectorConfiguration config ;
38+
39+ private static S3Client s3Client ;
40+
41+ @ BeforeAll
42+ static void setUp () {
3743
38- @ Mock
39- private InputStream is ;
44+ s3Mock = new S3Mock .Builder ().withPort (PORT ).withInMemoryBackend ().build ();
45+ s3Mock .start ();
46+ System .out .println ("S3Mock started at http://localhost:" + PORT );
4047
41- @ BeforeEach
42- void setUp () {
43- MockitoAnnotations .openMocks (this );
48+ s3Client = S3Client .builder ()
49+ .endpointOverride (URI .create ("http://localhost:" + PORT ))
50+ .credentialsProvider (StaticCredentialsProvider .create (
51+ AwsBasicCredentials .create ("accessKey" , "secretKey" )))
52+ .serviceConfiguration (S3Configuration .builder ().pathStyleAccessEnabled (true ).build ())
53+ .region (Region .EU_WEST_2 )
54+ .build ();
4455
4556 config = new StorageConnectorConfiguration ();
46- config .setContainerName ("s3Bucket" );
57+ config .setContainerName (BUCKET_NAME );
4758
48- s3StorageConnector = new S3StorageConnector (mockS3Client , config );
49- }
59+ s3Client .createBucket (CreateBucketRequest .builder ().bucket (BUCKET_NAME ).build ());
5060
61+ s3StorageConnector = new S3StorageConnector (s3Client , config );
62+ }
5163
5264 @ Test
53- void expectExceptionWhenS3ClientCannotDeliverResponse () {
54- S3StorageConnector storageConnector = new S3StorageConnector (S3Client .builder ().region (Region .EU_WEST_2 ).build (), config );
65+ void expectExceptionWhenFileDoesNotExist () {
5566 Exception exception = assertThrows (StorageConnectorException .class ,
56- () -> storageConnector .downloadFromStorage ("s3File " ));
67+ () -> s3StorageConnector .downloadFromStorage ("nonexistent-file.txt " ));
5768
5869 assertEquals ("Error occurred downloading from S3 Bucket" , exception .getMessage ());
5970 }
6071
6172 @ Test
62- void downloadFromStorageTest () {
63- var mockResponse = mock (GetObjectResponse .class );
64- var mockInputStream = new ByteArrayInputStream ("dummy-content" .getBytes ());
65- var mockResponseInputStream = new ResponseInputStream <>(mockResponse , mockInputStream );
66- final var request = GetObjectRequest .builder ().bucket (config .getContainerName ()).key (FILE_NAME ).build ();
73+ void downloadFromStorageTest () throws IOException {
6774
68- when (mockS3Client .getObject (request )).thenReturn (mockResponseInputStream );
75+ String fileContent = "dummy-content" ;
76+ s3Client .putObject (
77+ PutObjectRequest .builder ().bucket (BUCKET_NAME ).key (FILE_NAME ).build (),
78+ RequestBody .fromString (fileContent ));
6979
70- var result = s3StorageConnector .downloadFromStorage (FILE_NAME );
80+ ResponseInputStream <GetObjectResponse > response = s3StorageConnector .downloadFromStorage (FILE_NAME );
81+ String downloadedContent = new String (response .readAllBytes (), StandardCharsets .UTF_8 );
7182
72- assertNotNull (result );
73- verify ( mockS3Client ). getObject ( request );
83+ assertNotNull (response );
84+ assertEquals ( fileContent , downloadedContent );
7485 }
7586
7687 @ Test
77- void uploadToStorageTest () {
78- final var expectedRequest = PutObjectRequest .builder ().bucket (config .getContainerName ()).key (FILE_NAME ).build ();
88+ void uploadToStorageTest () throws IOException {
89+ String uploadContent = "upload-content" ;
90+ InputStream inputStream = new ByteArrayInputStream (uploadContent .getBytes (StandardCharsets .UTF_8 ));
91+ long streamLength = inputStream .available ();
92+
93+ s3StorageConnector .uploadToStorage (inputStream , streamLength , FILE_NAME );
7994
80- s3StorageConnector .uploadToStorage (is , STREAM_LENGTH , FILE_NAME );
95+ final var request = GetObjectRequest .builder ().bucket (BUCKET_NAME ).key (FILE_NAME ).build ();
96+ ResponseInputStream <GetObjectResponse > uploadedObjectInS3 = s3Client .getObject (request );
97+ String uploadedS3Content = new String (uploadedObjectInS3 .readAllBytes (), StandardCharsets .UTF_8 );
8198
82- verify ( mockS3Client , times ( 1 )). putObject ( eq ( expectedRequest ), any ( RequestBody . class ) );
99+ assertEquals ( uploadContent , uploadedS3Content );
83100 }
84101
85102}
0 commit comments