28
28
import java .nio .file .FileAlreadyExistsException ;
29
29
import java .nio .file .FileSystem ;
30
30
import java .nio .file .Files ;
31
+ import java .nio .file .NoSuchFileException ;
31
32
import java .nio .file .Path ;
32
33
import java .util .ArrayList ;
33
- import java .util .Arrays ;
34
34
import java .util .Collection ;
35
35
import java .util .List ;
36
36
import java .util .concurrent .Callable ;
50
50
import org .reactivestreams .Subscription ;
51
51
import software .amazon .awssdk .core .FileTransformerConfiguration ;
52
52
import software .amazon .awssdk .core .FileTransformerConfiguration .FileWriteOption ;
53
+ import software .amazon .awssdk .core .FileTransformerConfiguration .FailureBehavior ;
53
54
import software .amazon .awssdk .core .async .SdkPublisher ;
54
55
55
56
/**
@@ -185,8 +186,11 @@ void createOrAppendExisting_fileExists_shouldAppend() throws Exception {
185
186
@ MethodSource ("configurations" )
186
187
void exceptionOccurred_deleteFileBehavior (FileTransformerConfiguration configuration ) throws Exception {
187
188
Path testPath = testFs .getPath ("test_file.txt" );
188
- FileAsyncResponseTransformer <String > transformer = new FileAsyncResponseTransformer <>(testPath ,
189
- configuration );
189
+ if (configuration .fileWriteOption () == FileWriteOption .WRITE_TO_POSITION ) {
190
+ // file must exist for WRITE_TO_POSITION
191
+ Files .write (testPath , "foobar" .getBytes (StandardCharsets .UTF_8 ));
192
+ }
193
+ FileAsyncResponseTransformer <String > transformer = new FileAsyncResponseTransformer <>(testPath , configuration );
190
194
stubException (RandomStringUtils .random (200 ), transformer );
191
195
if (configuration .failureBehavior () == LEAVE ) {
192
196
assertThat (testPath ).exists ();
@@ -196,28 +200,19 @@ void exceptionOccurred_deleteFileBehavior(FileTransformerConfiguration configura
196
200
}
197
201
198
202
private static List <FileTransformerConfiguration > configurations () {
199
- return Arrays .asList (
200
- FileTransformerConfiguration .defaultCreateNew (),
201
- FileTransformerConfiguration .defaultCreateOrAppend (),
202
- FileTransformerConfiguration .defaultCreateOrReplaceExisting (),
203
- FileTransformerConfiguration .builder ()
204
- .fileWriteOption (FileWriteOption .CREATE_NEW )
205
- .failureBehavior (LEAVE ).build (),
206
- FileTransformerConfiguration .builder ()
207
- .fileWriteOption (FileWriteOption .CREATE_NEW )
208
- .failureBehavior (DELETE ).build (),
209
- FileTransformerConfiguration .builder ()
210
- .fileWriteOption (FileWriteOption .CREATE_OR_APPEND_TO_EXISTING )
211
- .failureBehavior (DELETE ).build (),
212
- FileTransformerConfiguration .builder ()
213
- .fileWriteOption (FileWriteOption .CREATE_OR_APPEND_TO_EXISTING )
214
- .failureBehavior (LEAVE ).build (),
215
- FileTransformerConfiguration .builder ()
216
- .fileWriteOption (FileWriteOption .CREATE_OR_REPLACE_EXISTING )
217
- .failureBehavior (DELETE ).build (),
218
- FileTransformerConfiguration .builder ()
219
- .fileWriteOption (FileWriteOption .CREATE_OR_REPLACE_EXISTING )
220
- .failureBehavior (LEAVE ).build ());
203
+ List <FileTransformerConfiguration > conf = new ArrayList <>();
204
+ conf .add (FileTransformerConfiguration .defaultCreateNew ());
205
+ conf .add (FileTransformerConfiguration .defaultCreateOrAppend ());
206
+ conf .add (FileTransformerConfiguration .defaultCreateOrReplaceExisting ());
207
+ for (FailureBehavior failureBehavior : FailureBehavior .values ()) {
208
+ for (FileWriteOption fileWriteOption : FileWriteOption .values ()) {
209
+ conf .add (FileTransformerConfiguration .builder ()
210
+ .fileWriteOption (fileWriteOption )
211
+ .failureBehavior (failureBehavior )
212
+ .build ());
213
+ }
214
+ }
215
+ return conf ;
221
216
}
222
217
223
218
@ Test
@@ -246,6 +241,63 @@ void explicitExecutor_shouldUseExecutor() throws Exception {
246
241
}
247
242
}
248
243
244
+ @ Test
245
+ void writeToPosition_fileExists_shouldAppendFromPosition () throws Exception {
246
+ int totalSize = 100 ;
247
+ long prefixSize = 80L ;
248
+ int newContentLength = 20 ;
249
+
250
+ Path testPath = testFs .getPath ("test_file.txt" );
251
+ String contentBeforeRewrite = RandomStringUtils .randomAlphanumeric (totalSize );
252
+ byte [] existingBytes = contentBeforeRewrite .getBytes (StandardCharsets .UTF_8 );
253
+ Files .write (testPath , existingBytes );
254
+ String newContent = RandomStringUtils .randomAlphanumeric (newContentLength );
255
+ FileAsyncResponseTransformer <String > transformer = new FileAsyncResponseTransformer <>(
256
+ testPath ,
257
+ FileTransformerConfiguration .builder ()
258
+ .position (prefixSize )
259
+ .failureBehavior (DELETE )
260
+ .fileWriteOption (FileWriteOption .WRITE_TO_POSITION )
261
+ .build ());
262
+
263
+ stubSuccessfulStreaming (newContent , transformer );
264
+
265
+ String expectedContent = contentBeforeRewrite .substring (0 , 80 ) + newContent ;
266
+ assertThat (testPath ).hasContent (expectedContent );
267
+ }
268
+
269
+ @ Test
270
+ void writeToPosition_fileDoesNotExists_shouldThrowException () throws Exception {
271
+ Path path = testFs .getPath ("this/file/does/not/exists" );
272
+ FileAsyncResponseTransformer <String > transformer = new FileAsyncResponseTransformer <>(path );
273
+ transformer .prepare ();
274
+ transformer .onResponse ("foobar" );
275
+ assertThatThrownBy (() -> transformer .onStream (testPublisher ("foo-bar-content" )))
276
+ .hasRootCauseInstanceOf (NoSuchFileException .class );
277
+
278
+ }
279
+
280
+ @ Test
281
+ void writeToPosition_fileExists_positionNotDefined_shouldRewriteFromStart () throws Exception {
282
+ int totalSize = 100 ;
283
+ Path testPath = testFs .getPath ("test_file.txt" );
284
+ String contentBeforeRewrite = RandomStringUtils .randomAlphanumeric (totalSize );
285
+ byte [] existingBytes = contentBeforeRewrite .getBytes (StandardCharsets .UTF_8 );
286
+ Files .write (testPath , existingBytes );
287
+ String newContent = RandomStringUtils .randomAlphanumeric (totalSize );
288
+ FileAsyncResponseTransformer <String > transformer = new FileAsyncResponseTransformer <>(
289
+ testPath ,
290
+ FileTransformerConfiguration .builder ()
291
+ .failureBehavior (DELETE )
292
+ .fileWriteOption (FileWriteOption .WRITE_TO_POSITION )
293
+ .build ());
294
+
295
+ stubSuccessfulStreaming (newContent , transformer );
296
+
297
+ assertThat (testPath ).hasContent (newContent );
298
+
299
+ }
300
+
249
301
private static void stubSuccessfulStreaming (String newContent , FileAsyncResponseTransformer <String > transformer ) throws Exception {
250
302
CompletableFuture <String > future = transformer .prepare ();
251
303
transformer .onResponse ("foobar" );
0 commit comments