1313import software .amazon .awssdk .core .sync .RequestBody ;
1414import software .amazon .awssdk .http .SdkHttpClient ;
1515import software .amazon .awssdk .http .apache .ApacheHttpClient ;
16+ import software .amazon .awssdk .metrics .MetricCollection ;
17+ import software .amazon .awssdk .metrics .MetricPublisher ;
1618import software .amazon .awssdk .services .s3 .S3Client ;
1719import software .amazon .awssdk .services .s3 .model .AbortMultipartUploadRequest ;
1820import software .amazon .awssdk .services .s3 .model .CompleteMultipartUploadRequest ;
@@ -99,7 +101,7 @@ public void testExecuteSingleUpload() throws IOException {
99101 }
100102
101103 final int bufferSize = randomIntBetween (1024 , 2048 );
102- final Long blobSize = randomLongBetween (0 , bufferSize );
104+ final int blobSize = randomIntBetween (0 , bufferSize );
103105
104106 final S3BlobStore blobStore = mock (S3BlobStore .class );
105107 when (blobStore .bucket ()).thenReturn (bucketName );
@@ -120,28 +122,36 @@ public void testExecuteSingleUpload() throws IOException {
120122
121123 final S3Client client = configureMockClient (blobStore );
122124
123- final ArgumentCaptor <PutObjectRequest > argumentCaptor = ArgumentCaptor .forClass (PutObjectRequest .class );
124- final ArgumentCaptor <RequestBody > argumentCaptorBody = ArgumentCaptor .forClass (RequestBody .class ); // TODO NOMERGE: test anything?
125+ final ArgumentCaptor <PutObjectRequest > requestCaptor = ArgumentCaptor .forClass (PutObjectRequest .class );
126+ final ArgumentCaptor <RequestBody > bodyCaptor = ArgumentCaptor .forClass (RequestBody .class );
125127
126- when (client .putObject (argumentCaptor .capture (), argumentCaptorBody .capture ())).thenReturn (PutObjectResponse .builder ().build ());
128+ when (client .putObject (requestCaptor .capture (), bodyCaptor .capture ())).thenReturn (PutObjectResponse .builder ().build ());
127129
128- final ByteArrayInputStream inputStream = new ByteArrayInputStream (new byte [blobSize . intValue () ]);
130+ final ByteArrayInputStream inputStream = new ByteArrayInputStream (new byte [blobSize ]);
129131 blobContainer .executeSingleUpload (randomPurpose (), blobStore , blobName , inputStream , blobSize );
130132
131- final PutObjectRequest request = argumentCaptor .getValue ();
133+ final PutObjectRequest request = requestCaptor .getValue ();
132134 assertEquals (bucketName , request .bucket ());
133135 assertEquals (blobPath .buildAsString () + blobName , request .key ());
134- // TODO NOMERGE: no more input stream access in the request object -- understand test and verify this is OK
135- // assertEquals(inputStream, request.getInputStream());
136- assertEquals (blobSize , request .contentLength ());
137- assertEquals (storageClass .toString (), request .storageClass ());
136+
137+ assertEquals (Long .valueOf (blobSize ), request .contentLength ());
138+ assertEquals (storageClass , request .storageClass ());
138139 assertEquals (cannedAccessControlList , request .acl ());
139140 if (serverSideEncryption ) {
140141 assertEquals (
141142 PutObjectRequest .builder ().serverSideEncryption ("AES256" ).build ().sseCustomerAlgorithm (),
142143 request .sseCustomerAlgorithm ()
143144 );
144145 }
146+
147+ final RequestBody requestBody = bodyCaptor .getValue ();
148+ try (var contentStream = requestBody .contentStreamProvider ().newStream ()) {
149+ assertEquals (inputStream .available (), blobSize );
150+ // checking that reading from contentStream also reads from inputStream
151+ final int toSkip = between (0 , blobSize );
152+ contentStream .skipNBytes (toSkip );
153+ assertEquals (inputStream .available (), blobSize - toSkip );
154+ }
145155 }
146156
147157 public void testExecuteMultipartUploadBlobSizeTooLarge () {
@@ -391,6 +401,13 @@ private static S3Client configureMockClient(S3BlobStore blobStore) {
391401 clientReference .mustIncRef ();
392402 return clientReference ;
393403 });
404+ when (blobStore .getMetricPublisher (any (), any ())).thenReturn (new MetricPublisher () {
405+ @ Override
406+ public void publish (MetricCollection metricCollection ) {}
407+
408+ @ Override
409+ public void close () {}
410+ });
394411 }
395412 return client ;
396413 }
0 commit comments