Skip to content

[Hackathon] Rename to S3ClientSdkExtension & delegate to impl class #3068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
import software.amazon.awssdk.annotations.SdkExtensionMethod;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.NoSuchBucketException;
import software.amazon.awssdk.services.s3.internal.extensions.DefaultS3ClientSdkExtension;
import software.amazon.awssdk.services.s3.model.S3Exception;
import software.amazon.awssdk.utils.Validate;

/**
* Extension methods for the {@link S3Client} interface.
*/
@SdkPublicApi
public interface S3ClientExtensionMethods {
public interface S3ClientSdkExtension {

/**
* Check whether the specified bucket exists in Amazon S3 (and you have permission to access it). If the bucket exists but is
Expand All @@ -39,13 +38,6 @@ public interface S3ClientExtensionMethods {
*/
@SdkExtensionMethod
default boolean doesBucketExist(String bucketName) {
Validate.notNull(bucketName, "bucketName");
S3Client s3Client = (S3Client) this;
try {
s3Client.headBucket(r -> r.bucket(bucketName));
return true;
} catch (NoSuchBucketException e) {
return false;
}
return new DefaultS3ClientSdkExtension((S3Client) this).doesBucketExist(bucketName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.services.s3.internal.extensions;

import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.extensions.S3ClientSdkExtension;
import software.amazon.awssdk.services.s3.model.NoSuchBucketException;
import software.amazon.awssdk.utils.Validate;

@SdkInternalApi
public class DefaultS3ClientSdkExtension implements S3ClientSdkExtension {

private final S3Client s3;

public DefaultS3ClientSdkExtension(S3Client s3) {
this.s3 = Validate.notNull(s3, "s3");
}

@Override
public boolean doesBucketExist(String bucket) {
Validate.notNull(bucket, "bucket");
try {
s3.headBucket(r -> r.bucket(bucket));
return true;
} catch (NoSuchBucketException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* permissions and limitations under the License.
*/

package software.amazon.awssdk.services.s3.extensions;
package software.amazon.awssdk.services.s3.internal.extensions;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand All @@ -30,56 +30,40 @@
import software.amazon.awssdk.services.s3.model.NoSuchBucketException;
import software.amazon.awssdk.services.s3.model.S3Exception;

class S3ClientExtensionMethodsTest {
class DefaultS3ClientSdkExtensionTest {

TestS3Client s3Client;
S3Client s3;

@BeforeEach
void setUp() {
s3Client = spy(TestS3Client.class);
s3 = spy(S3Client.class);
}

@Test
void doesBucketExist_200() {
void doesBucketExist_200_returnsTrue() {
stubHeadBucket(() -> {
return HeadBucketResponse.builder().build();
});
assertThat(s3Client.doesBucketExist("foo")).isEqualTo(true);
assertThat(s3.doesBucketExist("foo")).isEqualTo(true);
}

@Test
void doesBucketExist_404() {
void doesBucketExist_404_returnsFalse() {
stubHeadBucket(() -> {
throw NoSuchBucketException.builder().build();
});
assertThat(s3Client.doesBucketExist("foo")).isEqualTo(false);
assertThat(s3.doesBucketExist("foo")).isEqualTo(false);
}

@Test
void doesBucketExist_403() {
void doesBucketExist_403_propagatesException() {
stubHeadBucket(() -> {
throw S3Exception.builder().build();
});
assertThatThrownBy(() -> s3Client.doesBucketExist("foo")).isInstanceOf(S3Exception.class);
assertThatThrownBy(() -> s3.doesBucketExist("foo")).isInstanceOf(S3Exception.class);
}

private void stubHeadBucket(Supplier<HeadBucketResponse> behavior) {
doAnswer(i -> behavior.get()).when(s3Client).headBucket(any(HeadBucketRequest.class));
}

static class TestS3Client implements S3Client, S3ClientExtensionMethods {

@Override
public String serviceName() {
return null;
}

@Override
public void close() {

}

TestS3Client() {
}
doAnswer(i -> behavior.get()).when(s3).headBucket(any(HeadBucketRequest.class));
}
}