|
1 | 1 | package com.maciejwalkowiak.wiremock.spring;
|
2 | 2 |
|
3 |
| -import java.util.Arrays; |
4 |
| -import java.util.List; |
5 |
| -import java.util.Objects; |
| 3 | +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; |
6 | 4 |
|
7 | 5 | import com.github.tomakehurst.wiremock.WireMockServer;
|
8 | 6 | import com.github.tomakehurst.wiremock.common.Notifier;
|
9 | 7 | import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
|
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Objects; |
| 11 | +import java.util.stream.Collectors; |
10 | 12 | import org.junit.platform.commons.util.ReflectionUtils;
|
11 | 13 | import org.junit.platform.commons.util.StringUtils;
|
12 | 14 | import org.slf4j.Logger;
|
13 | 15 | import org.slf4j.LoggerFactory;
|
14 |
| - |
15 | 16 | import org.springframework.boot.test.util.TestPropertyValues;
|
16 | 17 | import org.springframework.context.ConfigurableApplicationContext;
|
17 | 18 | import org.springframework.context.event.ContextClosedEvent;
|
18 | 19 | import org.springframework.core.env.Environment;
|
19 | 20 | import org.springframework.test.context.ContextCustomizer;
|
20 | 21 | import org.springframework.test.context.MergedContextConfiguration;
|
21 | 22 |
|
22 |
| -import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; |
23 |
| - |
24 | 23 | /**
|
25 | 24 | * Attaches properties with urls pointing to {@link WireMockServer} instances to the Spring {@link Environment}.
|
26 | 25 | *
|
27 | 26 | * @author Maciej Walkowiak
|
28 | 27 | */
|
29 | 28 | public class WireMockContextCustomizer implements ContextCustomizer {
|
30 |
| - private static final Logger LOGGER = LoggerFactory.getLogger(WireMockContextCustomizer.class); |
31 |
| - |
32 |
| - private final List<ConfigureWireMock> configuration; |
33 |
| - |
34 |
| - /** |
35 |
| - * Creates an instance of {@link WireMockContextCustomizer}. |
36 |
| - * |
37 |
| - * @param configurations the configurations |
38 |
| - */ |
39 |
| - public WireMockContextCustomizer(List<ConfigureWireMock> configurations) { |
40 |
| - this.configuration = configurations; |
41 |
| - } |
42 | 29 |
|
43 |
| - /** |
44 |
| - * Creates an instance of {@link WireMockContextCustomizer}. |
45 |
| - * |
46 |
| - * @param configurations the configurations |
47 |
| - */ |
48 |
| - public WireMockContextCustomizer(ConfigureWireMock[] configurations) { |
49 |
| - this(Arrays.asList(configurations)); |
50 |
| - } |
51 |
| - |
52 |
| - @Override |
53 |
| - public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) { |
54 |
| - for (ConfigureWireMock configureWiremock : configuration) { |
55 |
| - resolveOrCreateWireMockServer(context, configureWiremock); |
56 |
| - } |
| 30 | + private static final Logger LOGGER = LoggerFactory.getLogger(WireMockContextCustomizer.class); |
| 31 | + |
| 32 | + private final List<ConfigureWireMock> configuration; |
| 33 | + |
| 34 | + /** |
| 35 | + * Creates an instance of {@link WireMockContextCustomizer}. |
| 36 | + * |
| 37 | + * @param configurations the configurations |
| 38 | + */ |
| 39 | + public WireMockContextCustomizer(List<ConfigureWireMock> configurations) { |
| 40 | + this.configuration = configurations; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Creates an instance of {@link WireMockContextCustomizer}. |
| 45 | + * |
| 46 | + * @param configurations the configurations |
| 47 | + */ |
| 48 | + public WireMockContextCustomizer(ConfigureWireMock[] configurations) { |
| 49 | + this(Arrays.asList(configurations)); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) { |
| 54 | + for (ConfigureWireMock configureWiremock : configuration) { |
| 55 | + resolveOrCreateWireMockServer(context, configureWiremock); |
57 | 56 | }
|
| 57 | + } |
58 | 58 |
|
59 |
| - private void resolveOrCreateWireMockServer(ConfigurableApplicationContext context, ConfigureWireMock options) { |
60 |
| - WireMockServer wireMockServer = Store.INSTANCE.findWireMockInstance(context, options.name()); |
| 59 | + private void resolveOrCreateWireMockServer(ConfigurableApplicationContext context, ConfigureWireMock options) { |
| 60 | + WireMockServer wireMockServer = Store.INSTANCE.findWireMockInstance(context, options.name()); |
61 | 61 |
|
62 |
| - if (wireMockServer == null) { |
63 |
| - // create & start wiremock server |
64 |
| - WireMockConfiguration serverOptions = options() |
65 |
| - .usingFilesUnderClasspath(resolveStubLocation(options)) |
66 |
| - .port(options.port()) |
67 |
| - .notifier(new Slf4jNotifier(true)); |
| 62 | + if (wireMockServer == null) { |
| 63 | + // create & start wiremock server |
| 64 | + WireMockConfiguration serverOptions = options() |
| 65 | + .usingFilesUnderClasspath(resolveStubLocation(options)) |
| 66 | + .port(options.port()) |
| 67 | + .notifier(new Slf4jNotifier(true)); |
68 | 68 |
|
69 |
| - if (options.extensions().length > 0) { |
70 |
| - serverOptions.extensions(options.extensions()); |
71 |
| - } |
| 69 | + if (options.extensions().length > 0) { |
| 70 | + serverOptions.extensions(options.extensions()); |
| 71 | + } |
72 | 72 |
|
73 |
| - applyCustomizers(options, serverOptions); |
| 73 | + applyCustomizers(options, serverOptions); |
74 | 74 |
|
75 |
| - LOGGER.info("Configuring WireMockServer with name '{}' on port: {}", options.name(), serverOptions.portNumber()); |
| 75 | + LOGGER.info("Configuring WireMockServer with name '{}' on port: {}", options.name(), serverOptions.portNumber()); |
76 | 76 |
|
77 |
| - WireMockServer newServer = new WireMockServer(serverOptions); |
78 |
| - newServer.start(); |
| 77 | + WireMockServer newServer = new WireMockServer(serverOptions); |
| 78 | + newServer.start(); |
79 | 79 |
|
80 |
| - LOGGER.info("Started WireMockServer with name '{}':{}", options.name(), newServer.baseUrl()); |
| 80 | + LOGGER.info("Started WireMockServer with name '{}':{}", options.name(), newServer.baseUrl()); |
81 | 81 |
|
82 |
| - // save server to store |
83 |
| - Store.INSTANCE.store(context, options.name(), newServer); |
| 82 | + // save server to store |
| 83 | + Store.INSTANCE.store(context, options.name(), newServer); |
84 | 84 |
|
85 |
| - // add shutdown hook |
86 |
| - context.addApplicationListener(event -> { |
87 |
| - if (event instanceof ContextClosedEvent) { |
88 |
| - LOGGER.info("Stopping WireMockServer with name '{}'", options.name()); |
89 |
| - newServer.stop(); |
90 |
| - } |
91 |
| - }); |
92 |
| - |
93 |
| - // configure Spring environment property |
94 |
| - Arrays.stream(options.property()).forEach(propertyName -> { |
95 |
| - String property = propertyName + "=" + newServer.baseUrl(); |
96 |
| - LOGGER.debug("Adding property '{}' to Spring application context", property); |
97 |
| - TestPropertyValues.of(property).applyTo(context.getEnvironment()); |
98 |
| - }); |
99 |
| - } else { |
100 |
| - LOGGER.info("WireMockServer with name '{}' is already configured", options.name()); |
| 85 | + // add shutdown hook |
| 86 | + context.addApplicationListener(event -> { |
| 87 | + if (event instanceof ContextClosedEvent) { |
| 88 | + LOGGER.info("Stopping WireMockServer with name '{}'", options.name()); |
| 89 | + newServer.stop(); |
101 | 90 | }
|
| 91 | + }); |
| 92 | + |
| 93 | + // configure Spring environment property |
| 94 | + List<String> propertyNames; |
| 95 | + if (StringUtils.isNotBlank(options.property())) { |
| 96 | + propertyNames = List.of(options.property()); |
| 97 | + } else { |
| 98 | + propertyNames = Arrays.asList(options.properties()).stream() |
| 99 | + .filter(StringUtils::isNotBlank) |
| 100 | + .collect(Collectors.toList()); |
| 101 | + } |
| 102 | + propertyNames.forEach(propertyName -> { |
| 103 | + String property = propertyName + "=" + newServer.baseUrl(); |
| 104 | + LOGGER.debug("Adding property '{}' to Spring application context", property); |
| 105 | + TestPropertyValues.of(property).applyTo(context.getEnvironment()); |
| 106 | + }); |
| 107 | + } else { |
| 108 | + LOGGER.info("WireMockServer with name '{}' is already configured", options.name()); |
102 | 109 | }
|
103 |
| - |
104 |
| - private static void applyCustomizers(ConfigureWireMock options, WireMockConfiguration serverOptions) { |
105 |
| - for (Class<? extends WireMockConfigurationCustomizer> customizer : options.configurationCustomizers()) { |
106 |
| - try { |
107 |
| - ReflectionUtils.newInstance(customizer).customize(serverOptions, options); |
108 |
| - } catch (Exception e) { |
109 |
| - if (e instanceof NoSuchMethodException) { |
110 |
| - LOGGER.error("Customizer {} must have a no-arg constructor", customizer, e); |
111 |
| - } |
112 |
| - throw e; |
113 |
| - } |
| 110 | + } |
| 111 | + |
| 112 | + private static void applyCustomizers(ConfigureWireMock options, WireMockConfiguration serverOptions) { |
| 113 | + for (Class<? extends WireMockConfigurationCustomizer> customizer : options.configurationCustomizers()) { |
| 114 | + try { |
| 115 | + ReflectionUtils.newInstance(customizer).customize(serverOptions, options); |
| 116 | + } catch (Exception e) { |
| 117 | + if (e instanceof NoSuchMethodException) { |
| 118 | + LOGGER.error("Customizer {} must have a no-arg constructor", customizer, e); |
114 | 119 | }
|
| 120 | + throw e; |
| 121 | + } |
115 | 122 | }
|
| 123 | + } |
116 | 124 |
|
117 |
| - private String resolveStubLocation(ConfigureWireMock options) { |
118 |
| - return StringUtils.isBlank(options.stubLocation()) ? "wiremock/" + options.name() : options.stubLocation(); |
119 |
| - } |
| 125 | + private String resolveStubLocation(ConfigureWireMock options) { |
| 126 | + return StringUtils.isBlank(options.stubLocation()) ? "wiremock/" + options.name() : options.stubLocation(); |
| 127 | + } |
120 | 128 |
|
121 |
| - @Override |
122 |
| - public boolean equals(Object o) { |
123 |
| - if (this == o) |
124 |
| - return true; |
125 |
| - if (o == null || getClass() != o.getClass()) |
126 |
| - return false; |
127 |
| - WireMockContextCustomizer that = (WireMockContextCustomizer) o; |
128 |
| - return Objects.equals(configuration, that.configuration); |
| 129 | + @Override |
| 130 | + public boolean equals(Object o) { |
| 131 | + if (this == o) { |
| 132 | + return true; |
129 | 133 | }
|
130 |
| - |
131 |
| - @Override |
132 |
| - public int hashCode() { |
133 |
| - return Objects.hash(configuration); |
| 134 | + if (o == null || getClass() != o.getClass()) { |
| 135 | + return false; |
134 | 136 | }
|
| 137 | + WireMockContextCustomizer that = (WireMockContextCustomizer) o; |
| 138 | + return Objects.equals(configuration, that.configuration); |
| 139 | + } |
135 | 140 |
|
136 |
| - // ported from: https://github.com/spring-cloud/spring-cloud-contract/commit/44c634d0e9e82515d2fba66343530eb7d2ba8223 |
137 |
| - static class Slf4jNotifier implements Notifier { |
| 141 | + @Override |
| 142 | + public int hashCode() { |
| 143 | + return Objects.hash(configuration); |
| 144 | + } |
138 | 145 |
|
139 |
| - private static final Logger log = LoggerFactory.getLogger("WireMock"); |
| 146 | + // ported from: https://github.com/spring-cloud/spring-cloud-contract/commit/44c634d0e9e82515d2fba66343530eb7d2ba8223 |
| 147 | + static class Slf4jNotifier implements Notifier { |
140 | 148 |
|
141 |
| - private final boolean verbose; |
| 149 | + private static final Logger log = LoggerFactory.getLogger("WireMock"); |
142 | 150 |
|
143 |
| - Slf4jNotifier(boolean verbose) { |
144 |
| - this.verbose = verbose; |
145 |
| - } |
| 151 | + private final boolean verbose; |
146 | 152 |
|
147 |
| - @Override |
148 |
| - public void info(String message) { |
149 |
| - if (verbose) { |
150 |
| - log.info(message); |
151 |
| - } |
152 |
| - } |
| 153 | + Slf4jNotifier(boolean verbose) { |
| 154 | + this.verbose = verbose; |
| 155 | + } |
153 | 156 |
|
154 |
| - @Override |
155 |
| - public void error(String message) { |
156 |
| - log.error(message); |
157 |
| - } |
| 157 | + @Override |
| 158 | + public void info(String message) { |
| 159 | + if (verbose) { |
| 160 | + log.info(message); |
| 161 | + } |
| 162 | + } |
158 | 163 |
|
159 |
| - @Override |
160 |
| - public void error(String message, Throwable t) { |
161 |
| - log.error(message, t); |
162 |
| - } |
| 164 | + @Override |
| 165 | + public void error(String message) { |
| 166 | + log.error(message); |
| 167 | + } |
163 | 168 |
|
| 169 | + @Override |
| 170 | + public void error(String message, Throwable t) { |
| 171 | + log.error(message, t); |
164 | 172 | }
|
| 173 | + |
| 174 | + } |
165 | 175 | }
|
0 commit comments