|
18 | 18 | import static org.assertj.core.api.Assertions.assertThat; |
19 | 19 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
20 | 20 | 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; |
23 | 23 |
|
24 | 24 | import java.util.concurrent.CompletableFuture; |
25 | 25 | import java.util.concurrent.CompletionException; |
|
35 | 35 |
|
36 | 36 | class DefaultS3AsyncClientSdkExtensionTest { |
37 | 37 |
|
38 | | - private S3AsyncClient s3; |
39 | | - private DefaultS3AsyncClientSdkExtension extension; |
| 38 | + S3AsyncClient s3; |
40 | 39 |
|
41 | 40 | @BeforeEach |
42 | 41 | void setUp() { |
43 | | - s3 = mock(S3AsyncClient.class); |
44 | | - extension = new DefaultS3AsyncClientSdkExtension(s3); |
| 42 | + s3 = spy(S3AsyncClient.class); |
45 | 43 | } |
46 | 44 |
|
47 | 45 | @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(); |
52 | 49 | } |
53 | 50 |
|
54 | 51 | @Test |
55 | | - void doesObjectExist_noSuchKey_returnsFalse() { |
| 52 | + void doesObjectExist_404_returnsFalse() { |
56 | 53 | CompletableFuture<HeadObjectResponse> future = new CompletableFuture<>(); |
57 | 54 | 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(); |
60 | 57 | } |
61 | 58 |
|
62 | 59 | @Test |
63 | | - void doesObjectExist_otherException_propagates() { |
| 60 | + void doesObjectExist_403_propagatesException() { |
64 | 61 | S3Exception forbidden = (S3Exception) S3Exception.builder().statusCode(403).message("Forbidden").build(); |
65 | 62 | CompletableFuture<HeadObjectResponse> future = new CompletableFuture<>(); |
66 | 63 | 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()) |
69 | 66 | .isInstanceOf(CompletionException.class) |
70 | 67 | .hasCause(forbidden); |
71 | 68 | } |
72 | 69 |
|
73 | 70 | @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(); |
78 | 74 | } |
79 | 75 |
|
80 | 76 | @Test |
81 | | - void doesBucketExist_noSuchBucket_returnsFalse() { |
| 77 | + void doesBucketExist_404_returnsFalse() { |
82 | 78 | CompletableFuture<HeadBucketResponse> future = new CompletableFuture<>(); |
83 | 79 | 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(); |
86 | 82 | } |
87 | 83 |
|
88 | 84 | @Test |
89 | | - void doesBucketExist_otherException_propagates() { |
| 85 | + void doesBucketExist_403_propagatesException() { |
90 | 86 | S3Exception forbidden = (S3Exception) S3Exception.builder().statusCode(403).message("Forbidden").build(); |
91 | 87 | CompletableFuture<HeadBucketResponse> future = new CompletableFuture<>(); |
92 | 88 | 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()) |
95 | 91 | .isInstanceOf(CompletionException.class) |
96 | 92 | .hasCause(forbidden); |
97 | 93 | } |
98 | 94 |
|
| 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 | + |
99 | 109 | @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); |
103 | 113 | } |
104 | 114 |
|
105 | 115 | @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); |
109 | 119 | } |
110 | 120 |
|
111 | 121 | @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)); |
115 | 139 | } |
116 | 140 | } |
0 commit comments