|
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.doAnswer; |
| 22 | +import static org.mockito.Mockito.spy; |
23 | 23 |
|
24 | | -import java.util.function.Consumer; |
| 24 | +import java.util.function.Supplier; |
25 | 25 | import org.junit.jupiter.api.BeforeEach; |
26 | 26 | import org.junit.jupiter.api.Test; |
27 | 27 | import software.amazon.awssdk.services.s3.S3Client; |
|
35 | 35 |
|
36 | 36 | class DefaultS3ClientSdkExtensionTest { |
37 | 37 |
|
38 | | - private S3Client s3; |
39 | | - private DefaultS3ClientSdkExtension extension; |
| 38 | + S3Client s3; |
40 | 39 |
|
41 | 40 | @BeforeEach |
42 | 41 | void setUp() { |
43 | | - s3 = mock(S3Client.class); |
44 | | - extension = new DefaultS3ClientSdkExtension(s3); |
| 42 | + s3 = spy(S3Client.class); |
45 | 43 | } |
46 | 44 |
|
47 | 45 | @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); |
51 | 49 | } |
52 | 50 |
|
53 | 51 | @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); |
57 | 57 | } |
58 | 58 |
|
59 | 59 | @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); |
64 | 65 | } |
65 | 66 |
|
66 | 67 | @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); |
70 | 71 | } |
71 | 72 |
|
72 | 73 | @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); |
76 | 79 | } |
77 | 80 |
|
78 | 81 | @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); |
83 | 87 | } |
84 | 88 |
|
| 89 | + // Validation tests (not in prototype) |
| 90 | + |
85 | 91 | @Test |
86 | 92 | void doesBucketExist_nullBucket_throws() { |
87 | | - assertThatThrownBy(() -> extension.doesBucketExist(null)).isInstanceOf(NullPointerException.class); |
| 93 | + assertThatThrownBy(() -> s3.doesBucketExist(null)).isInstanceOf(NullPointerException.class); |
88 | 94 | } |
89 | 95 |
|
90 | 96 | @Test |
91 | 97 | void doesBucketExist_emptyBucket_throws() { |
92 | | - assertThatThrownBy(() -> extension.doesBucketExist("")).isInstanceOf(IllegalArgumentException.class); |
| 98 | + assertThatThrownBy(() -> s3.doesBucketExist("")).isInstanceOf(IllegalArgumentException.class); |
93 | 99 | } |
94 | 100 |
|
95 | 101 | @Test |
96 | 102 | void doesObjectExist_nullBucket_throws() { |
97 | | - assertThatThrownBy(() -> extension.doesObjectExist(null, "key")).isInstanceOf(NullPointerException.class); |
| 103 | + assertThatThrownBy(() -> s3.doesObjectExist(null, "key")).isInstanceOf(NullPointerException.class); |
98 | 104 | } |
99 | 105 |
|
100 | 106 | @Test |
101 | 107 | void doesObjectExist_emptyBucket_throws() { |
102 | | - assertThatThrownBy(() -> extension.doesObjectExist("", "key")).isInstanceOf(IllegalArgumentException.class); |
| 108 | + assertThatThrownBy(() -> s3.doesObjectExist("", "key")).isInstanceOf(IllegalArgumentException.class); |
103 | 109 | } |
104 | 110 |
|
105 | 111 | @Test |
106 | 112 | void doesObjectExist_nullKey_throws() { |
107 | | - assertThatThrownBy(() -> extension.doesObjectExist("bucket", null)).isInstanceOf(NullPointerException.class); |
| 113 | + assertThatThrownBy(() -> s3.doesObjectExist("bucket", null)).isInstanceOf(NullPointerException.class); |
108 | 114 | } |
109 | 115 |
|
110 | 116 | @Test |
111 | 117 | 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)); |
113 | 127 | } |
114 | 128 | } |
0 commit comments