Skip to content

Commit

Permalink
feat(configFactory): be able to access junit-context during config of…
Browse files Browse the repository at this point in the history
… the server (#17)

Closes #9
  • Loading branch information
davinkevin authored and lanwen committed Aug 15, 2019
1 parent 44be487 commit 0b8853e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.tomakehurst.wiremock.common.Slf4jNotifier;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
import org.junit.jupiter.api.extension.ExtensionContext;
import ru.lanwen.wiremock.ext.WiremockResolver;

import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
Expand All @@ -20,18 +21,18 @@ public interface WiremockConfigFactory {
*
* @return config for wiremock
*/
WireMockConfiguration create();
default WireMockConfiguration create() {
return options().dynamicPort().notifier(new Slf4jNotifier(true));
}

default WireMockConfiguration create(ExtensionContext context) {
return create();
}

/**
* By default creates config with dynamic port only and notifier.
*/
class DefaultWiremockConfigFactory implements WiremockConfigFactory {

@Override
public WireMockConfiguration create() {
return options().dynamicPort().notifier(new Slf4jNotifier(true));
}
}
class DefaultWiremockConfigFactory implements WiremockConfigFactory {}

/**
* By default creates config with dynamic port only, notifier and Templating Response enabled.
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/ru/lanwen/wiremock/ext/WiremockFactory.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package ru.lanwen.wiremock.ext;

import com.github.tomakehurst.wiremock.WireMockServer;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import ru.lanwen.wiremock.config.CustomizationContext;
import ru.lanwen.wiremock.config.CustomizationContext.CustomizationContextBuilder;
import ru.lanwen.wiremock.config.WiremockConfigFactory;
import ru.lanwen.wiremock.config.WiremockCustomizer;
import ru.lanwen.wiremock.ext.WiremockResolver.Wiremock;

Expand All @@ -15,8 +17,16 @@
class WiremockFactory {

public WireMockServer createServer(final Wiremock mockedServer) {
return new WireMockServer(createFactoryInstance(mockedServer).create());
}

public WireMockServer createServer(final Wiremock mockedServer, ExtensionContext extensionContext) {
return new WireMockServer(createFactoryInstance(mockedServer).create(extensionContext));
}

private WiremockConfigFactory createFactoryInstance(final Wiremock mockedServer) {
try {
return new WireMockServer(mockedServer.factory().newInstance().create());
return mockedServer.factory().newInstance();
} catch (ReflectiveOperationException e) {
throw new ParameterResolutionException(
format("Can't create config with given factory %s", mockedServer.factory()),
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ru/lanwen/wiremock/ext/WiremockResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte

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

server = wiremockFactory.createServer(mockedServer);
server = wiremockFactory.createServer(mockedServer, extensionContext);
server.start();

CustomizationContext customizationContext = wiremockFactory.createContextBuilder().
Expand Down
35 changes: 33 additions & 2 deletions src/test/java/ru/lanwen/wiremock/ext/WiremockFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import ru.lanwen.wiremock.config.CustomizationContext.CustomizationContextBuilder;
import ru.lanwen.wiremock.config.WiremockConfigFactory;
import ru.lanwen.wiremock.config.WiremockCustomizer;
import ru.lanwen.wiremock.ext.WiremockResolver.Wiremock;

import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import static org.junit.jupiter.api.extension.ExtensionContext.Store;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -36,6 +40,15 @@ public void customize(WireMockServer server) {
}
}

public static class FactoryUsingContext implements WiremockConfigFactory {
@Override
public WireMockConfiguration create(ExtensionContext context) {
Integer port = context.getStore(Namespace.GLOBAL)
.get("port", Integer.class);
return options().port(port);
}
}

private static class PrivateClassNotAllowed implements WiremockConfigFactory, WiremockCustomizer {

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

@BeforeEach
public void setup() {
when(mockedServer.factory()).thenReturn((Class) StubClass.class);
when(mockedServer.customizer()).thenReturn((Class) StubClass.class);
}

@Test
public void createServer() {
when(mockedServer.factory()).thenReturn((Class) StubClass.class);
WireMockServer srv1 = factory.createServer(mockedServer);
WireMockServer srv2 = factory.createServer(mockedServer);
assertNotNull(srv1);
Expand All @@ -70,11 +83,29 @@ public void createServer() {
assertNotSame(srv1, srv2);
}

@Test
public void createServerWithContext() {
ExtensionContext ctx = mock(ExtensionContext.class);
Store store = mock(Store.class);
when(ctx.getStore(Namespace.GLOBAL)).thenReturn(store);
when(store.get("port", Integer.class)).thenReturn(9874);

when(mockedServer.factory()).thenReturn((Class) FactoryUsingContext.class);

WireMockServer srv1 = factory.createServer(mockedServer, ctx);
WireMockServer srv2 = factory.createServer(mockedServer, ctx);
assertNotNull(srv1);
assertNotNull(srv2);
assertEquals(9874, srv1.getOptions().portNumber());
assertEquals(9874, srv2.getOptions().portNumber());
assertNotSame(srv1, srv2);
}

@Test
public void configFactoryCouldNotBeInstantiated() {
when(mockedServer.factory()).thenReturn((Class) PrivateClassNotAllowed.class);
assertThrows(ParameterResolutionException.class,
() -> factory.createServer(mockedServer),
() -> factory.createServer(mockedServer, null),
"Can't create config with given factory class ru.lanwen.wiremock.ext.WiremockFactoryTest$PrivateClassNotAllowed");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void supportsParameter() throws Exception {

@Test
public void resolveParameterFailed() throws Exception {
when(wiremockFactory.createServer(mockedServer)).thenReturn(server);
when(wiremockFactory.createServer(mockedServer, extensionContext)).thenReturn(server);
when(wiremockFactory.createContextBuilder()).thenReturn(customizationContextBuilder);
when(wiremockFactory.createCustomizer(mockedServer)).thenReturn(customizer);
when(customizationContextBuilder.extensionContext(extensionContext)).thenReturn(customizationContextBuilder);
Expand Down

0 comments on commit 0b8853e

Please sign in to comment.