1616
1717package org .springframework .cloud .vault .util ;
1818
19- import org .springframework .boot .WebApplicationType ;
20- import org .springframework .boot .builder .SpringApplicationBuilder ;
21- import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
22- import org .springframework .cloud .vault .config .VaultProperties .AuthenticationMethod ;
23- import org .springframework .context .ConfigurableApplicationContext ;
24-
2519import java .util .ArrayList ;
2620import java .util .Arrays ;
2721import java .util .LinkedHashMap ;
2822import java .util .List ;
2923import java .util .Map ;
3024import java .util .function .Consumer ;
3125
26+ import org .springframework .boot .WebApplicationType ;
27+ import org .springframework .boot .builder .SpringApplicationBuilder ;
28+ import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
29+ import org .springframework .cloud .vault .config .VaultProperties .AuthenticationMethod ;
30+ import org .springframework .context .ConfigurableApplicationContext ;
31+
3232/**
3333 * A utility class built on top of {@link SpringApplicationBuilder} that follows Spring
3434 * Boot’s testing patterns but designed for Spring Cloud Vault integration tests.
3535 *
3636 * <p>
3737 * Its goal is to eliminate repetitive boilerplate particularly when repeatedly
38- * configuring:
39- * </p>
40- *
41- * <ul>
42- * <li>Bootstrap mode</li>
43- * <li>Reactive vs. non-reactive modes</li>
44- * <li>Environment and test classes</li>
45- * <li>Vault-specific properties</li>
46- * </ul>
38+ * configuring startup properties.
4739 *
4840 * @author Issam EL-ATIF
49- * @since 5.0.1
41+ * @author Mark Paluch
5042 * @see SpringApplicationBuilder
5143 * @see ApplicationContextRunner
5244 */
53- public class VaultTestContextRunner {
45+ public final class VaultTestContextRunner {
5446
5547 private final List <Class <?>> configurationClasses = new ArrayList <>();
5648
5749 private final Map <String , Object > properties = new LinkedHashMap <>();
5850
51+ private final TestSettings testSettings = new TestSettings ();
52+
5953 private VaultTestContextRunner (Map <String , Object > properties ) {
6054 this .properties .putAll (properties );
6155 }
6256
63- public static VaultTestContextRunner of (AuthenticationMethod authenticationMethod ) {
64- Map <String , Object > authProperties = new LinkedHashMap <>();
65- switch (authenticationMethod ) {
66- case CERT -> {
67- authProperties .put ("spring.cloud.vault.authentication" , "cert" );
68- authProperties .put ("spring.cloud.vault.ssl.key-store" , "file:../work/client-cert.jks" );
69- authProperties .put ("spring.cloud.vault.ssl.key-store-password" , "changeit" );
70- }
71- case KUBERNETES -> {
72- authProperties .put ("spring.cloud.vault.authentication" , "kubernetes" );
73- authProperties .put ("spring.cloud.vault.kubernetes.service-account-token-file" ,
74- "../work/minikube/hello-minikube-token" );
75- }
76- }
57+ /**
58+ * Create a {@code VaultTestContextRunner} with default TLS configuration.
59+ * @return the {@link VaultTestContextRunner}.
60+ */
61+ public static VaultTestContextRunner create () {
62+
63+ Map <String , Object > configurationProperties = new LinkedHashMap <>();
64+ configurationProperties .put ("spring.cloud.vault.ssl.key-store" , "file:../work/client-cert.jks" );
65+ configurationProperties .put ("spring.cloud.vault.ssl.key-store-password" , "changeit" );
7766
78- return new VaultTestContextRunner (authProperties );
67+ return new VaultTestContextRunner (configurationProperties );
7968 }
8069
81- public VaultTestContextRunner configurations (Class <?>... configClasses ) {
82- this .configurationClasses .addAll (Arrays .asList (configClasses ));
83- return this ;
70+ /**
71+ * Create a {@code VaultTestContextRunner} with default TLS configuration and using
72+ * the {@code testClass} to set the application name.
73+ * @return the {@link VaultTestContextRunner}.
74+ */
75+ public static VaultTestContextRunner of (Class <?> testClass ) {
76+ return create ().testClass (testClass );
8477 }
8578
86- public VaultTestContextRunner property (String key , Object value ) {
87- this .properties .put (key , value );
88- return this ;
79+ /**
80+ * Configure an authentication method to be used.
81+ * @param authenticationMethod the authentication method to use.
82+ * @return this builder.
83+ */
84+ public VaultTestContextRunner auth (AuthenticationMethod authenticationMethod ) {
85+ return property ("spring.cloud.vault.authentication" , authenticationMethod .name ());
8986 }
9087
88+ /**
89+ * Configure the application name based on the given test class.
90+ * @param testClass the test class.
91+ * @return this builder.
92+ */
9193 public VaultTestContextRunner testClass (Class <?> testClass ) {
92- this .properties .put ("spring.cloud.vault.application-name" , testClass .getSimpleName ());
94+ return property ("spring.cloud.vault.application-name" , testClass .getSimpleName ());
95+ }
96+
97+ /**
98+ * Configure configuration classes to be added.
99+ * @param configClasses configuration classes to add.
100+ * @return this builder.
101+ */
102+ public VaultTestContextRunner configurations (Class <?>... configClasses ) {
103+ this .configurationClasses .addAll (Arrays .asList (configClasses ));
93104 return this ;
94105 }
95106
96- public VaultTestContextRunner bootstrap (boolean bootstrap ) {
97- this .properties .put ("spring.cloud.bootstrap.enabled" , bootstrap );
107+ /**
108+ * Set a property to be used in the test context.
109+ * @param key the property key.
110+ * @param value the property value.
111+ * @return this builder.
112+ */
113+ public VaultTestContextRunner property (String key , Object value ) {
114+ this .properties .put (key , value );
98115 return this ;
99116 }
100117
101- public VaultTestContextRunner reactive (boolean reactive ) {
102- this .properties .put ("spring.cloud.vault.reactive.enabled" , reactive );
118+ /**
119+ * Provides access to {@link TestSettings} with the possibility to update test
120+ * settings.
121+ * @param settingsConsumer a function that consumes the test settings.
122+ * @return this builder.
123+ */
124+ public VaultTestContextRunner settings (Consumer <TestSettings > settingsConsumer ) {
125+ settingsConsumer .accept (this .testSettings );
103126 return this ;
104127 }
105128
129+ /**
130+ * Run the application and provide access to the application context through the
131+ * {@code consumer}.
132+ * @param consumer consumes the application context.
133+ */
106134 public void run (Consumer <ConfigurableApplicationContext > consumer ) {
135+
136+ property ("spring.cloud.bootstrap.enabled" , this .testSettings .bootstrap );
137+ property ("spring.cloud.vault.reactive.enabled" , this .testSettings .reactive );
138+
107139 SpringApplicationBuilder builder = new SpringApplicationBuilder ();
108- builder .sources (configurationClasses .toArray (new Class <?>[0 ]))
140+ builder .sources (this . configurationClasses .toArray (new Class <?>[0 ]))
109141 .web (WebApplicationType .NONE )
110142 .properties (this .properties );
111143
@@ -114,4 +146,49 @@ public void run(Consumer<ConfigurableApplicationContext> consumer) {
114146 }
115147 }
116148
149+ /**
150+ * Common settings used in tests.
151+ */
152+ public class TestSettings {
153+
154+ private boolean bootstrap = false ;
155+
156+ private boolean reactive = false ;
157+
158+ /**
159+ * Enable Spring Cloud bootstrap context usage (disabled by default).
160+ * @return this settings builder.
161+ */
162+ public TestSettings bootstrap () {
163+ return bootstrap (true );
164+ }
165+
166+ /**
167+ * Enable or disable Spring Cloud bootstrap context usage (disabled by default).
168+ * @return this settings builder.
169+ */
170+ public TestSettings bootstrap (boolean bootstrap ) {
171+ this .bootstrap = bootstrap ;
172+ return this ;
173+ }
174+
175+ /**
176+ * Enable reactive usage (disabled by default).
177+ * @return this settings builder.
178+ */
179+ public TestSettings reactive () {
180+ return reactive (true );
181+ }
182+
183+ /**
184+ * Enable or disable reactive usage (disabled by default).
185+ * @return this settings builder.
186+ */
187+ public TestSettings reactive (boolean reactive ) {
188+ this .reactive = reactive ;
189+ return this ;
190+ }
191+
192+ }
193+
117194}
0 commit comments