17
17
18
18
import static org .assertj .core .api .Assertions .assertThat ;
19
19
import static software .amazon .awssdk .testutils .service .S3BucketUtils .temporaryBucketName ;
20
+ import static software .amazon .awssdk .utils .IoUtils .closeQuietly ;
20
21
21
22
import java .io .IOException ;
22
23
import java .nio .charset .StandardCharsets ;
25
26
import java .nio .file .Paths ;
26
27
import java .util .List ;
27
28
import java .util .stream .Collectors ;
29
+ import org .apache .commons .lang3 .RandomStringUtils ;
28
30
import org .junit .AfterClass ;
29
31
import org .junit .BeforeClass ;
30
32
import org .junit .Test ;
33
+ import software .amazon .awssdk .core .sync .ResponseTransformer ;
31
34
import software .amazon .awssdk .services .s3 .S3Client ;
32
35
import software .amazon .awssdk .services .s3 .model .S3Object ;
33
36
import software .amazon .awssdk .testutils .FileUtils ;
37
+ import software .amazon .awssdk .utils .Logger ;
34
38
35
39
public class S3TransferManagerUploadDirectoryIntegrationTest extends S3IntegrationTestBase {
40
+ private static final Logger log = Logger .loggerFor (S3TransferManagerUploadDirectoryIntegrationTest .class );
36
41
private static final String TEST_BUCKET = temporaryBucketName (S3TransferManagerUploadIntegrationTest .class );
37
42
38
43
private static S3TransferManager tm ;
39
44
private static Path directory ;
40
45
private static S3Client s3Client ;
46
+ private static String randomString ;
41
47
42
48
@ BeforeClass
43
49
public static void setUp () throws Exception {
44
50
S3IntegrationTestBase .setUp ();
45
51
createBucket (TEST_BUCKET );
46
-
52
+ randomString = RandomStringUtils . random ( 100 );
47
53
directory = createLocalTestDirectory ();
48
54
49
55
tm = S3TransferManager .builder ()
@@ -59,27 +65,63 @@ public static void setUp() throws Exception {
59
65
60
66
@ AfterClass
61
67
public static void teardown () {
62
- tm .close ();
63
- s3Client .close ();
64
- deleteBucketAndAllContents (TEST_BUCKET );
65
- FileUtils .cleanUpTestDirectory (directory );
68
+ try {
69
+ FileUtils .cleanUpTestDirectory (directory );
70
+ } catch (Exception exception ) {
71
+ log .warn (() -> "Failed to clean up test directory " + directory , exception );
72
+ }
73
+
74
+ try {
75
+ deleteBucketAndAllContents (TEST_BUCKET );
76
+ } catch (Exception exception ) {
77
+ log .warn (() -> "Failed to delete s3 bucket " + TEST_BUCKET , exception );
78
+ }
79
+
80
+ closeQuietly (tm , log .logger ());
81
+ closeQuietly (s3Client , log .logger ());
66
82
S3IntegrationTestBase .cleanUp ();
67
83
}
68
84
69
85
@ Test
70
86
public void uploadDirectory_filesSentCorrectly () {
71
87
String prefix = "yolo" ;
72
- UploadDirectory uploadDirectory = tm .uploadDirectory (u -> u .sourceDirectory (directory )
73
- .bucket (TEST_BUCKET )
74
- .prefix (prefix )
75
- .overrideConfiguration (o -> o .recursive (true )));
76
- uploadDirectory .completionFuture ().join ();
88
+ UploadDirectoryTransfer uploadDirectory = tm .uploadDirectory (u -> u .sourceDirectory (directory )
89
+ .bucket (TEST_BUCKET )
90
+ .prefix (prefix )
91
+ .overrideConfiguration (o -> o .recursive (true )));
92
+ CompletedUploadDirectory completedUploadDirectory = uploadDirectory .completionFuture ().join ();
93
+ assertThat (completedUploadDirectory .failedUploads ()).isEmpty ();
77
94
78
95
List <String > keys =
79
96
s3Client .listObjectsV2Paginator (b -> b .bucket (TEST_BUCKET ).prefix (prefix )).contents ().stream ().map (S3Object ::key )
80
97
.collect (Collectors .toList ());
81
98
82
99
assertThat (keys ).containsOnly (prefix + "/bar.txt" , prefix + "/foo/1.txt" , prefix + "/foo/2.txt" );
100
+
101
+ keys .forEach (k -> verifyContent (k , k .substring (prefix .length () + 1 ) + randomString ));
102
+ }
103
+
104
+ @ Test
105
+ public void uploadDirectory_withDelimiter_filesSentCorrectly () {
106
+ String prefix = "hello" ;
107
+ String delimiter = "0" ;
108
+ UploadDirectoryTransfer uploadDirectory = tm .uploadDirectory (u -> u .sourceDirectory (directory )
109
+ .bucket (TEST_BUCKET )
110
+ .delimiter (delimiter )
111
+ .prefix (prefix )
112
+ .overrideConfiguration (o -> o .recursive (true )));
113
+ CompletedUploadDirectory completedUploadDirectory = uploadDirectory .completionFuture ().join ();
114
+ assertThat (completedUploadDirectory .failedUploads ()).isEmpty ();
115
+
116
+ List <String > keys =
117
+ s3Client .listObjectsV2Paginator (b -> b .bucket (TEST_BUCKET ).prefix (prefix )).contents ().stream ().map (S3Object ::key )
118
+ .collect (Collectors .toList ());
119
+
120
+ assertThat (keys ).containsOnly (prefix + "0bar.txt" , prefix + "0foo01.txt" , prefix + "0foo02.txt" );
121
+ keys .forEach (k -> {
122
+ String path = k .replace (delimiter , "/" );
123
+ verifyContent (k , path .substring (prefix .length () + 1 ) + randomString );
124
+ });
83
125
}
84
126
85
127
private static Path createLocalTestDirectory () throws IOException {
@@ -88,11 +130,17 @@ private static Path createLocalTestDirectory() throws IOException {
88
130
String directoryName = directory .toString ();
89
131
90
132
Files .createDirectory (Paths .get (directory + "/foo" ));
91
-
92
- Files .write (Paths .get (directoryName , "bar.txt" ), "bar" .getBytes (StandardCharsets .UTF_8 ));
93
- Files .write (Paths .get (directoryName , "foo/1.txt" ), "1" .getBytes (StandardCharsets .UTF_8 ));
94
- Files .write (Paths .get (directoryName , "foo/2.txt" ), "2" .getBytes (StandardCharsets .UTF_8 ));
133
+ Files .write (Paths .get (directoryName , "bar.txt" ), ("bar.txt" + randomString ).getBytes (StandardCharsets .UTF_8 ));
134
+ Files .write (Paths .get (directoryName , "foo/1.txt" ), ("foo/1.txt" + randomString ).getBytes (StandardCharsets .UTF_8 ));
135
+ Files .write (Paths .get (directoryName , "foo/2.txt" ), ("foo/2.txt" + randomString ).getBytes (StandardCharsets .UTF_8 ));
95
136
96
137
return directory ;
97
138
}
139
+
140
+ private static void verifyContent (String key , String expectedContent ) {
141
+ String actualContent = s3 .getObject (r -> r .bucket (TEST_BUCKET ).key (key ),
142
+ ResponseTransformer .toBytes ()).asUtf8String ();
143
+
144
+ assertThat (actualContent ).isEqualTo (expectedContent );
145
+ }
98
146
}
0 commit comments