Skip to content

Commit

Permalink
Embed Wiremock server #106
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Kulasek-Szwed committed Feb 18, 2020
1 parent a803f88 commit 6eac1f7
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/mokka/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@
<scope>runtime</scope>
</dependency>

<!-- ADR-0004 Should Wiremock be used as stubs engine -->
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>2.26.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
Expand Down Expand Up @@ -85,7 +92,6 @@
<artifactId>postgresql</artifactId>
</dependency>


<!-- Utilities -->
<dependency>
<groupId>org.projectlombok</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package pl.hycom.mokka.stubbing;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* Configuration bean related to {@link com.github.tomakehurst.wiremock.WireMockServer}.
*
* @author Piotr Kulasek-Szwed <[email protected]>
*/
@Configuration
public class WireMockServerConfiguration {

/**
* WireMock HTTP port to be exposed.
*/
@Value("${wiremock.httpPort}")
private int wiremockHttpPort;

/**
* Provides configured {@link com.github.tomakehurst.wiremock.WireMockServer}.
*
* @return
*/
@Bean
public WireMockServer wireMockOptions() {
WireMockConfiguration o = new WireMockConfiguration();
o.port(wiremockHttpPort);

WireMockServer server = new WireMockServer(o);
return server;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package pl.hycom.mokka.stubbing;

import com.github.tomakehurst.wiremock.WireMockServer;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;

/**
* Starts embedded WireMock server provided by {@link WireMockServerConfiguration} configuration bean.
* <p>
* May be disabled with `wiremock.enabled` property.
*
* @author Piotr Kulasek-Szwed <[email protected]>
* @see: ADR-0004 Should Wiremock be used as stubs engine
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class WireMockServerRunner implements CommandLineRunner {

private final WireMockServer server;

@Setter
@Value("${wiremock.enabled}")
private boolean wiremockEnabled;

@Override
public void run(String... args) throws Exception {
if (!wiremockEnabled) {
log.debug("Embedded Wiremock disabled by configuration.");
return;
}

log.info("Starting embedded Wiremock server.");

server.start();

log.info("Embedded Wiremock is running [{}] on port {}.", server.isRunning(), server.port());
}
}
2 changes: 2 additions & 0 deletions src/mokka/src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Datasource
spring.jpa.hibernate.ddl-auto = update

wiremock.enabled=false

# Reset configuration provided in application.properties as in development in-memory H2 database is used by default
# and Spring Boot handles the configuration on its own.
spring.datasource.url =
Expand Down
9 changes: 7 additions & 2 deletions src/mokka/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ file.contentDisposition=attachment
multipart.maxFileSize= 256MB
multipart.maxRequestSize= 256MB

# Stamping the version number and the build time of an application

mock.response.addDebugHeaders=true
setup.initial.enabled=true

# Embedded WireMock configuration

## Enable/Disable embedded WireMock instance - default should be false
wiremock.enabled=false
## Defines HTTP port on which Wiremock instance will be exposed
wiremock.httpPort=8082
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package pl.hycom.mokka.stubbing;

import com.github.tomakehurst.wiremock.WireMockServer;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
* @author Piotr Kulasek-Szwed <[email protected]>
*/
public class WireMockServerRunnerTest {

@Test
public void shouldNotStartWireMock() {
// given
WireMockServer server = new WireMockServer();
WireMockServerRunner runner = new WireMockServerRunner(server);
runner.setWiremockEnabled(false);

// when
try {
runner.run("");
} catch (Exception e) {
fail();
}

// then
assertFalse(server.isRunning());
}

@Test
public void shouldStartWireMock() {
// given
WireMockServer server = new WireMockServer();
WireMockServerRunner runner = new WireMockServerRunner(server);
runner.setWiremockEnabled(true);

// when
try {
runner.run("");
} catch (Exception e) {
fail();
}

// then
assertTrue(server.isRunning());
}

}

0 comments on commit 6eac1f7

Please sign in to comment.