forked from awspring/spring-cloud-aws
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSnsAutoConfigurationTest.java
More file actions
201 lines (168 loc) · 6.74 KB
/
SnsAutoConfigurationTest.java
File metadata and controls
201 lines (168 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Copyright 2013-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.awspring.cloud.autoconfigure.sns;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import io.awspring.cloud.autoconfigure.ConfiguredAwsClient;
import io.awspring.cloud.autoconfigure.core.AwsAutoConfiguration;
import io.awspring.cloud.autoconfigure.core.CredentialsProviderAutoConfiguration;
import io.awspring.cloud.autoconfigure.core.RegionProviderAutoConfiguration;
import io.awspring.cloud.sns.core.SnsOperations;
import io.awspring.cloud.sns.core.SnsTemplate;
import io.awspring.cloud.sns.core.TopicArnResolver;
import io.awspring.cloud.sns.core.batch.SnsBatchTemplate;
import io.awspring.cloud.sns.core.batch.converter.SnsMessageConverter;
import io.awspring.cloud.sns.core.batch.executor.BatchExecutionStrategy;
import io.awspring.cloud.sns.sms.SnsSmsOperations;
import io.awspring.cloud.sns.sms.SnsSmsTemplate;
import java.net.URI;
import org.junit.jupiter.api.Test;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import software.amazon.awssdk.arns.Arn;
import software.amazon.awssdk.services.sns.SnsClient;
/**
* Tests for class {@link io.awspring.cloud.autoconfigure.sns.SnsAutoConfiguration}.
*
* @author Matej Nedic
* @author Mariusz Sondecki
*/
class SnsAutoConfigurationTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withPropertyValues("spring.cloud.aws.region.static:eu-west-1")
.withConfiguration(AutoConfigurations.of(RegionProviderAutoConfiguration.class,
CredentialsProviderAutoConfiguration.class, SnsAutoConfiguration.class,
AwsAutoConfiguration.class));
@Test
void snsAutoConfigurationIsDisabled() {
this.contextRunner.withPropertyValues("spring.cloud.aws.sns.enabled:false")
.run(context -> assertThat(context).doesNotHaveBean(SnsClient.class));
}
@Test
void snsAutoConfigurationIsEnabled() {
this.contextRunner.withPropertyValues("spring.cloud.aws.sns.enabled:true").run(context -> {
assertThat(context).hasSingleBean(SnsClient.class);
assertThat(context).hasSingleBean(SnsTemplate.class);
assertThat(context).hasSingleBean(SnsSmsTemplate.class);
assertThat(context).hasBean("snsWebMvcConfigurer");
ConfiguredAwsClient client = new ConfiguredAwsClient(context.getBean(SnsClient.class));
assertThat(client.getEndpoint()).isEqualTo(URI.create("https://sns.eu-west-1.amazonaws.com"));
});
}
@Test
void withCustomEndpoint() {
this.contextRunner.withPropertyValues("spring.cloud.aws.sns.endpoint:http://localhost:8090").run(context -> {
assertThat(context).hasSingleBean(SnsTemplate.class);
assertThat(context).hasBean("snsWebMvcConfigurer");
ConfiguredAwsClient client = new ConfiguredAwsClient(context.getBean(SnsClient.class));
assertThat(client.getEndpoint()).isEqualTo(URI.create("http://localhost:8090"));
assertThat(client.isEndpointOverridden()).isTrue();
});
}
@Test
void customTopicArnResolverCanBeConfigured() {
this.contextRunner.withUserConfiguration(CustomTopicArnResolverConfiguration.class)
.run(context -> assertThat(context).hasSingleBean(CustomTopicArnResolver.class));
}
@Test
void doesNotConfigureArgumentResolversWhenSpringWebNotOnTheClasspath() {
this.contextRunner.withClassLoader(new FilteredClassLoader(WebMvcConfigurer.class)).run(context -> {
assertThat(context).hasSingleBean(SnsClient.class);
assertThat(context).hasSingleBean(SnsTemplate.class);
assertThat(context).hasSingleBean(SnsBatchTemplate.class);
assertThat(context).hasSingleBean(SnsMessageConverter.class);
assertThat(context).hasSingleBean(BatchExecutionStrategy.class);
assertThat(context).hasSingleBean(SnsSmsTemplate.class);
assertThat(context).doesNotHaveBean("snsWebMvcConfigurer");
});
}
@Test
void smsSnsOperationsBeanCanBeOverwritten() {
this.contextRunner.withUserConfiguration(CustomSmsOperations.class).run(context -> {
assertThat(context.getBean("customSmsOperations", SnsSmsOperations.class)).isNotNull();
assertThat(context).doesNotHaveBean(SnsSmsTemplate.class);
});
}
@Test
void bothTemplatesAndOperationsAreInjectable() {
this.contextRunner.withUserConfiguration(InjectingTemplatesConfiguration.class).run(context -> {
assertThat(context.isRunning()).isTrue();
});
}
@Test
void customChannelInterceptorCanBeConfigured() {
this.contextRunner.withUserConfiguration(CustomChannelInterceptorConfiguration.class)
.run(context -> assertThat(context).hasSingleBean(CustomChannelInterceptor.class));
}
@Configuration(proxyBeanMethods = false)
static class CustomTopicArnResolverConfiguration {
@Bean
TopicArnResolver customS3OutputStreamProvider() {
return new CustomTopicArnResolver();
}
}
static class CustomTopicArnResolver implements TopicArnResolver {
@Override
public Arn resolveTopicArn(String topicName) {
return Arn.builder().build();
}
}
@Configuration(proxyBeanMethods = false)
static class CustomSmsOperations {
@Bean
SnsSmsOperations customSmsOperations() {
return mock(SnsSmsOperations.class);
}
}
@Configuration(proxyBeanMethods = false)
static class InjectingTemplatesConfiguration {
@Bean
ApplicationRunner runner1(SnsTemplate snsTemplate) {
return args -> {
};
}
@Bean
ApplicationRunner runner2(SnsOperations snsOperations) {
return args -> {
};
}
@Bean
ApplicationRunner runner3(SnsSmsTemplate snsSmsTemplate) {
return args -> {
};
}
@Bean
ApplicationRunner runner4(SnsSmsOperations snsSmsOperations) {
return args -> {
};
}
}
@Configuration(proxyBeanMethods = false)
static class CustomChannelInterceptorConfiguration {
@Bean
ChannelInterceptor customChannelInterceptor() {
return new CustomChannelInterceptor();
}
}
static class CustomChannelInterceptor implements ChannelInterceptor {
}
}