34
34
import org .assertj .core .util .Lists ;
35
35
import org .junit .jupiter .api .Test ;
36
36
import org .junit .jupiter .params .ParameterizedTest ;
37
- import org .junit .jupiter .params .provider .Arguments ;
38
37
import org .junit .jupiter .params .provider .MethodSource ;
39
38
import org .reactivestreams .Publisher ;
40
39
import org .reactivestreams .Subscriber ;
41
40
import org .reactivestreams .Subscription ;
42
41
import software .amazon .awssdk .checksums .DefaultChecksumAlgorithm ;
43
42
import software .amazon .awssdk .core .async .AsyncRequestBody ;
44
- import software .amazon .awssdk .core .checksums .Algorithm ;
45
43
import software .amazon .awssdk .core .internal .util .Mimetype ;
46
44
import software .amazon .awssdk .http .async .SimpleSubscriber ;
47
45
import software .amazon .awssdk .utils .BinaryUtils ;
@@ -74,36 +72,36 @@ public class ChecksumCalculatingAsyncRequestBodyTest {
74
72
}
75
73
}
76
74
77
- private static Stream <Arguments > publishers () {
75
+ private static Stream <TestCase > contentPublishers () {
78
76
return Stream .of (
79
- Arguments . of ("RequestBody from string, test string" ,
80
- checksumPublisher (AsyncRequestBody .fromString (testString )),
81
- expectedTestString ),
82
- Arguments . of ("RequestBody from file, test string" ,
83
- checksumPublisher (AsyncRequestBody .fromFile (path )),
84
- expectedTestString ),
85
- Arguments . of ("RequestBody from buffer, 0 pos, test string" ,
86
- checksumPublisher (AsyncRequestBody .fromRemainingByteBuffer (posZeroByteBuffer (testString ))),
87
- expectedTestString ),
88
- Arguments . of ("RequestBody from buffer, random pos, test string" ,
89
- checksumPublisher (AsyncRequestBody .fromRemainingByteBufferUnsafe (nonPosZeroByteBuffer (testString ))),
90
- expectedTestString ),
91
- Arguments . of ("RequestBody from string, empty string" ,
92
- checksumPublisher (AsyncRequestBody .fromString (emptyString )),
93
- expectedEmptyString ),
77
+ new TestCase (). description ("RequestBody from string, test string" )
78
+ . requestBody (AsyncRequestBody .fromString (testString ))
79
+ . expectedBody ( expectedTestString ),
80
+ new TestCase (). description ("RequestBody from file, test string" )
81
+ . requestBody (AsyncRequestBody .fromFile (path ))
82
+ . expectedBody ( expectedTestString ),
83
+ new TestCase (). description ("RequestBody from buffer, 0 pos, test string" )
84
+ . requestBody (AsyncRequestBody .fromRemainingByteBuffer (posZeroByteBuffer (testString )))
85
+ . expectedBody ( expectedTestString ),
86
+ new TestCase (). description ("RequestBody from buffer, random pos, test string" )
87
+ . requestBody (AsyncRequestBody .fromRemainingByteBufferUnsafe (nonPosZeroByteBuffer (testString )))
88
+ . expectedBody ( expectedTestString ),
89
+ new TestCase (). description ("RequestBody from string, empty string" )
90
+ . requestBody (AsyncRequestBody .fromString (emptyString ))
91
+ . expectedBody ( expectedEmptyString ),
94
92
//Note: FileAsyncRequestBody with empty file does not call onNext, only onComplete()
95
- Arguments . of ("RequestBody from file, empty string" ,
96
- checksumPublisher (AsyncRequestBody .fromFile (pathToEmpty )),
97
- expectedEmptyString ),
98
- Arguments . of ("RequestBody from buffer, 0 pos, empty string" ,
99
- checksumPublisher (AsyncRequestBody .fromRemainingByteBuffer (posZeroByteBuffer (emptyString ))),
100
- expectedEmptyString ),
101
- Arguments . of ("RequestBody from string, random pos, empty string" ,
102
- checksumPublisher (AsyncRequestBody .fromRemainingByteBufferUnsafe (nonPosZeroByteBuffer (emptyString ))),
103
- expectedEmptyString ),
104
- Arguments . of ("EmptyBufferPublisher, test string" ,
105
- checksumPublisher (new EmptyBufferPublisher (testString )),
106
- expectedTestString ));
93
+ new TestCase (). description ("RequestBody from file, empty string" )
94
+ . requestBody (AsyncRequestBody .fromFile (pathToEmpty ))
95
+ . expectedBody ( expectedEmptyString ),
96
+ new TestCase (). description ("RequestBody from buffer, 0 pos, empty string" )
97
+ . requestBody (AsyncRequestBody .fromRemainingByteBuffer (posZeroByteBuffer (emptyString )))
98
+ . expectedBody ( expectedEmptyString ),
99
+ new TestCase (). description ("RequestBody from string, random pos, empty string" )
100
+ . requestBody (AsyncRequestBody .fromRemainingByteBufferUnsafe (nonPosZeroByteBuffer (emptyString )))
101
+ . expectedBody ( expectedEmptyString ),
102
+ new TestCase (). description ("EmptyBufferPublisher, test string" )
103
+ . requestBody (new EmptyBufferPublisher (testString ))
104
+ . expectedBody ( expectedTestString ));
107
105
}
108
106
109
107
private static ChecksumCalculatingAsyncRequestBody checksumPublisher (AsyncRequestBody sourcePublisher ) {
@@ -133,10 +131,8 @@ private static ByteBuffer nonPosZeroByteBuffer(String content) {
133
131
}
134
132
135
133
@ ParameterizedTest (name = "{index} {0}" )
136
- @ MethodSource ("publishers" )
137
- public void publish_differentAsyncRequestBodiesAndSources_produceCorrectData (String description ,
138
- AsyncRequestBody provider ,
139
- String expectedContent ) throws InterruptedException {
134
+ @ MethodSource ("contentPublishers" )
135
+ public void publish_differentAsyncRequestBodiesAndSources_produceCorrectData (TestCase tc ) throws InterruptedException {
140
136
StringBuilder sb = new StringBuilder ();
141
137
CountDownLatch done = new CountDownLatch (1 );
142
138
@@ -157,11 +153,14 @@ public void onComplete() {
157
153
done .countDown ();
158
154
}
159
155
};
156
+
157
+ AsyncRequestBody provider = checksumPublisher (tc .requestBody );
158
+
160
159
provider .subscribe (subscriber );
161
160
done .await (10 , TimeUnit .SECONDS );
162
161
163
- assertThat (provider .contentLength ()).hasValue ((long ) expectedContent .length ());
164
- assertThat (sb ).hasToString (expectedContent );
162
+ assertThat (provider .contentLength ()).hasValue ((long ) tc . expectedBody .length ());
163
+ assertThat (sb ).hasToString (tc . expectedBody );
165
164
}
166
165
167
166
@ Test
@@ -281,6 +280,27 @@ public void fromBytes_byteArrayNotNullChecksumSupplied() {
281
280
assertThat (BinaryUtils .copyAllBytesFrom (publishedBb )).isEqualTo (expected );
282
281
}
283
282
283
+ @ ParameterizedTest (name = "{index} {0}" )
284
+ @ MethodSource ("contentPublishers" )
285
+ public void explicit0ContentLength_containsEmptyStringTrailingChecksum (TestCase tc ) {
286
+ ChecksumCalculatingAsyncRequestBody checksumBody =
287
+ ChecksumCalculatingAsyncRequestBody .builder ()
288
+ .contentLengthHeader (0L )
289
+ .trailerHeader ("x-amz-checksum-crc32" )
290
+ .algorithm (DefaultChecksumAlgorithm .CRC32 )
291
+ .asyncRequestBody (tc .requestBody )
292
+ .build ();
293
+
294
+ StringBuilder sb = new StringBuilder ();
295
+ for (ByteBuffer byteBuffer : Flowable .fromPublisher (checksumBody ).toList ().blockingGet ()) {
296
+ sb .append (StandardCharsets .UTF_8 .decode (byteBuffer ));
297
+ }
298
+
299
+ // Note: we ignore tc.expectedBody, since we expect the checksum to always be the empty body because of the 0 content
300
+ // length.
301
+ assertThat (sb .toString ()).isEqualTo (expectedEmptyString );
302
+ }
303
+
284
304
static class EmptyBufferPublisher implements AsyncRequestBody {
285
305
286
306
private final ByteBuffer [] buffers = new ByteBuffer [2 ];
@@ -316,4 +336,30 @@ public Optional<Long> contentLength() {
316
336
return Optional .of ((long ) payload .length ());
317
337
}
318
338
}
339
+
340
+ private static class TestCase {
341
+ private String description ;
342
+ private AsyncRequestBody requestBody ;
343
+ private String expectedBody ;
344
+
345
+ public TestCase description (String description ) {
346
+ this .description = description ;
347
+ return this ;
348
+ }
349
+
350
+ public TestCase requestBody (AsyncRequestBody requestBody ) {
351
+ this .requestBody = requestBody ;
352
+ return this ;
353
+ }
354
+
355
+ public TestCase expectedBody (String expectedBody ) {
356
+ this .expectedBody = expectedBody ;
357
+ return this ;
358
+ }
359
+
360
+ @ Override
361
+ public String toString () {
362
+ return description ;
363
+ }
364
+ }
319
365
}
0 commit comments