Skip to content
This repository was archived by the owner on May 31, 2022. It is now read-only.

Commit 9fc0aad

Browse files
tsachevjzheaux
authored andcommitted
Use ApplicationContextRunner for autoconfiguration tests
1 parent 1ead042 commit 9fc0aad

File tree

1 file changed

+34
-55
lines changed

1 file changed

+34
-55
lines changed

spring-security-oauth2-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/authserver/AuthorizationServerTokenServicesConfigurationTests.java

Lines changed: 34 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -20,125 +20,104 @@
2020
import java.nio.file.Path;
2121
import java.util.stream.Collectors;
2222

23-
import org.junit.After;
2423
import org.junit.Rule;
2524
import org.junit.Test;
2625
import org.junit.rules.ExpectedException;
2726

2827
import org.springframework.beans.factory.UnsatisfiedDependencyException;
29-
import org.springframework.boot.WebApplicationType;
30-
import org.springframework.boot.builder.SpringApplicationBuilder;
28+
import org.springframework.boot.autoconfigure.AutoConfigurations;
3129
import org.springframework.boot.context.properties.EnableConfigurationProperties;
32-
import org.springframework.boot.test.util.TestPropertyValues;
33-
import org.springframework.context.ConfigurableApplicationContext;
30+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3431
import org.springframework.context.annotation.Configuration;
3532
import org.springframework.context.annotation.Import;
36-
import org.springframework.core.env.ConfigurableEnvironment;
37-
import org.springframework.core.env.StandardEnvironment;
3833
import org.springframework.core.io.ClassPathResource;
3934
import org.springframework.security.oauth2.provider.token.TokenStore;
4035
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
4136
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
4237

4338
import static org.assertj.core.api.Assertions.assertThat;
44-
import static org.assertj.core.api.Assertions.assertThatCode;
4539

