Skip to content

Commit 1e5272c

Browse files
committed
Fix tests
1 parent e1af859 commit 1e5272c

1 file changed

Lines changed: 47 additions & 33 deletions

File tree

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

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
import static org.assertj.core.api.Assertions.assertThat;
1919
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2020
import static org.mockito.ArgumentMatchers.any;
21-
import static org.mockito.Mockito.mock;
22-
import static org.mockito.Mockito.when;
21+
import static org.mockito.Mockito.doAnswer;
22+
import static org.mockito.Mockito.spy;
2323

24-
import java.util.function.Consumer;
24+
import java.util.function.Supplier;
2525
import org.junit.jupiter.api.BeforeEach;
2626
import org.junit.jupiter.api.Test;
2727
import software.amazon.awssdk.services.s3.S3Client;
@@ -35,80 +35,94 @@
3535

3636
class DefaultS3ClientSdkExtensionTest {
3737

38-
private S3Client s3;
39-
private DefaultS3ClientSdkExtension extension;
38+
S3Client s3;
4039

4140
@BeforeEach
4241
void setUp() {
43-
s3 = mock(S3Client.class);
44-
extension = new DefaultS3ClientSdkExtension(s3);
42+
s3 = spy(S3Client.class);
4543
}
4644

4745
@Test
48-
void doesObjectExist_objectExists_returnsTrue() {
49-
when(s3.headObject(any(Consumer.class))).thenReturn(HeadObjectResponse.builder().build());
50-
assertThat(extension.doesObjectExist("bucket", "key")).isTrue();
46+
void doesBucketExist_200_returnsTrue() {
47+
stubHeadBucket(() -> HeadBucketResponse.builder().build());
48+
assertThat(s3.doesBucketExist("foo")).isEqualTo(true);
5149
}
5250

5351
@Test
54-
void doesObjectExist_noSuchKey_returnsFalse() {
55-
when(s3.headObject(any(Consumer.class))).thenThrow(NoSuchKeyException.builder().build());
56-
assertThat(extension.doesObjectExist("bucket", "key")).isFalse();
52+
void doesBucketExist_404_returnsFalse() {
53+
stubHeadBucket(() -> {
54+
throw NoSuchBucketException.builder().build();
55+
});
56+
assertThat(s3.doesBucketExist("foo")).isEqualTo(false);
5757
}
5858

5959
@Test
60-
void doesObjectExist_otherS3Exception_propagates() {
61-
S3Exception forbidden = (S3Exception) S3Exception.builder().statusCode(403).message("Forbidden").build();
62-
when(s3.headObject(any(Consumer.class))).thenThrow(forbidden);
63-
assertThatThrownBy(() -> extension.doesObjectExist("bucket", "key")).isSameAs(forbidden);
60+
void doesBucketExist_403_propagatesException() {
61+
stubHeadBucket(() -> {
62+
throw S3Exception.builder().build();
63+
});
64+
assertThatThrownBy(() -> s3.doesBucketExist("foo")).isInstanceOf(S3Exception.class);
6465
}
6566

6667
@Test
67-
void doesBucketExist_bucketExists_returnsTrue() {
68-
when(s3.headBucket(any(Consumer.class))).thenReturn(HeadBucketResponse.builder().build());
69-
assertThat(extension.doesBucketExist("bucket")).isTrue();
68+
void doesObjectExist_200_returnsTrue() {
69+
stubHeadObject(() -> HeadObjectResponse.builder().build());
70+
assertThat(s3.doesObjectExist("foo", "bar")).isEqualTo(true);
7071
}
7172

7273
@Test
73-
void doesBucketExist_noSuchBucket_returnsFalse() {
74-
when(s3.headBucket(any(Consumer.class))).thenThrow(NoSuchBucketException.builder().build());
75-
assertThat(extension.doesBucketExist("bucket")).isFalse();
74+
void doesObjectExist_404_returnsFalse() {
75+
stubHeadObject(() -> {
76+
throw NoSuchKeyException.builder().build();
77+
});
78+
assertThat(s3.doesObjectExist("foo", "bar")).isEqualTo(false);
7679
}
7780

7881
@Test
79-
void doesBucketExist_otherS3Exception_propagates() {
80-
S3Exception forbidden = (S3Exception) S3Exception.builder().statusCode(403).message("Forbidden").build();
81-
when(s3.headBucket(any(Consumer.class))).thenThrow(forbidden);
82-
assertThatThrownBy(() -> extension.doesBucketExist("bucket")).isSameAs(forbidden);
82+
void doesObjectExist_403_propagatesException() {
83+
stubHeadObject(() -> {
84+
throw S3Exception.builder().build();
85+
});
86+
assertThatThrownBy(() -> s3.doesObjectExist("foo", "bar")).isInstanceOf(S3Exception.class);
8387
}
8488

89+
// Validation tests (not in prototype)
90+
8591
@Test
8692
void doesBucketExist_nullBucket_throws() {
87-
assertThatThrownBy(() -> extension.doesBucketExist(null)).isInstanceOf(NullPointerException.class);
93+
assertThatThrownBy(() -> s3.doesBucketExist(null)).isInstanceOf(NullPointerException.class);
8894
}
8995

9096
@Test
9197
void doesBucketExist_emptyBucket_throws() {
92-
assertThatThrownBy(() -> extension.doesBucketExist("")).isInstanceOf(IllegalArgumentException.class);
98+
assertThatThrownBy(() -> s3.doesBucketExist("")).isInstanceOf(IllegalArgumentException.class);
9399
}
94100

95101
@Test
96102
void doesObjectExist_nullBucket_throws() {
97-
assertThatThrownBy(() -> extension.doesObjectExist(null, "key")).isInstanceOf(NullPointerException.class);
103+
assertThatThrownBy(() -> s3.doesObjectExist(null, "key")).isInstanceOf(NullPointerException.class);
98104
}
99105

100106
@Test
101107
void doesObjectExist_emptyBucket_throws() {
102-
assertThatThrownBy(() -> extension.doesObjectExist("", "key")).isInstanceOf(IllegalArgumentException.class);
108+
assertThatThrownBy(() -> s3.doesObjectExist("", "key")).isInstanceOf(IllegalArgumentException.class);
103109
}
104110

105111
@Test
106112
void doesObjectExist_nullKey_throws() {
107-
assertThatThrownBy(() -> extension.doesObjectExist("bucket", null)).isInstanceOf(NullPointerException.class);
113+
assertThatThrownBy(() -> s3.doesObjectExist("bucket", null)).isInstanceOf(NullPointerException.class);
108114
}
109115

110116
@Test
111117
void doesObjectExist_emptyKey_throws() {
112-
assertThatThrownBy(() -> extension.doesObjectExist("bucket", "")).isInstanceOf(IllegalArgumentException.class);
118+
assertThatThrownBy(() -> s3.doesObjectExist("bucket", "")).isInstanceOf(IllegalArgumentException.class);
119+
}
120+
121+
private void stubHeadBucket(Supplier<HeadBucketResponse> behavior) {
122+
doAnswer(i -> behavior.get()).when(s3).headBucket(any(HeadBucketRequest.class));
123+
}
124+
125+
private void stubHeadObject(Supplier<HeadObjectResponse> behavior) {
126+
doAnswer(i -> behavior.get()).when(s3).headObject(any(HeadObjectRequest.class));
113127
}
114128
}

0 commit comments

Comments
 (0)