Untitled
vas
plain_text
10 months ago
1.4 kB
5
Indexable
@Test
void testDownloadFile_Success() throws IOException {
    // Mock getSecrets to return necessary keys
    when(awsS3ClientBuilderService.getSecrets("bucket")).thenReturn(mockJsonNodeWithKeys());
    // Mock S3 client creation
    AmazonS3 mockS3Client = mock(AmazonS3.class);
    when(awsS3ClientBuilderService.createAwsS3Client(anyString(), anyString(), any())).thenReturn(mockS3Client);
    // Simulate non-empty bucket
    ListObjectsV2Result mockResult = mock(ListObjectsV2Result.class);
    when(mockResult.getObjectSummaries()).thenReturn(List.of(mock(S3ObjectSummary.class)));
    when(mockS3Client.listObjectsV2(anyString())).thenReturn(mockResult);
    // Mock S3Object and its InputStream
    S3Object mockS3Object = mock(S3Object.class);
    S3ObjectInputStream mockInputStream = new S3ObjectInputStream(new ByteArrayInputStream("file content".getBytes()), null);
    when(mockS3Object.getObjectContent()).thenReturn(mockInputStream);
    when(mockS3Client.getObject(any(GetObjectRequest.class))).thenReturn(mockS3Object);
    // Call the method
    byte[] result = fileMgmtService.downloadFile("bucket", "file.txt");
    // Assert the file content
    assertNotNull(result);
    assertEquals("file content", new String(result));
    // Verify AWS S3 client shutdown
    verify(awsS3ClientBuilderService).shutdown(mockS3Client);
}
Editor is loading...
Leave a Comment