Skip to content

Commit 0b8853e

Browse files
davinkevinlanwen
authored andcommitted
feat(configFactory): be able to access junit-context during config of the server (#17)
Closes #9
1 parent 44be487 commit 0b8853e

File tree

5 files changed

+55
-13
lines changed

5 files changed

+55
-13
lines changed

src/main/java/ru/lanwen/wiremock/config/WiremockConfigFactory.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.github.tomakehurst.wiremock.common.Slf4jNotifier;
44
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
55
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
6+
import org.junit.jupiter.api.extension.ExtensionContext;
67
import ru.lanwen.wiremock.ext.WiremockResolver;
78

89
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
@@ -20,18 +21,18 @@ public interface WiremockConfigFactory {
2021
*
2122
* @return config for wiremock
2223
*/
23-
WireMockConfiguration create();
24+
default WireMockConfiguration create() {
25+
return options().dynamicPort().notifier(new Slf4jNotifier(true));
26+
}
27+
28+
default WireMockConfiguration create(ExtensionContext context) {
29+
return create();
30+
}
2431

2532
/**
2633
* By default creates config with dynamic port only and notifier.
2734
*/
28-
class DefaultWiremockConfigFactory implements WiremockConfigFactory {
29-
30-
@Override
31-
public WireMockConfiguration create() {
32-
return options().dynamicPort().notifier(new Slf4jNotifier(true));
33-
}
34-
}
35+
class DefaultWiremockConfigFactory implements WiremockConfigFactory {}
3536

3637
/**
3738
* By default creates config with dynamic port only, notifier and Templating Response enabled.

src/main/java/ru/lanwen/wiremock/ext/WiremockFactory.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package ru.lanwen.wiremock.ext;
22

33
import com.github.tomakehurst.wiremock.WireMockServer;
4+
import org.junit.jupiter.api.extension.ExtensionContext;
45
import org.junit.jupiter.api.extension.ParameterResolutionException;
56
import ru.lanwen.wiremock.config.CustomizationContext;
67
import ru.lanwen.wiremock.config.CustomizationContext.CustomizationContextBuilder;
8+
import ru.lanwen.wiremock.config.WiremockConfigFactory;
79
import ru.lanwen.wiremock.config.WiremockCustomizer;
810
import ru.lanwen.wiremock.ext.WiremockResolver.Wiremock;
911

@@ -15,8 +17,16 @@
1517
class WiremockFactory {
1618

1719
public WireMockServer createServer(final Wiremock mockedServer) {
20+
return new WireMockServer(createFactoryInstance(mockedServer).create());
21+
}
22+
23+
public WireMockServer createServer(final Wiremock mockedServer, ExtensionContext extensionContext) {
24+
return new WireMockServer(createFactoryInstance(mockedServer).create(extensionContext));
25+
}
26+
27+
private WiremockConfigFactory createFactoryInstance(final Wiremock mockedServer) {
1828
try {
19-
return new WireMockServer(mockedServer.factory().newInstance().create());
29+
return mockedServer.factory().newInstance();
2030
} catch (ReflectiveOperationException e) {
2131
throw new ParameterResolutionException(
2232
format("Can't create config with given factory %s", mockedServer.factory()),

src/main/java/ru/lanwen/wiremock/ext/WiremockResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
6565

6666
Wiremock mockedServer = parameterContext.getParameter().getAnnotation(Wiremock.class);
6767

68-
server = wiremockFactory.createServer(mockedServer);
68+
server = wiremockFactory.createServer(mockedServer, extensionContext);
6969
server.start();
7070

7171
CustomizationContext customizationContext = wiremockFactory.createContextBuilder().

src/test/java/ru/lanwen/wiremock/ext/WiremockFactoryTest.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@
44
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
55
import org.junit.jupiter.api.BeforeEach;
66
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.extension.ExtensionContext;
78
import org.junit.jupiter.api.extension.ParameterResolutionException;
89
import ru.lanwen.wiremock.config.CustomizationContext.CustomizationContextBuilder;
910
import ru.lanwen.wiremock.config.WiremockConfigFactory;
1011
import ru.lanwen.wiremock.config.WiremockCustomizer;
1112
import ru.lanwen.wiremock.ext.WiremockResolver.Wiremock;
1213

1314
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
15+
import static org.junit.jupiter.api.Assertions.assertEquals;
1416
import static org.junit.jupiter.api.Assertions.assertNotNull;
1517
import static org.junit.jupiter.api.Assertions.assertNotSame;
1618
import static org.junit.jupiter.api.Assertions.assertSame;
1719
import static org.junit.jupiter.api.Assertions.assertThrows;
20+
import static org.junit.jupiter.api.extension.ExtensionContext.Namespace;
21+
import static org.junit.jupiter.api.extension.ExtensionContext.Store;
1822
import static org.mockito.Mockito.mock;
1923
import static org.mockito.Mockito.when;
2024

@@ -36,6 +40,15 @@ public void customize(WireMockServer server) {
3640
}
3741
}
3842

43+
public static class FactoryUsingContext implements WiremockConfigFactory {
44+
@Override
45+
public WireMockConfiguration create(ExtensionContext context) {
46+
Integer port = context.getStore(Namespace.GLOBAL)
47+
.get("port", Integer.class);
48+
return options().port(port);
49+
}
50+
}
51+
3952
private static class PrivateClassNotAllowed implements WiremockConfigFactory, WiremockCustomizer {
4053

4154
@Override
@@ -55,12 +68,12 @@ public void customize(WireMockServer server) {
5568

5669
@BeforeEach
5770
public void setup() {
58-
when(mockedServer.factory()).thenReturn((Class) StubClass.class);
5971
when(mockedServer.customizer()).thenReturn((Class) StubClass.class);
6072
}
6173

6274
@Test
6375
public void createServer() {
76+
when(mockedServer.factory()).thenReturn((Class) StubClass.class);
6477
WireMockServer srv1 = factory.createServer(mockedServer);
6578
WireMockServer srv2 = factory.createServer(mockedServer);
6679
assertNotNull(srv1);
@@ -70,11 +83,29 @@ public void createServer() {
7083
assertNotSame(srv1, srv2);
7184
}
7285

86+
@Test
87+
public void createServerWithContext() {
88+
ExtensionContext ctx = mock(ExtensionContext.class);
89+
Store store = mock(Store.class);
90+
when(ctx.getStore(Namespace.GLOBAL)).thenReturn(store);
91+
when(store.get("port", Integer.class)).thenReturn(9874);
92+
93+
when(mockedServer.factory()).thenReturn((Class) FactoryUsingContext.class);
94+
95+
WireMockServer srv1 = factory.createServer(mockedServer, ctx);
96+
WireMockServer srv2 = factory.createServer(mockedServer, ctx);
97+
assertNotNull(srv1);
98+
assertNotNull(srv2);
99+
assertEquals(9874, srv1.getOptions().portNumber());
100+
assertEquals(9874, srv2.getOptions().portNumber());
101+
assertNotSame(srv1, srv2);
102+
}
103+
73104
@Test
74105
public void configFactoryCouldNotBeInstantiated() {
75106
when(mockedServer.factory()).thenReturn((Class) PrivateClassNotAllowed.class);
76107
assertThrows(ParameterResolutionException.class,
77-
() -> factory.createServer(mockedServer),
108+
() -> factory.createServer(mockedServer, null),
78109
"Can't create config with given factory class ru.lanwen.wiremock.ext.WiremockFactoryTest$PrivateClassNotAllowed");
79110
}
80111

src/test/java/ru/lanwen/wiremock/ext/WiremockResolverUnitTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void supportsParameter() throws Exception {
8888

8989
@Test
9090
public void resolveParameterFailed() throws Exception {
91-
when(wiremockFactory.createServer(mockedServer)).thenReturn(server);
91+
when(wiremockFactory.createServer(mockedServer, extensionContext)).thenReturn(server);
9292
when(wiremockFactory.createContextBuilder()).thenReturn(customizationContextBuilder);
9393
when(wiremockFactory.createCustomizer(mockedServer)).thenReturn(customizer);
9494
when(customizationContextBuilder.extensionContext(extensionContext)).thenReturn(customizationContextBuilder);

0 commit comments

Comments
 (0)