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