11package uk .nhs .adaptors .gp2gp .common .storage ;
22
3- import org .junit .jupiter .api .BeforeEach ;
3+ import com .adobe .testing .s3mock .junit5 .S3MockExtension ;
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 org .junit .jupiter .api .extension .RegisterExtension ;
77import software .amazon .awssdk .core .ResponseInputStream ;
88import software .amazon .awssdk .core .sync .RequestBody ;
9- import software .amazon .awssdk .regions .Region ;
109import software .amazon .awssdk .services .s3 .S3Client ;
11- import software .amazon .awssdk .services .s3 .model .GetObjectRequest ;
10+ import software .amazon .awssdk .services .s3 .model .CreateBucketRequest ;
1211import software .amazon .awssdk .services .s3 .model .GetObjectResponse ;
12+ import software .amazon .awssdk .services .s3 .model .HeadObjectRequest ;
13+ import software .amazon .awssdk .services .s3 .model .HeadObjectResponse ;
1314import software .amazon .awssdk .services .s3 .model .PutObjectRequest ;
1415
1516import java .io .ByteArrayInputStream ;
17+ import java .io .IOException ;
1618import java .io .InputStream ;
19+ import java .nio .charset .StandardCharsets ;
20+ import java .util .Optional ;
1721
1822import static org .junit .Assert .assertEquals ;
1923import static org .junit .Assert .assertThrows ;
2024import 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 ;
25+
2726
2827class S3StorageConnectorTest {
2928
29+ public static final int PORT = 9090 ;
30+ private static final String BUCKET_NAME = "s3bucket" ;
3031 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 ;
3432
35- @ Mock
36- private S3Client mockS3Client ;
33+ @ RegisterExtension
34+ static final S3MockExtension S3_MOCK = S3MockExtension .builder ().withSecureConnection (false ).build ();
35+
36+ private static S3StorageConnector s3StorageConnector ;
37+ private static StorageConnectorConfiguration config ;
38+
39+ private static S3Client s3Client ;
3740
38- @ Mock
39- private InputStream is ;
41+ @ BeforeAll
42+ static void setUp () {
4043
41- @ BeforeEach
42- void setUp () {
43- MockitoAnnotations .openMocks (this );
44+ s3Client = S3_MOCK .createS3ClientV2 ();
4445
4546 config = new StorageConnectorConfiguration ();
46- config .setContainerName ("s3Bucket" );
47+ config .setContainerName (BUCKET_NAME );
4748
48- s3StorageConnector = new S3StorageConnector (mockS3Client , config );
49- }
49+ s3Client .createBucket (CreateBucketRequest .builder ().bucket (BUCKET_NAME ).build ());
5050
51+ s3StorageConnector = new S3StorageConnector (s3Client , config );
52+ }
5153
5254 @ Test
53- void expectExceptionWhenS3ClientCannotDeliverResponse () {
54- S3StorageConnector storageConnector = new S3StorageConnector (S3Client .builder ().region (Region .EU_WEST_2 ).build (), config );
55+ void expectExceptionWhenFileDoesNotExist () {
5556 Exception exception = assertThrows (StorageConnectorException .class ,
56- () -> storageConnector .downloadFromStorage ("s3File " ));
57+ () -> s3StorageConnector .downloadFromStorage ("nonexistent-file.txt " ));
5758
5859 assertEquals ("Error occurred downloading from S3 Bucket" , exception .getMessage ());
5960 }
6061
6162 @ 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 ();
63+ void downloadFromStorageTest () throws IOException {
6764
68- when (mockS3Client .getObject (request )).thenReturn (mockResponseInputStream );
65+ String fileContent = "dummy-content" ;
66+ s3Client .putObject (
67+ PutObjectRequest .builder ().bucket (BUCKET_NAME ).key (FILE_NAME ).build (),
68+ RequestBody .fromString (fileContent ));
6969
70- var result = s3StorageConnector .downloadFromStorage (FILE_NAME );
70+ ResponseInputStream <GetObjectResponse > response = s3StorageConnector .downloadFromStorage (FILE_NAME );
71+ String downloadedContent = new String (response .readAllBytes (), StandardCharsets .UTF_8 );
7172
72- assertNotNull (result );
73- verify ( mockS3Client ). getObject ( request );
73+ assertNotNull (response );
74+ assertEquals ( fileContent , downloadedContent );
7475 }
7576
7677 @ Test
77- void uploadToStorageTest () {
78- final var expectedRequest = PutObjectRequest .builder ().bucket (config .getContainerName ()).key (FILE_NAME ).build ();
78+ void uploadToStorageTest () throws IOException {
79+ InputStream inputStream = new ByteArrayInputStream ("upload-content" .getBytes (StandardCharsets .UTF_8 ));
80+ long streamLength = inputStream .available ();
81+
82+ s3StorageConnector .uploadToStorage (inputStream , streamLength , FILE_NAME );
7983
80- s3StorageConnector . uploadToStorage ( is , STREAM_LENGTH , FILE_NAME );
84+ HeadObjectResponse response = s3Client . headObject ( HeadObjectRequest . builder (). bucket ( BUCKET_NAME ). key ( FILE_NAME ). build () );
8185
82- verify (mockS3Client , times (1 )).putObject (eq (expectedRequest ), any (RequestBody .class ));
86+ assertNotNull (response );
87+ assertEquals (Optional .of (streamLength ).get (), response .contentLength ());
8388 }
8489
8590}
0 commit comments