1+ package uk .nhs .adaptors .gp2gp .common .storage ;
2+
3+ import org .junit .jupiter .api .BeforeEach ;
4+ import org .junit .jupiter .api .Test ;
5+ import org .mockito .Mock ;
6+ import org .mockito .MockitoAnnotations ;
7+ import software .amazon .awssdk .core .ResponseInputStream ;
8+ import software .amazon .awssdk .services .s3 .S3Client ;
9+ import software .amazon .awssdk .services .s3 .model .GetObjectRequest ;
10+ import software .amazon .awssdk .services .s3 .model .GetObjectResponse ;
11+ import java .io .ByteArrayInputStream ;
12+
13+ import static org .junit .Assert .assertEquals ;
14+ import static org .junit .Assert .assertThrows ;
15+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
16+ import static org .mockito .ArgumentMatchers .any ;
17+ import static org .mockito .Mockito .mock ;
18+ import static org .mockito .Mockito .verify ;
19+ import static org .mockito .Mockito .when ;
20+
21+ class S3StorageConnectorTest {
22+
23+ private static final String FILE_NAME = "test-file.txt" ;
24+ private S3StorageConnector s3StorageConnector ;
25+ private StorageConnectorConfiguration config ;
26+ @ Mock
27+ private S3Client mockS3Client ;
28+
29+ @ BeforeEach
30+ void setUp () {
31+ MockitoAnnotations .openMocks (this );
32+
33+ config = new StorageConnectorConfiguration ();
34+ config .setContainerName ("s3Bucket" );
35+
36+ s3StorageConnector = new S3StorageConnector (mockS3Client , config );
37+ }
38+
39+
40+ @ Test
41+ void expectExceptionWhenS3ClientCantDeliverResponse () {
42+ s3StorageConnector = new S3StorageConnector (S3Client .builder ().build (), config );
43+ Exception exception = assertThrows (StorageConnectorException .class ,
44+ () -> s3StorageConnector .downloadFromStorage ("s3File" ));
45+
46+ assertEquals ("Error occurred downloading from S3 Bucket" , exception .getMessage ());
47+ }
48+
49+ @ Test
50+ void downloadFromStorage () {
51+ var mockResponse = mock (GetObjectResponse .class );
52+ var mockInputStream = new ByteArrayInputStream ("dummy-content" .getBytes ());
53+ var mockResponseInputStream = new ResponseInputStream <>(mockResponse , mockInputStream );
54+
55+ when (mockS3Client .getObject (any (GetObjectRequest .class ))).thenReturn (mockResponseInputStream );
56+
57+ var result = s3StorageConnector .downloadFromStorage (FILE_NAME );
58+
59+ assertNotNull (result );
60+ verify (mockS3Client ).getObject (any (GetObjectRequest .class ));
61+ }
62+ }
0 commit comments