Skip to content

Commit 8364df3

Browse files
author
Bennett Lynch
authored
[Hackathon] Rename to S3ClientSdkExtension & delegate to impl class (#3068)
1 parent a71c177 commit 8364df3

File tree

3 files changed

+57
-38
lines changed

3 files changed

+57
-38
lines changed
Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818
import software.amazon.awssdk.annotations.SdkExtensionMethod;
1919
import software.amazon.awssdk.annotations.SdkPublicApi;
2020
import software.amazon.awssdk.services.s3.S3Client;
21-
import software.amazon.awssdk.services.s3.model.NoSuchBucketException;
21+
import software.amazon.awssdk.services.s3.internal.extensions.DefaultS3ClientSdkExtension;
2222
import software.amazon.awssdk.services.s3.model.S3Exception;
23-
import software.amazon.awssdk.utils.Validate;
2423

2524
/**
2625
* Extension methods for the {@link S3Client} interface.
2726
*/
2827
@SdkPublicApi
29-
public interface S3ClientExtensionMethods {
28+
public interface S3ClientSdkExtension {
3029

3130
/**
3231
* Check whether the specified bucket exists in Amazon S3 (and you have permission to access it). If the bucket exists but is
@@ -39,13 +38,6 @@ public interface S3ClientExtensionMethods {
3938
*/
4039
@SdkExtensionMethod
4140
default boolean doesBucketExist(String bucketName) {
42-
Validate.notNull(bucketName, "bucketName");
43-
S3Client s3Client = (S3Client) this;
44-
try {
45-
s3Client.headBucket(r -> r.bucket(bucketName));
46-
return true;
47-
} catch (NoSuchBucketException e) {
48-
return false;
49-
}
41+
return new DefaultS3ClientSdkExtension((S3Client) this).doesBucketExist(bucketName);
5042
}
5143
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.services.s3.internal.extensions;
17+
18+
import software.amazon.awssdk.annotations.SdkInternalApi;
19+
import software.amazon.awssdk.services.s3.S3Client;
20+
import software.amazon.awssdk.services.s3.extensions.S3ClientSdkExtension;
21+
import software.amazon.awssdk.services.s3.model.NoSuchBucketException;
22+
import software.amazon.awssdk.utils.Validate;
23+
24+
@SdkInternalApi
25+
public class DefaultS3ClientSdkExtension implements S3ClientSdkExtension {
26+
27+
private final S3Client s3;
28+
29+
public DefaultS3ClientSdkExtension(S3Client s3) {
30+
this.s3 = Validate.notNull(s3, "s3");
31+
}
32+
33+
@Override
34+
public boolean doesBucketExist(String bucket) {
35+
Validate.notNull(bucket, "bucket");
36+
try {
37+
s3.headBucket(r -> r.bucket(bucket));
38+
return true;
39+
} catch (NoSuchBucketException e) {
40+
return false;
41+
}
42+
}
43+
}

services/s3/src/test/java/software/amazon/awssdk/services/s3/extensions/S3ClientExtensionMethodsTest.java renamed to services/s3/src/test/java/software/amazon/awssdk/services/s3/internal/extensions/DefaultS3ClientSdkExtensionTest.java

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16-
package software.amazon.awssdk.services.s3.extensions;
16+
package software.amazon.awssdk.services.s3.internal.extensions;
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
1919
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -30,56 +30,40 @@
3030
import software.amazon.awssdk.services.s3.model.NoSuchBucketException;
3131
import software.amazon.awssdk.services.s3.model.S3Exception;
3232

33-
class S3ClientExtensionMethodsTest {
33+
class DefaultS3ClientSdkExtensionTest {
3434

35-
TestS3Client s3Client;
35+
S3Client s3;
3636

3737
@BeforeEach
3838
void setUp() {
39-
s3Client = spy(TestS3Client.class);
39+
s3 = spy(S3Client.class);
4040
}
4141

4242
@Test
43-
void doesBucketExist_200() {
43+
void doesBucketExist_200_returnsTrue() {
4444
stubHeadBucket(() -> {
4545
return HeadBucketResponse.builder().build();
4646
});
47-
assertThat(s3Client.doesBucketExist("foo")).isEqualTo(true);
47+
assertThat(s3.doesBucketExist("foo")).isEqualTo(true);
4848
}
4949

5050
@Test
51-
void doesBucketExist_404() {
51+
void doesBucketExist_404_returnsFalse() {
5252
stubHeadBucket(() -> {
5353
throw NoSuchBucketException.builder().build();
5454
});
55-
assertThat(s3Client.doesBucketExist("foo")).isEqualTo(false);
55+
assertThat(s3.doesBucketExist("foo")).isEqualTo(false);
5656
}
5757

5858
@Test
59-
void doesBucketExist_403() {
59+
void doesBucketExist_403_propagatesException() {
6060
stubHeadBucket(() -> {
6161
throw S3Exception.builder().build();
6262
});
63-
assertThatThrownBy(() -> s3Client.doesBucketExist("foo")).isInstanceOf(S3Exception.class);
63+
assertThatThrownBy(() -> s3.doesBucketExist("foo")).isInstanceOf(S3Exception.class);
6464
}
6565

6666
private void stubHeadBucket(Supplier<HeadBucketResponse> behavior) {
67-
doAnswer(i -> behavior.get()).when(s3Client).headBucket(any(HeadBucketRequest.class));
68-
}
69-
70-
static class TestS3Client implements S3Client, S3ClientExtensionMethods {
71-
72-
@Override
73-
public String serviceName() {
74-
return null;
75-
}
76-
77-
@Override
78-
public void close() {
79-
80-
}
81-
82-
TestS3Client() {
83-
}
67+
doAnswer(i -> behavior.get()).when(s3).headBucket(any(HeadBucketRequest.class));
8468
}
8569
}

0 commit comments

Comments
 (0)