|
6 | 6 | import java.util.List; |
7 | 7 | import java.util.Set; |
8 | 8 | import java.util.stream.Collectors; |
| 9 | + |
9 | 10 | import org.springframework.core.annotation.AnnotationUtils; |
10 | 11 | import org.springframework.test.context.ContextConfigurationAttributes; |
11 | 12 | import org.springframework.test.context.ContextCustomizer; |
|
15 | 16 | import org.wiremock.spring.EnableWireMock; |
16 | 17 |
|
17 | 18 | /** |
18 | | - * Creates {@link WireMockContextCustomizer} for test classes annotated with {@link EnableWireMock}. |
| 19 | + * Creates {@link WireMockContextCustomizer} for test classes annotated with |
| 20 | + * {@link EnableWireMock}. |
19 | 21 | * |
20 | 22 | * @author Maciej Walkowiak |
21 | 23 | */ |
22 | 24 | public class WireMockContextCustomizerFactory implements ContextCustomizerFactory { |
23 | | - static final ConfigureWireMock DEFAULT_CONFIGURE_WIREMOCK = |
24 | | - DefaultConfigureWireMock.class.getAnnotation(ConfigureWireMock.class); |
25 | | - |
26 | | - @ConfigureWireMock(name = "wiremock") |
27 | | - private static class DefaultConfigureWireMock {} |
28 | | - |
29 | | - static ConfigureWireMock[] getConfigureWireMocksOrDefault( |
30 | | - final ConfigureWireMock... configureWireMock) { |
31 | | - if (configureWireMock == null || configureWireMock.length == 0) { |
32 | | - return new ConfigureWireMock[] {WireMockContextCustomizerFactory.DEFAULT_CONFIGURE_WIREMOCK}; |
33 | | - } |
34 | | - return configureWireMock; |
35 | | - } |
36 | | - |
37 | | - @Override |
38 | | - public ContextCustomizer createContextCustomizer( |
39 | | - final Class<?> testClass, final List<ContextConfigurationAttributes> configAttributes) { |
40 | | - // scan class and all enclosing classes if the test class is @Nested |
41 | | - final ConfigureWiremockHolder holder = new ConfigureWiremockHolder(); |
42 | | - this.parseDefinitions(testClass, holder); |
43 | | - |
44 | | - if (holder.isEmpty()) { |
45 | | - return null; |
46 | | - } else { |
47 | | - return new WireMockContextCustomizer(holder.asArray()); |
48 | | - } |
49 | | - } |
50 | | - |
51 | | - private void parseDefinitions(final Class<?> testClass, final ConfigureWiremockHolder parser) { |
52 | | - parser.parse(testClass); |
53 | | - if (TestContextAnnotationUtils.searchEnclosingClass(testClass)) { |
54 | | - this.parseDefinitions(testClass.getEnclosingClass(), parser); |
55 | | - } |
56 | | - } |
57 | | - |
58 | | - private static class ConfigureWiremockHolder { |
59 | | - private final List<ConfigureWireMock> annotations = new ArrayList<>(); |
60 | | - |
61 | | - void add(final ConfigureWireMock... annotations) { |
62 | | - this.annotations.addAll(Arrays.asList(annotations)); |
63 | | - this.sanityCheckDuplicateNames(this.annotations); |
64 | | - } |
65 | | - |
66 | | - void parse(final Class<?> clazz) { |
67 | | - final EnableWireMock annotation = AnnotationUtils.findAnnotation(clazz, EnableWireMock.class); |
68 | | - if (annotation != null) { |
69 | | - this.add(getConfigureWireMocksOrDefault(annotation.value())); |
70 | | - } |
71 | | - } |
72 | | - |
73 | | - private void sanityCheckDuplicateNames(final List<ConfigureWireMock> check) { |
74 | | - final List<String> names = check.stream().map(it -> it.name()).toList(); |
75 | | - final Set<String> dublicateNames = |
76 | | - names.stream() |
77 | | - .filter(it -> Collections.frequency(names, it) > 1) |
78 | | - .collect(Collectors.toSet()); |
79 | | - if (!dublicateNames.isEmpty()) { |
80 | | - throw new IllegalStateException( |
81 | | - "Names of mocks must be unique, found duplicates of: " |
82 | | - + dublicateNames.stream().sorted().collect(Collectors.joining(","))); |
83 | | - } |
84 | | - } |
85 | | - |
86 | | - boolean isEmpty() { |
87 | | - return this.annotations.isEmpty(); |
88 | | - } |
89 | | - |
90 | | - ConfigureWireMock[] asArray() { |
91 | | - return this.annotations.toArray(new ConfigureWireMock[] {}); |
92 | | - } |
93 | | - } |
| 25 | + static final ConfigureWireMock DEFAULT_CONFIGURE_WIREMOCK = DefaultConfigureWireMock.class |
| 26 | + .getAnnotation(ConfigureWireMock.class); |
| 27 | + |
| 28 | + @ConfigureWireMock(name = "wiremock") |
| 29 | + private static class DefaultConfigureWireMock { |
| 30 | + } |
| 31 | + |
| 32 | + static ConfigureWireMock[] getConfigureWireMocksOrDefault(final ConfigureWireMock... configureWireMock) { |
| 33 | + if (configureWireMock == null || configureWireMock.length == 0) { |
| 34 | + return new ConfigureWireMock[] { WireMockContextCustomizerFactory.DEFAULT_CONFIGURE_WIREMOCK }; |
| 35 | + } |
| 36 | + return configureWireMock; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public ContextCustomizer createContextCustomizer(final Class<?> testClass, |
| 41 | + final List<ContextConfigurationAttributes> configAttributes) { |
| 42 | + // scan class and all enclosing classes if the test class is @Nested |
| 43 | + final ConfigureWiremockHolder holder = new ConfigureWiremockHolder(); |
| 44 | + this.parseDefinitions(testClass, holder); |
| 45 | + |
| 46 | + if (holder.isEmpty()) { |
| 47 | + return null; |
| 48 | + } else { |
| 49 | + return new WireMockContextCustomizer(holder.asArray()); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private void parseDefinitions(final Class<?> testClass, final ConfigureWiremockHolder parser) { |
| 54 | + parser.parse(testClass); |
| 55 | + if (TestContextAnnotationUtils.searchEnclosingClass(testClass)) { |
| 56 | + this.parseDefinitions(testClass.getEnclosingClass(), parser); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private static class ConfigureWiremockHolder { |
| 61 | + private final List<ConfigureWireMock> annotations = new ArrayList<>(); |
| 62 | + |
| 63 | + void add(final ConfigureWireMock... annotations) { |
| 64 | + this.annotations.addAll(Arrays.asList(annotations)); |
| 65 | + this.sanityCheckDuplicateNames(this.annotations); |
| 66 | + this.sanityCheckHttpOrHttpsMustBeEnabled(this.annotations); |
| 67 | + this.sanityCheckHttpAndHttpsMustUseDifferentPorts(this.annotations); |
| 68 | + this.sanityCheckUniquePorts(this.annotations); |
| 69 | + } |
| 70 | + |
| 71 | + void parse(final Class<?> clazz) { |
| 72 | + final EnableWireMock annotation = AnnotationUtils.findAnnotation(clazz, EnableWireMock.class); |
| 73 | + if (annotation != null) { |
| 74 | + this.add(getConfigureWireMocksOrDefault(annotation.value())); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private void sanityCheckDuplicateNames(final List<ConfigureWireMock> check) { |
| 79 | + final List<String> names = check.stream().map(it -> it.name()).toList(); |
| 80 | + final Set<String> dublicateNames = names.stream().filter(it -> Collections.frequency(names, it) > 1) |
| 81 | + .collect(Collectors.toSet()); |
| 82 | + if (!dublicateNames.isEmpty()) { |
| 83 | + throw new IllegalStateException("Names of mocks must be unique, found duplicates of: " |
| 84 | + + dublicateNames.stream().sorted().collect(Collectors.joining(","))); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private void sanityCheckHttpOrHttpsMustBeEnabled(final List<ConfigureWireMock> check) { |
| 89 | + for (final ConfigureWireMock configureWireMock : check) { |
| 90 | + if (configureWireMock.port() == -1 && configureWireMock.httpsPort() == -1) { |
| 91 | + throw new IllegalStateException("ConfigureWireMock " + configureWireMock.name() |
| 92 | + + " has both HTTP and HTTPS disabled. It is an invalid configuration."); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private void sanityCheckUniquePorts(final List<ConfigureWireMock> check) { |
| 98 | + final List<Integer> ports = check.stream().map(it -> List.of(it.port(), it.httpsPort())).toList().stream() |
| 99 | + .collect(ArrayList::new, List::addAll, List::addAll); |
| 100 | + final Set<Integer> dublicatePors = ports.stream().filter(it -> it > 0) |
| 101 | + .filter(it -> Collections.frequency(ports, it) > 1).collect(Collectors.toSet()); |
| 102 | + if (!dublicatePors.isEmpty()) { |
| 103 | + throw new IllegalStateException("Some statically configured ports are being used mor than once: " |
| 104 | + + dublicatePors.stream().sorted().map(it -> it.toString()).collect(Collectors.joining(","))); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private void sanityCheckHttpAndHttpsMustUseDifferentPorts(final List<ConfigureWireMock> check) { |
| 109 | + for (final ConfigureWireMock configureWireMock : check) { |
| 110 | + if (configureWireMock.port() > 0 && configureWireMock.port() == configureWireMock.httpsPort()) { |
| 111 | + throw new IllegalStateException("ConfigureWireMock " + configureWireMock.name() + " uses same port " |
| 112 | + + configureWireMock.port() + " for HTTP and HTTPS."); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + boolean isEmpty() { |
| 118 | + return this.annotations.isEmpty(); |
| 119 | + } |
| 120 | + |
| 121 | + ConfigureWireMock[] asArray() { |
| 122 | + return this.annotations.toArray(new ConfigureWireMock[] {}); |
| 123 | + } |
| 124 | + } |
94 | 125 | } |
0 commit comments