Skip to content

Commit af94ef1

Browse files
committed
Added file config options to FileAsyncResponseTransformerPublisherTest
1 parent fe7985f commit af94ef1

1 file changed

Lines changed: 83 additions & 13 deletions

File tree

core/sdk-core/src/test/java/software/amazon/awssdk/core/internal/async/FileAsyncResponseTransformerPublisherTest.java

Lines changed: 83 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
package software.amazon.awssdk.core.internal.async;
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1920
import static org.assertj.core.api.Assertions.fail;
21+
import static org.assertj.core.api.Assertions.in;
2022
import static org.mockito.Mockito.mock;
2123
import static org.mockito.Mockito.when;
2224

@@ -29,16 +31,23 @@
2931
import java.util.Arrays;
3032
import java.util.List;
3133
import java.util.Optional;
34+
import java.util.UUID;
3235
import java.util.concurrent.CompletableFuture;
3336
import java.util.concurrent.CountDownLatch;
3437
import java.util.concurrent.TimeUnit;
3538
import java.util.concurrent.atomic.AtomicInteger;
3639
import java.util.concurrent.atomic.AtomicReference;
40+
import java.util.function.Function;
41+
import java.util.stream.Stream;
3742
import org.junit.jupiter.api.AfterEach;
3843
import org.junit.jupiter.api.BeforeEach;
3944
import org.junit.jupiter.api.Test;
45+
import org.junit.jupiter.api.TestInstance;
46+
import org.junit.jupiter.params.ParameterizedTest;
47+
import org.junit.jupiter.params.provider.MethodSource;
4048
import org.reactivestreams.Subscriber;
4149
import org.reactivestreams.Subscription;
50+
import software.amazon.awssdk.core.FileTransformerConfiguration;
4251
import software.amazon.awssdk.core.SdkResponse;
4352
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
4453
import software.amazon.awssdk.core.async.SdkPublisher;
@@ -53,22 +62,27 @@ class FileAsyncResponseTransformerPublisherTest {
5362
@BeforeEach
5463
void setUp() throws Exception {
5564
fileSystem = Jimfs.newFileSystem();
56-
testFile = fileSystem.getPath("/test-file.txt");
65+
testFile = fileSystem.getPath(String.format("/test-file-%s.txt", UUID.randomUUID()));
5766
}
5867

5968
@AfterEach
6069
void tearDown() throws Exception {
6170
fileSystem.close();
6271
}
6372

64-
@Test
65-
void singleDemand_shouldEmitOneTransformer() throws Exception {
73+
@ParameterizedTest
74+
@MethodSource("transformers")
75+
void singleDemand_shouldEmitOneTransformer(
76+
Function<Path, AsyncResponseTransformer<SdkResponse, SdkResponse>> transformerFunction) throws Exception {
6677
// Given
67-
FileAsyncResponseTransformer<SdkResponse> initialTransformer =
68-
(FileAsyncResponseTransformer<SdkResponse>) AsyncResponseTransformer.<SdkResponse>toFile(testFile);
78+
// FileAsyncResponseTransformer<SdkResponse> initialTransformer =
79+
// (FileAsyncResponseTransformer<SdkResponse>) AsyncResponseTransformer.<SdkResponse>toFile(testFile);
80+
81+
AsyncResponseTransformer<SdkResponse, SdkResponse> initialTransformer = transformerFunction.apply(testFile);
82+
createFileIfNeeded(initialTransformer);
6983

7084
FileAsyncResponseTransformerPublisher<SdkResponse> publisher =
71-
new FileAsyncResponseTransformerPublisher<>(initialTransformer);
85+
new FileAsyncResponseTransformerPublisher<>((FileAsyncResponseTransformer<SdkResponse>) initialTransformer);
7286

7387
CountDownLatch latch = new CountDownLatch(1);
7488
AtomicReference<AsyncResponseTransformer<SdkResponse, SdkResponse>> receivedTransformer = new AtomicReference<>();
@@ -119,6 +133,14 @@ public void onComplete() {
119133
assertThat(future).succeedsWithin(10, TimeUnit.SECONDS);
120134
}
121135

136+
private void createFileIfNeeded(AsyncResponseTransformer<SdkResponse, SdkResponse> initialTransformer) throws Exception {
137+
FileTransformerConfiguration.FileWriteOption fileWriteOption =
138+
((FileAsyncResponseTransformer<SdkResponse>) initialTransformer).config().fileWriteOption();
139+
if (fileWriteOption == FileTransformerConfiguration.FileWriteOption.WRITE_TO_POSITION) {
140+
Files.createFile(testFile);
141+
}
142+
}
143+
122144
private SdkPublisher<ByteBuffer> createMockPublisher() {
123145
return s -> s.onSubscribe(new Subscription() {
124146
@Override
@@ -133,12 +155,18 @@ public void cancel() {
133155
});
134156
}
135157

136-
@Test
137-
void requestManyTransformers_withResponseContainingDifferentContentRanges_shouldWriteToFileAtThoseRanges() throws Exception {
158+
@ParameterizedTest
159+
@MethodSource("transformers")
160+
void requestManyTransformers_withResponseContainingDifferentContentRanges_shouldWriteToFileAtThoseRanges(
161+
Function<Path, AsyncResponseTransformer<SdkResponse, SdkResponse>> transformerFunction
162+
) throws Exception {
138163
// Given
139-
AsyncResponseTransformer<Object, Object> initialTransformer = AsyncResponseTransformer.toFile(testFile);
164+
FileAsyncResponseTransformer<SdkResponse> initialTransformer =
165+
(FileAsyncResponseTransformer<SdkResponse>) transformerFunction.apply(testFile);
166+
createFileIfNeeded(initialTransformer);
167+
140168
FileAsyncResponseTransformerPublisher<SdkResponse> publisher =
141-
new FileAsyncResponseTransformerPublisher<>((FileAsyncResponseTransformer<?>) initialTransformer);
169+
new FileAsyncResponseTransformerPublisher<>(initialTransformer);
142170

143171
int numTransformers = 8;
144172
CountDownLatch latch = new CountDownLatch(numTransformers);
@@ -196,15 +224,18 @@ public void onComplete() {
196224
assertThat(Files.exists(testFile)).isTrue();
197225
byte[] fileContent = Files.readAllBytes(testFile);
198226

199-
assertThat(fileContent.length).isEqualTo(80);
200-
227+
int offset =
228+
initialTransformer.config().fileWriteOption() == FileTransformerConfiguration.FileWriteOption.WRITE_TO_POSITION
229+
? (int) initialTransformer.position()
230+
: 0;
231+
assertThat(fileContent.length).isEqualTo(80 + offset);
201232
for (int i = 0; i < numTransformers; i++) {
202233
int startPos = i * 10;
203234
byte[] expectedData = new byte[10];
204235
for (int j = 0; j < 10; j++) {
205236
expectedData[j] = (byte) ((byte) startPos + j);
206237
}
207-
byte[] actualData = Arrays.copyOfRange(fileContent, startPos, startPos + 10);
238+
byte[] actualData = Arrays.copyOfRange(fileContent, startPos + offset, startPos + offset + 10);
208239
assertThat(actualData).isEqualTo(expectedData);
209240
}
210241
}
@@ -233,4 +264,43 @@ public void cancel() {
233264
});
234265
}
235266

267+
private static Stream<Function<Path, AsyncResponseTransformer<SdkResponse, SdkResponse>>> transformers() {
268+
return Stream.of(
269+
AsyncResponseTransformer::toFile,
270+
path -> AsyncResponseTransformer.toFile(
271+
path,
272+
FileTransformerConfiguration.builder()
273+
.fileWriteOption(FileTransformerConfiguration.FileWriteOption.CREATE_NEW)
274+
.failureBehavior(FileTransformerConfiguration.FailureBehavior.LEAVE)
275+
.build()),
276+
path -> AsyncResponseTransformer.toFile(
277+
path,
278+
FileTransformerConfiguration.builder()
279+
.fileWriteOption(FileTransformerConfiguration.FileWriteOption.CREATE_OR_REPLACE_EXISTING)
280+
.failureBehavior(FileTransformerConfiguration.FailureBehavior.LEAVE)
281+
.build()),
282+
path -> AsyncResponseTransformer.toFile(
283+
path,
284+
FileTransformerConfiguration.builder()
285+
.fileWriteOption(FileTransformerConfiguration.FileWriteOption.WRITE_TO_POSITION)
286+
.failureBehavior(FileTransformerConfiguration.FailureBehavior.LEAVE)
287+
.position(10L)
288+
.build())
289+
);
290+
}
291+
292+
@Test
293+
void createOrAppendToExisting_shouldThrowException() throws Exception {
294+
AsyncResponseTransformer<Object, Object> initialTransformer = AsyncResponseTransformer.toFile(
295+
testFile,
296+
FileTransformerConfiguration.builder()
297+
.failureBehavior(FileTransformerConfiguration.FailureBehavior.DELETE)
298+
.fileWriteOption(FileTransformerConfiguration.FileWriteOption.CREATE_OR_APPEND_TO_EXISTING)
299+
.build());
300+
assertThatThrownBy(() -> new FileAsyncResponseTransformerPublisher<>((FileAsyncResponseTransformer<?>) initialTransformer))
301+
.isInstanceOf(IllegalArgumentException.class)
302+
.hasMessageContaining("CREATE_OR_APPEND_TO_EXISTING");
303+
304+
}
305+
236306
}

0 commit comments

Comments
 (0)