Skip to content

Commit

Permalink
Refactored WiremockCustomizer to take a CustomizationContext as argum…
Browse files Browse the repository at this point in the history
…ent (#7)

* Refactored WiremockCustomizer to take a CustomizationContext as argument
- Introduced a parameter class CustomizationContext which additionally provides ExtensionContext and ParameterContext objects. This allows to implement more sophisticated setup logic based on the execution context.
- Added WiremockCustomizer::customize(WireMockServer server, CustomizationContext ctx) as default method to keep backward compatibility.
- Made WiremockCustomizer::customize(WireMockServer server) also a default method. This is to make sure, that only the necessary "customize" method needs be implemented on a WiremockCustomizer implementation (see TicketEndpoint).
- Allow to throw an exception from WiremockCustomizer::customize
- Added Unit-Tests
- Updated dependencies and plugin versions where possible
- Bumped version to 2.0.0-SNAPSHOT because the interface change

* Fixed assertion

* Use NoopWiremockCustomizer to test default methods (code coverage)

* Pass correct replacement to exception message

* Simplified tests
- Use assertThrows where applicable
- Inlined Executable where necessary
- Simplified WiremockResolverUnitTest, removed tests which are already covered by integration test
  • Loading branch information
SourcePond authored and lanwen committed May 2, 2018
1 parent b571da6 commit dbebebb
Show file tree
Hide file tree
Showing 14 changed files with 486 additions and 39 deletions.
49 changes: 36 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>ru.lanwen.wiremock</groupId>
<artifactId>wiremock-junit5</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<url>https://github.com/lanwen/wiremock-junit5</url>
Expand Down Expand Up @@ -37,13 +37,14 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<wiremock.version>2.5.1</wiremock.version>
<feign.version>9.5.0</feign.version>
<lombok.version>1.16.16</lombok.version>
<logback.version>1.1.11</logback.version>
<wiremock.version>2.16.0</wiremock.version>
<feign.version>9.6.0</feign.version>
<lombok.version>1.16.20</lombok.version>
<logback.version>1.2.3</logback.version>
<jacoco.version>0.7.7.201606060606</jacoco.version>
<junit.jupiter.version>5.0.0</junit.jupiter.version>
<junit.platform.version>1.0.0</junit.platform.version>
<junit.jupiter.version>5.1.0</junit.jupiter.version>
<junit.platform.version>1.1.0</junit.platform.version>
<mockito.version>2.18.3</mockito.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -98,6 +99,16 @@
<version>${logback.version}</version>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -151,22 +162,34 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
Expand Down Expand Up @@ -243,7 +266,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
Expand All @@ -257,7 +280,7 @@
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.3</version>
<version>1.6.8</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
Expand All @@ -269,7 +292,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
Expand All @@ -283,7 +306,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<version>3.0.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/ru/lanwen/wiremock/config/CustomizationContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.lanwen.wiremock.config;

import lombok.Builder;
import lombok.Value;
import lombok.experimental.NonFinal;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;

/**
* @author SourcePond (Roland Hauser)
*/
@Value
@Builder
@NonFinal
public class CustomizationContext {
ExtensionContext extensionContext;
ParameterContext parameterContext;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import ru.lanwen.wiremock.ext.WiremockResolver;

import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;

/**
* You can create custom config to init wiremock server in test.
*
Expand All @@ -26,7 +28,7 @@ class DefaultWiremockConfigFactory implements WiremockConfigFactory {

@Override
public WireMockConfiguration create() {
return WireMockConfiguration.options().dynamicPort().notifier(new Slf4jNotifier(true));
return options().dynamicPort().notifier(new Slf4jNotifier(true));
}
}
}
13 changes: 8 additions & 5 deletions src/main/java/ru/lanwen/wiremock/config/WiremockCustomizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
*/
public interface WiremockCustomizer {

void customize(final WireMockServer server);
default void customize(WireMockServer server) throws Exception {
// noop
}

default void customize(WireMockServer server, CustomizationContext ctx) throws Exception {
customize(server);
}

class NoopWiremockCustomizer implements WiremockCustomizer {
@Override
public void customize(final WireMockServer server) {
// noop
}

}
}
42 changes: 42 additions & 0 deletions src/main/java/ru/lanwen/wiremock/ext/WiremockFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ru.lanwen.wiremock.ext;

import com.github.tomakehurst.wiremock.WireMockServer;
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.WiremockCustomizer;
import ru.lanwen.wiremock.ext.WiremockResolver.Wiremock;

import static java.lang.String.format;

/**
* @author SourcePond (Roland Hauser)
*/
class WiremockFactory {

public WireMockServer createServer(final Wiremock mockedServer) {
try {
return new WireMockServer(mockedServer.factory().newInstance().create());
} catch (ReflectiveOperationException e) {
throw new ParameterResolutionException(
format("Can't create config with given factory %s", mockedServer.factory()),
e
);
}
}

public CustomizationContextBuilder createContextBuilder() {
return CustomizationContext.builder();
}

public WiremockCustomizer createCustomizer(final Wiremock mockedServer) {
try {
return mockedServer.customizer().newInstance();
} catch (ReflectiveOperationException e) {
throw new ParameterResolutionException(
format("Can't customize server with given customizer %s", mockedServer.customizer()),
e
);
}
}
}
43 changes: 24 additions & 19 deletions src/main/java/ru/lanwen/wiremock/ext/WiremockResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
import ru.lanwen.wiremock.config.CustomizationContext;
import ru.lanwen.wiremock.config.WiremockConfigFactory;
import ru.lanwen.wiremock.config.WiremockCustomizer;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Optional;

import static java.lang.String.format;
import static java.util.Optional.ofNullable;
import static ru.lanwen.wiremock.ext.Validate.validState;

/**
* @author lanwen (Merkushev Kirill)
Expand All @@ -26,8 +28,17 @@
public class WiremockResolver implements ParameterResolver, AfterEachCallback {
static final String WIREMOCK_PORT = "wiremock.port";

private final WiremockFactory wiremockFactory;
private WireMockServer server;

public WiremockResolver() {
this(new WiremockFactory());
}

WiremockResolver(final WiremockFactory wiremockFactory) {
this.wiremockFactory = wiremockFactory;
}

@Override
public void afterEach(ExtensionContext testExtensionContext) throws Exception {
if (server == null || !server.isRunning()) {
Expand All @@ -46,38 +57,32 @@ public boolean supportsParameter(ParameterContext parameterContext, ExtensionCon
}

@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext context) {
Validate.validState(
!Optional.ofNullable(server).map(WireMockServer::isRunning).orElse(false),
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
validState(
!ofNullable(server).map(WireMockServer::isRunning).orElse(false),
"Can't inject more than one server"
);

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


try {
server = new WireMockServer(
mockedServer.factory().newInstance().create()
);
} catch (ReflectiveOperationException e) {
throw new ParameterResolutionException(
format("Can't create config with given factory %s", mockedServer.factory()),
e
);
}

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

CustomizationContext customizationContext = wiremockFactory.createContextBuilder().
parameterContext(parameterContext).
extensionContext(extensionContext).
build();

try {
mockedServer.customizer().newInstance().customize(server);
} catch (ReflectiveOperationException e) {
wiremockFactory.createCustomizer(mockedServer).customize(server, customizationContext);
} catch (Exception e) {
throw new ParameterResolutionException(
format("Can't customize server with given customizer %s", mockedServer.customizer()),
e
);
}

ExtensionContext.Store store = context.getStore(Namespace.create(WiremockResolver.class));
ExtensionContext.Store store = extensionContext.getStore(Namespace.create(WiremockResolver.class));
store.put(WIREMOCK_PORT, server.port());

log.info("Started wiremock server on localhost:{}", server.port());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.lanwen.wiremock.config;

import com.github.tomakehurst.wiremock.common.Slf4jNotifier;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import org.junit.jupiter.api.Test;
import ru.lanwen.wiremock.config.WiremockConfigFactory.DefaultWiremockConfigFactory;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* @author SourcePond (Roland Hauser)
*/
public class DefaultWiremockConfigFactoryTest {
private DefaultWiremockConfigFactory factory = new DefaultWiremockConfigFactory();

@Test
public void create() {
WireMockConfiguration config = factory.create();
assertEquals(0, config.portNumber());
assertEquals(Slf4jNotifier.class, config.notifier().getClass());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ru.lanwen.wiremock.config;

import com.github.tomakehurst.wiremock.WireMockServer;
import org.junit.jupiter.api.Test;
import ru.lanwen.wiremock.config.WiremockCustomizer.NoopWiremockCustomizer;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyZeroInteractions;

/**
* @author SourcePond (Roland Hauser)
*/
public class NoopWiremockCustomizerTest {
private WireMockServer server = mock(WireMockServer.class);
private CustomizationContext customizable = mock(CustomizationContext.class);
private WiremockCustomizer customizer = new NoopWiremockCustomizer();

@Test
public void customize() throws Exception {
customizer.customize(server);
verifyZeroInteractions(server, customizable);
}

@Test
public void customizeWithContext() throws Exception {
customizer.customize(server, customizable);
verifyZeroInteractions(server, customizable);
}
}
Loading

0 comments on commit dbebebb

Please sign in to comment.