1+ package software .amazon .encryption .s3 .internal ;
2+
3+ import org .junit .jupiter .api .Test ;
4+ import org .mockito .ArgumentCaptor ;
5+ import software .amazon .awssdk .core .async .AsyncRequestBody ;
6+ import software .amazon .awssdk .core .sync .RequestBody ;
7+ import software .amazon .awssdk .services .s3 .S3AsyncClient ;
8+ import software .amazon .awssdk .services .s3 .S3Client ;
9+ import software .amazon .awssdk .services .s3 .model .PutObjectRequest ;
10+
11+ import java .util .concurrent .CompletableFuture ;
12+
13+ import static org .junit .jupiter .api .Assertions .assertEquals ;
14+ import static org .mockito .ArgumentMatchers .any ;
15+ import static org .mockito .Mockito .*;
16+
17+ class InstructionFileConfigUploadTest {
18+
19+ @ Test
20+ void uploadInstructionFileWithSetContentLengthSyncClient () {
21+ // Create a mock for the S3 client
22+ S3Client mockedS3Client = mock (S3Client .class );
23+ // The argument captor is used to capture the PutObjectRequest passed to the putObject method
24+ ArgumentCaptor <PutObjectRequest > instructionFilePutCaptor = ArgumentCaptor .forClass (PutObjectRequest .class );
25+
26+ // Create the InstructionFileConfig with the mocked S3 client
27+ InstructionFileConfig instructionFileConfig = InstructionFileConfig .builder ()
28+ .instructionFileClient (mockedS3Client )
29+ .enableInstructionFilePutObject (true )
30+ .build ();
31+
32+ // Build some data for the test
33+ PutObjectRequest putObjectRequest = PutObjectRequest .builder ()
34+ .key ("someKey" ).build ();
35+ String instructionFileContent = "some content that fakes an instruction file" ;
36+
37+ // call the actual method under test
38+ instructionFileConfig .putInstructionFile (putObjectRequest , instructionFileContent );
39+
40+ // Verify that the putObject method was called and the captured request has the correct content length
41+ verify (mockedS3Client ).putObject (instructionFilePutCaptor .capture (), any (RequestBody .class ));
42+ assertEquals (instructionFileContent .getBytes ().length , instructionFilePutCaptor .getValue ().contentLength ());
43+ }
44+
45+ @ Test
46+ void uploadInstructionFileWithSetContentLengthAsyncClient () {
47+ // Create a mock for the S3 client
48+ S3AsyncClient mockedS3Client = mock (S3AsyncClient .class );
49+ // The async putObject method returns a CompletableFuture, so we need to mock that behavior
50+ when (mockedS3Client .putObject (any (PutObjectRequest .class ), any (AsyncRequestBody .class )))
51+ .thenReturn (CompletableFuture .completedFuture (null ));
52+ // The argument captor is used to capture the PutObjectRequest passed to the putObject method
53+ ArgumentCaptor <PutObjectRequest > instructionFilePutCaptor = ArgumentCaptor .forClass (PutObjectRequest .class );
54+
55+ // Create the InstructionFileConfig with the mocked S3 async client
56+ InstructionFileConfig instructionFileConfig = InstructionFileConfig .builder ()
57+ .instructionFileAsyncClient (mockedS3Client )
58+ .enableInstructionFilePutObject (true )
59+ .build ();
60+
61+ // Build some data for the test
62+ PutObjectRequest putObjectRequest = PutObjectRequest .builder ()
63+ .key ("someKey" ).build ();
64+ String instructionFileContent = "some content that fakes an instruction file" ;
65+
66+ // call the actual method under test
67+ instructionFileConfig .putInstructionFile (putObjectRequest , instructionFileContent );
68+
69+ // Verify that the putObject method was called and the captured request has the correct content length
70+ verify (mockedS3Client ).putObject (instructionFilePutCaptor .capture (), any (AsyncRequestBody .class ));
71+ assertEquals (instructionFileContent .getBytes ().length , instructionFilePutCaptor .getValue ().contentLength ());
72+ }
73+ }
0 commit comments