Skip to content

Commit 383084a

Browse files
committed
Fix tests
1 parent 65248cd commit 383084a

1 file changed

Lines changed: 59 additions & 35 deletions

File tree

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

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
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.doReturn;
22+
import static org.mockito.Mockito.spy;
2323

2424
import java.util.concurrent.CompletableFuture;
2525
import java.util.concurrent.CompletionException;
@@ -35,82 +35,106 @@
3535

3636
class DefaultS3AsyncClientSdkExtensionTest {
3737

38-
private S3AsyncClient s3;
39-
private DefaultS3AsyncClientSdkExtension extension;
38+
S3AsyncClient s3;
4039

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

4745
@Test
48-
void doesObjectExist_objectExists_returnsTrue() {
49-
when(s3.headObject(any(Consumer.class)))
50-
.thenReturn(CompletableFuture.completedFuture(HeadObjectResponse.builder().build()));
51-
assertThat(extension.doesObjectExist("bucket", "key").join()).isTrue();
46+
void doesObjectExist_200_returnsTrue() {
47+
stubHeadObject(CompletableFuture.completedFuture(HeadObjectResponse.builder().build()));
48+
assertThat(s3.doesObjectExist("foo", "bar").join()).isTrue();
5249
}
5350

5451
@Test
55-
void doesObjectExist_noSuchKey_returnsFalse() {
52+
void doesObjectExist_404_returnsFalse() {
5653
CompletableFuture<HeadObjectResponse> future = new CompletableFuture<>();
5754
future.completeExceptionally(NoSuchKeyException.builder().build());
58-
when(s3.headObject(any(Consumer.class))).thenReturn(future);
59-
assertThat(extension.doesObjectExist("bucket", "key").join()).isFalse();
55+
stubHeadObject(future);
56+
assertThat(s3.doesObjectExist("foo", "bar").join()).isFalse();
6057
}
6158

6259
@Test
63-
void doesObjectExist_otherException_propagates() {
60+
void doesObjectExist_403_propagatesException() {
6461
S3Exception forbidden = (S3Exception) S3Exception.builder().statusCode(403).message("Forbidden").build();
6562
CompletableFuture<HeadObjectResponse> future = new CompletableFuture<>();
6663
future.completeExceptionally(forbidden);
67-
when(s3.headObject(any(Consumer.class))).thenReturn(future);
68-
assertThatThrownBy(() -> extension.doesObjectExist("bucket", "key").join())
64+
stubHeadObject(future);
65+
assertThatThrownBy(() -> s3.doesObjectExist("foo", "bar").join())
6966
.isInstanceOf(CompletionException.class)
7067
.hasCause(forbidden);
7168
}
7269

7370
@Test
74-
void doesBucketExist_bucketExists_returnsTrue() {
75-
when(s3.headBucket(any(Consumer.class)))
76-
.thenReturn(CompletableFuture.completedFuture(HeadBucketResponse.builder().build()));
77-
assertThat(extension.doesBucketExist("bucket").join()).isTrue();
71+
void doesBucketExist_200_returnsTrue() {
72+
stubHeadBucket(CompletableFuture.completedFuture(HeadBucketResponse.builder().build()));
73+
assertThat(s3.doesBucketExist("foo").join()).isTrue();
7874
}
7975

8076
@Test
81-
void doesBucketExist_noSuchBucket_returnsFalse() {
77+
void doesBucketExist_404_returnsFalse() {
8278
CompletableFuture<HeadBucketResponse> future = new CompletableFuture<>();
8379
future.completeExceptionally(NoSuchBucketException.builder().build());
84-
when(s3.headBucket(any(Consumer.class))).thenReturn(future);
85-
assertThat(extension.doesBucketExist("bucket").join()).isFalse();
80+
stubHeadBucket(future);
81+
assertThat(s3.doesBucketExist("foo").join()).isFalse();
8682
}
8783

8884
@Test
89-
void doesBucketExist_otherException_propagates() {
85+
void doesBucketExist_403_propagatesException() {
9086
S3Exception forbidden = (S3Exception) S3Exception.builder().statusCode(403).message("Forbidden").build();
9187
CompletableFuture<HeadBucketResponse> future = new CompletableFuture<>();
9288
future.completeExceptionally(forbidden);
93-
when(s3.headBucket(any(Consumer.class))).thenReturn(future);
94-
assertThatThrownBy(() -> extension.doesBucketExist("bucket").join())
89+
stubHeadBucket(future);
90+
assertThatThrownBy(() -> s3.doesBucketExist("foo").join())
9591
.isInstanceOf(CompletionException.class)
9692
.hasCause(forbidden);
9793
}
9894

95+
// Validation tests
96+
97+
@Test
98+
void doesBucketExist_nullBucket_fails() {
99+
assertThatThrownBy(() -> s3.doesBucketExist(null).join())
100+
.hasCauseInstanceOf(NullPointerException.class);
101+
}
102+
103+
@Test
104+
void doesBucketExist_emptyBucket_fails() {
105+
assertThatThrownBy(() -> s3.doesBucketExist("").join())
106+
.hasCauseInstanceOf(IllegalArgumentException.class);
107+
}
108+
99109
@Test
100-
void doesBucketExist_nullBucket_failsFuture() {
101-
CompletableFuture<Boolean> result = extension.doesBucketExist(null);
102-
assertThatThrownBy(result::join).hasCauseInstanceOf(NullPointerException.class);
110+
void doesObjectExist_nullBucket_fails() {
111+
assertThatThrownBy(() -> s3.doesObjectExist(null, "key").join())
112+
.hasCauseInstanceOf(NullPointerException.class);
103113
}
104114

105115
@Test
106-
void doesObjectExist_nullBucket_failsFuture() {
107-
CompletableFuture<Boolean> result = extension.doesObjectExist(null, "key");
108-
assertThatThrownBy(result::join).hasCauseInstanceOf(NullPointerException.class);
116+
void doesObjectExist_emptyBucket_fails() {
117+
assertThatThrownBy(() -> s3.doesObjectExist("", "key").join())
118+
.hasCauseInstanceOf(IllegalArgumentException.class);
109119
}
110120

111121
@Test
112-
void doesObjectExist_nullKey_failsFuture() {
113-
CompletableFuture<Boolean> result = extension.doesObjectExist("bucket", null);
114-
assertThatThrownBy(result::join).hasCauseInstanceOf(NullPointerException.class);
122+
void doesObjectExist_nullKey_fails() {
123+
assertThatThrownBy(() -> s3.doesObjectExist("bucket", null).join())
124+
.hasCauseInstanceOf(NullPointerException.class);
125+
}
126+
127+
@Test
128+
void doesObjectExist_emptyKey_fails() {
129+
assertThatThrownBy(() -> s3.doesObjectExist("bucket", "").join())
130+
.hasCauseInstanceOf(IllegalArgumentException.class);
131+
}
132+
133+
private void stubHeadBucket(CompletableFuture<HeadBucketResponse> result) {
134+
doReturn(result).when(s3).headBucket(any(Consumer.class));
135+
}
136+
137+
private void stubHeadObject(CompletableFuture<HeadObjectResponse> result) {
138+
doReturn(result).when(s3).headObject(any(Consumer.class));
115139
}
116140
}

0 commit comments

Comments
 (0)