4640
/**
4741
* Tests for {@link AuthorizationServerTokenServicesConfiguration}.
4842
*
4943
* @author Harold Li
5044
* @author Josh Cummings
45+
* @author Vladimir Tsanev
5146
* @since 2.1.0
5247
*/
5348
public class AuthorizationServerTokenServicesConfigurationTests {
5449

55-
private ConfigurableApplicationContext context;
56-
57-
private ConfigurableEnvironment environment = new StandardEnvironment();
50+
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
51+
.withConfiguration(AutoConfigurations.of(AuthorizationServerConfiguration.class));
5852

5953
@Rule
6054
public ExpectedException thrown = ExpectedException.none();
6155

62-
@After
63-
public void close() {
64-
if (this.context != null) {
65-
this.context.close();
66-
}
67-
}
68-
6956
@Test
7057
public void configureWhenPrivateKeyIsProvidedThenExposesJwtAccessTokenConverter() throws Exception {
7158
Path privateKeyPath = new ClassPathResource("key.private", this.getClass()).getFile().toPath();
7259
String privateKey = Files.readAllLines(privateKeyPath).stream().collect(Collectors.joining("\n"));
7360

74-
TestPropertyValues.of("security.oauth2.authorization.jwt.key-value=" + privateKey).applyTo(this.environment);
75-
this.context = new SpringApplicationBuilder(AuthorizationServerConfiguration.class)
76-
.environment(this.environment).web(WebApplicationType.NONE).run();
77-
78-
JwtAccessTokenConverter converter = this.context.getBean(JwtAccessTokenConverter.class);
79-
assertThat(converter.isPublic()).isTrue();
61+
this.contextRunner.withPropertyValues("security.oauth2.authorization.jwt.key-value=" + privateKey)
62+
.run(context -> {
63+
assertThat(context).getBean(JwtAccessTokenConverter.class)
64+
.satisfies(JwtAccessTokenConverter::isPublic);
65+
});
8066
}
8167

8268
@Test
8369
public void configureWhenKeyStoreIsProvidedThenExposesJwtTokenStore() {
84-
TestPropertyValues.of(
70+
this.contextRunner.withPropertyValues(
8571
"security.oauth2.authorization.jwt.key-store=classpath:"
8672
+ "org/springframework/boot/autoconfigure/security/oauth2/authserver/keystore.jks",
8773
"security.oauth2.authorization.jwt.key-store-password=changeme",
88-
"security.oauth2.authorization.jwt.key-alias=jwt").applyTo(this.environment);
89-
this.context = new SpringApplicationBuilder(AuthorizationServerConfiguration.class)
90-
.environment(this.environment).web(WebApplicationType.NONE).run();
91-
assertThat(this.context.getBeansOfType(TokenStore.class)).hasSize(1);
92-
assertThat(this.context.getBean(TokenStore.class)).isInstanceOf(JwtTokenStore.class);
74+
"security.oauth2.authorization.jwt.key-alias=jwt").run(context -> {
75+
assertThat(context.getBeansOfType(TokenStore.class)).hasSize(1);
76+
assertThat(context.getBean(TokenStore.class)).isInstanceOf(JwtTokenStore.class);
77+
});
9378
}
9479

9580
@Test
9681
public void configureWhenKeyStoreIsProvidedThenExposesJwtAccessTokenConverter() {
97-
TestPropertyValues.of(
82+
this.contextRunner.withPropertyValues(
9883
"security.oauth2.authorization.jwt.key-store=classpath:"
9984
+ "org/springframework/boot/autoconfigure/security/oauth2/authserver/keystore.jks",
10085
"security.oauth2.authorization.jwt.key-store-password=changeme",
101-
"security.oauth2.authorization.jwt.key-alias=jwt").applyTo(this.environment);
102-
this.context = new SpringApplicationBuilder(AuthorizationServerConfiguration.class)
103-
.environment(this.environment).web(WebApplicationType.NONE).run();
104-
assertThat(this.context.getBeansOfType(JwtAccessTokenConverter.class)).hasSize(1);
86+
"security.oauth2.authorization.jwt.key-alias=jwt").run(context -> {
87+
assertThat(context.getBeansOfType(JwtAccessTokenConverter.class)).hasSize(1);
88+
});
10589
}
10690

10791
@Test
10892
public void configureWhenKeyStoreIsProvidedWithKeyPasswordThenExposesJwtAccessTokenConverter() {
109-
TestPropertyValues.of(
93+
this.contextRunner.withPropertyValues(
11094
"security.oauth2.authorization.jwt.key-store=classpath:"
11195
+ "org/springframework/boot/autoconfigure/security/oauth2/authserver/keyhaspassword.jks",
11296
"security.oauth2.authorization.jwt.key-store-password=changeme",
11397
"security.oauth2.authorization.jwt.key-alias=jwt",
114-
"security.oauth2.authorization.jwt.key-password=password").applyTo(this.environment);
115-
this.context = new SpringApplicationBuilder(AuthorizationServerConfiguration.class)
116-
.environment(this.environment).web(WebApplicationType.NONE).run();
117-
assertThat(this.context.getBeansOfType(JwtAccessTokenConverter.class)).hasSize(1);
98+
"security.oauth2.authorization.jwt.key-password=password").run(context -> {
99+
assertThat(context.getBeansOfType(JwtAccessTokenConverter.class)).hasSize(1);
100+
});
118101
}
119102

120103
@Test
121104
public void configureWhenKeyStoreIsProvidedButNoAliasThenThrowsException() {
122-
TestPropertyValues.of(
105+
this.contextRunner.withPropertyValues(
123106
"security.oauth2.authorization.jwt.key-store=classpath:"
124107
+ "org/springframework/boot/autoconfigure/security/oauth2/authserver/keystore.jks",
125-
"security.oauth2.authorization.jwt.key-store-password=changeme").applyTo(this.environment);
126-
127-
assertThatCode(() -> new SpringApplicationBuilder(AuthorizationServerConfiguration.class)
128-
.environment(this.environment).web(WebApplicationType.NONE).run())
129-
.isInstanceOf(UnsatisfiedDependencyException.class);
108+
"security.oauth2.authorization.jwt.key-store-password=changeme").run(context -> {
109+
assertThat(context).getFailure().isInstanceOf(UnsatisfiedDependencyException.class);
110+
});
130111
}
131112

132113
@Test
133114
public void configureWhenKeyStoreIsProvidedButNoPasswordThenThrowsException() {
134-
TestPropertyValues.of(
135-
"security.oauth2.authorization.jwt.key-store=classpath:"
136-
+ "org/springframework/boot/autoconfigure/security/oauth2/authserver/keystore.jks",
137-
"security.oauth2.authorization.jwt.key-alias=jwt").applyTo(this.environment);
138-
139-
assertThatCode(() -> new SpringApplicationBuilder(AuthorizationServerConfiguration.class)
140-
.environment(this.environment).web(WebApplicationType.NONE).run())
141-
.isInstanceOf(UnsatisfiedDependencyException.class);
115+
this.contextRunner
116+
.withPropertyValues(
117+
"security.oauth2.authorization.jwt.key-store=classpath:"
118+
+ "org/springframework/boot/autoconfigure/security/oauth2/authserver/keystore.jks",
119+
"security.oauth2.authorization.jwt.key-alias=jwt")
120+
.run(context -> assertThat(context).getFailure().isInstanceOf(UnsatisfiedDependencyException.class));
142121
}
143122

144123
@Configuration
@@ -148,4 +127,4 @@ protected static class AuthorizationServerConfiguration {
148127

149128
}
150129

151-
}
130+
}

0 commit comments

Comments
 (0)