|
18 | 18 | import static org.assertj.core.api.Assertions.assertThat; |
19 | 19 | import static org.mockito.Mockito.mock; |
20 | 20 |
|
| 21 | +import com.fasterxml.jackson.databind.ObjectMapper; |
21 | 22 | import io.awspring.cloud.autoconfigure.ConfiguredAwsClient; |
22 | 23 | import io.awspring.cloud.autoconfigure.core.AwsAutoConfiguration; |
23 | 24 | import io.awspring.cloud.autoconfigure.core.CredentialsProviderAutoConfiguration; |
24 | 25 | import io.awspring.cloud.autoconfigure.core.RegionProviderAutoConfiguration; |
25 | 26 | import io.awspring.cloud.s3.DiskBufferingS3OutputStreamProvider; |
26 | 27 | import io.awspring.cloud.s3.ObjectMetadata; |
| 28 | +import io.awspring.cloud.s3.S3ObjectConverter; |
27 | 29 | import io.awspring.cloud.s3.S3OutputStream; |
28 | 30 | import io.awspring.cloud.s3.S3OutputStreamProvider; |
| 31 | +import io.awspring.cloud.s3.S3Template; |
29 | 32 | import io.awspring.cloud.s3.crossregion.CrossRegionS3Client; |
30 | 33 | import java.io.IOException; |
31 | 34 | import java.net.URI; |
| 35 | +import org.junit.jupiter.api.Nested; |
32 | 36 | import org.junit.jupiter.api.Test; |
33 | 37 | import org.springframework.boot.autoconfigure.AutoConfigurations; |
34 | 38 | import org.springframework.boot.test.context.FilteredClassLoader; |
35 | 39 | import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
36 | 40 | import org.springframework.context.annotation.Bean; |
37 | 41 | import org.springframework.context.annotation.Configuration; |
38 | 42 | import org.springframework.lang.Nullable; |
| 43 | +import org.springframework.test.util.ReflectionTestUtils; |
39 | 44 | import software.amazon.awssdk.services.s3.S3Client; |
40 | 45 | import software.amazon.awssdk.services.s3.S3ClientBuilder; |
41 | 46 |
|
@@ -82,68 +87,141 @@ void autoconfigurationIsNotTriggeredWhenS3ModuleIsNotOnClasspath() { |
82 | 87 | }); |
83 | 88 | } |
84 | 89 |
|
85 | | - @Test |
86 | | - void byDefaultCreatesCrossRegionS3Client() { |
87 | | - this.contextRunner |
88 | | - .run(context -> assertThat(context).getBean(S3Client.class).isInstanceOf(CrossRegionS3Client.class)); |
89 | | - } |
| 90 | + @Nested |
| 91 | + class S3ClientTests { |
| 92 | + @Test |
| 93 | + void byDefaultCreatesCrossRegionS3Client() { |
| 94 | + contextRunner.run( |
| 95 | + context -> assertThat(context).getBean(S3Client.class).isInstanceOf(CrossRegionS3Client.class)); |
| 96 | + } |
90 | 97 |
|
91 | | - @Test |
92 | | - void s3ClientCanBeOverwritten() { |
93 | | - this.contextRunner.withUserConfiguration(CustomS3ClientConfiguration.class).run(context -> { |
94 | | - assertThat(context).hasSingleBean(S3Client.class); |
95 | | - assertThat(context).getBean(S3Client.class).isNotInstanceOf(CrossRegionS3Client.class); |
96 | | - }); |
97 | | - } |
| 98 | + @Test |
| 99 | + void s3ClientCanBeOverwritten() { |
| 100 | + contextRunner.withUserConfiguration(CustomS3ClientConfiguration.class).run(context -> { |
| 101 | + assertThat(context).hasSingleBean(S3Client.class); |
| 102 | + assertThat(context).getBean(S3Client.class).isNotInstanceOf(CrossRegionS3Client.class); |
| 103 | + }); |
| 104 | + } |
98 | 105 |
|
99 | | - @Test |
100 | | - void byDefaultCreatesDiskBufferingS3OutputStreamProvider() { |
101 | | - this.contextRunner.run(context -> assertThat(context).hasSingleBean(DiskBufferingS3OutputStreamProvider.class)); |
| 106 | + @Test |
| 107 | + void createsStandardClientWhenCrossRegionModuleIsNotInClasspath() { |
| 108 | + contextRunner.withClassLoader(new FilteredClassLoader(CrossRegionS3Client.class)).run(context -> { |
| 109 | + assertThat(context).doesNotHaveBean(CrossRegionS3Client.class); |
| 110 | + assertThat(context).hasSingleBean(S3Client.class); |
| 111 | + }); |
| 112 | + } |
102 | 113 | } |
103 | 114 |
|
104 | | - @Test |
105 | | - void customS3OutputStreamProviderCanBeConfigured() { |
106 | | - this.contextRunner.withUserConfiguration(CustomS3OutputStreamProviderConfiguration.class) |
107 | | - .run(context -> assertThat(context).hasSingleBean(CustomS3OutputStreamProvider.class)); |
| 115 | + @Nested |
| 116 | + class OutputStreamProviderTests { |
| 117 | + @Test |
| 118 | + void byDefaultCreatesDiskBufferingS3OutputStreamProvider() { |
| 119 | + contextRunner.run(context -> assertThat(context).hasSingleBean(DiskBufferingS3OutputStreamProvider.class)); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void customS3OutputStreamProviderCanBeConfigured() { |
| 124 | + contextRunner.withUserConfiguration(CustomS3OutputStreamProviderConfiguration.class) |
| 125 | + .run(context -> assertThat(context).hasSingleBean(CustomS3OutputStreamProvider.class)); |
| 126 | + } |
108 | 127 | } |
109 | 128 |
|
110 | | - @Test |
111 | | - void createsStandardClientWhenCrossRegionModuleIsNotInClasspath() { |
112 | | - this.contextRunner.withClassLoader(new FilteredClassLoader(CrossRegionS3Client.class)).run(context -> { |
113 | | - assertThat(context).doesNotHaveBean(CrossRegionS3Client.class); |
114 | | - assertThat(context).hasSingleBean(S3Client.class); |
115 | | - }); |
| 129 | + @Nested |
| 130 | + class EndpointConfigurationTests { |
| 131 | + @Test |
| 132 | + void withCustomEndpoint() { |
| 133 | + contextRunner.withPropertyValues("spring.cloud.aws.s3.endpoint:http://localhost:8090").run(context -> { |
| 134 | + S3ClientBuilder builder = context.getBean(S3ClientBuilder.class); |
| 135 | + ConfiguredAwsClient client = new ConfiguredAwsClient(builder.build()); |
| 136 | + assertThat(client.getEndpoint()).isEqualTo(URI.create("http://localhost:8090")); |
| 137 | + assertThat(client.isEndpointOverridden()).isTrue(); |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + void withCustomGlobalEndpoint() { |
| 143 | + contextRunner.withPropertyValues("spring.cloud.aws.endpoint:http://localhost:8090").run(context -> { |
| 144 | + S3ClientBuilder builder = context.getBean(S3ClientBuilder.class); |
| 145 | + ConfiguredAwsClient client = new ConfiguredAwsClient(builder.build()); |
| 146 | + assertThat(client.getEndpoint()).isEqualTo(URI.create("http://localhost:8090")); |
| 147 | + assertThat(client.isEndpointOverridden()).isTrue(); |
| 148 | + }); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + void withCustomGlobalEndpointAndS3Endpoint() { |
| 153 | + contextRunner.withPropertyValues("spring.cloud.aws.endpoint:http://localhost:8090", |
| 154 | + "spring.cloud.aws.s3.endpoint:http://localhost:9999").run(context -> { |
| 155 | + S3ClientBuilder builder = context.getBean(S3ClientBuilder.class); |
| 156 | + ConfiguredAwsClient client = new ConfiguredAwsClient(builder.build()); |
| 157 | + assertThat(client.getEndpoint()).isEqualTo(URI.create("http://localhost:9999")); |
| 158 | + assertThat(client.isEndpointOverridden()).isTrue(); |
| 159 | + }); |
| 160 | + } |
116 | 161 | } |
117 | 162 |
|
118 | | - @Test |
119 | | - void withCustomEndpoint() { |
120 | | - this.contextRunner.withPropertyValues("spring.cloud.aws.s3.endpoint:http://localhost:8090").run(context -> { |
121 | | - S3ClientBuilder builder = context.getBean(S3ClientBuilder.class); |
122 | | - ConfiguredAwsClient client = new ConfiguredAwsClient(builder.build()); |
123 | | - assertThat(client.getEndpoint()).isEqualTo(URI.create("http://localhost:8090")); |
124 | | - assertThat(client.isEndpointOverridden()).isTrue(); |
125 | | - }); |
| 163 | + @Nested |
| 164 | + class S3TemplateAutoConfigurationTests { |
| 165 | + |
| 166 | + @Test |
| 167 | + void withJacksonOnClasspathAutoconfiguresObjectConverter() { |
| 168 | + contextRunner.run(context -> { |
| 169 | + assertThat(context).hasSingleBean(S3ObjectConverter.class); |
| 170 | + assertThat(context).hasSingleBean(S3Template.class); |
| 171 | + }); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + void withoutJacksonOnClasspathDoesNotConfigureObjectConverter() { |
| 176 | + contextRunner.withClassLoader(new FilteredClassLoader(ObjectMapper.class)).run(context -> { |
| 177 | + assertThat(context).doesNotHaveBean(S3ObjectConverter.class); |
| 178 | + assertThat(context).doesNotHaveBean(S3Template.class); |
| 179 | + }); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + void usesCustomObjectMapperBean() { |
| 184 | + contextRunner.withUserConfiguration(CustomJacksonConfiguration.class).run(context -> { |
| 185 | + S3ObjectConverter bean = context.getBean(S3ObjectConverter.class); |
| 186 | + ObjectMapper objectMapper = (ObjectMapper) ReflectionTestUtils.getField(bean, "objectMapper"); |
| 187 | + assertThat(objectMapper).isEqualTo(context.getBean("customObjectMapper")); |
| 188 | + }); |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + void usesCustomS3ObjectConverter() { |
| 193 | + contextRunner |
| 194 | + .withUserConfiguration(CustomJacksonConfiguration.class, CustomS3ObjectConverterConfiguration.class) |
| 195 | + .run(context -> { |
| 196 | + S3ObjectConverter s3ObjectConverter = context.getBean(S3ObjectConverter.class); |
| 197 | + S3ObjectConverter customS3ObjectConverter = (S3ObjectConverter) context |
| 198 | + .getBean("customS3ObjectConverter"); |
| 199 | + assertThat(s3ObjectConverter).isEqualTo(customS3ObjectConverter); |
| 200 | + |
| 201 | + S3Template s3Template = context.getBean(S3Template.class); |
| 202 | + |
| 203 | + S3ObjectConverter converter = (S3ObjectConverter) ReflectionTestUtils.getField(s3Template, |
| 204 | + "s3ObjectConverter"); |
| 205 | + assertThat(converter).isEqualTo(customS3ObjectConverter); |
| 206 | + }); |
| 207 | + } |
126 | 208 | } |
127 | 209 |
|
128 | | - @Test |
129 | | - void withCustomGlobalEndpoint() { |
130 | | - this.contextRunner.withPropertyValues("spring.cloud.aws.endpoint:http://localhost:8090").run(context -> { |
131 | | - S3ClientBuilder builder = context.getBean(S3ClientBuilder.class); |
132 | | - ConfiguredAwsClient client = new ConfiguredAwsClient(builder.build()); |
133 | | - assertThat(client.getEndpoint()).isEqualTo(URI.create("http://localhost:8090")); |
134 | | - assertThat(client.isEndpointOverridden()).isTrue(); |
135 | | - }); |
| 210 | + @Configuration(proxyBeanMethods = false) |
| 211 | + static class CustomJacksonConfiguration { |
| 212 | + @Bean |
| 213 | + ObjectMapper customObjectMapper() { |
| 214 | + return new ObjectMapper(); |
| 215 | + } |
136 | 216 | } |
137 | 217 |
|
138 | | - @Test |
139 | | - void withCustomGlobalEndpointAndS3Endpoint() { |
140 | | - this.contextRunner.withPropertyValues("spring.cloud.aws.endpoint:http://localhost:8090", |
141 | | - "spring.cloud.aws.s3.endpoint:http://localhost:9999").run(context -> { |
142 | | - S3ClientBuilder builder = context.getBean(S3ClientBuilder.class); |
143 | | - ConfiguredAwsClient client = new ConfiguredAwsClient(builder.build()); |
144 | | - assertThat(client.getEndpoint()).isEqualTo(URI.create("http://localhost:9999")); |
145 | | - assertThat(client.isEndpointOverridden()).isTrue(); |
146 | | - }); |
| 218 | + @Configuration(proxyBeanMethods = false) |
| 219 | + static class CustomS3ObjectConverterConfiguration { |
| 220 | + |
| 221 | + @Bean |
| 222 | + S3ObjectConverter customS3ObjectConverter() { |
| 223 | + return mock(S3ObjectConverter.class); |
| 224 | + } |
147 | 225 | } |
148 | 226 |
|
149 | 227 | @Configuration(proxyBeanMethods = false) |
@@ -172,7 +250,6 @@ static class CustomS3OutputStreamProvider implements S3OutputStreamProvider { |
172 | 250 | public S3OutputStream create(String bucket, String key, @Nullable ObjectMetadata metadata) throws IOException { |
173 | 251 | return null; |
174 | 252 | } |
175 | | - |
176 | 253 | } |
177 | 254 |
|
178 | 255 | } |
0 commit comments