diff --git a/src/mokka/pom.xml b/src/mokka/pom.xml index 865a552..526f227 100644 --- a/src/mokka/pom.xml +++ b/src/mokka/pom.xml @@ -49,6 +49,13 @@ runtime + + + com.github.tomakehurst + wiremock-standalone + 2.26.0 + + org.springframework.boot spring-boot-starter-activemq @@ -85,7 +92,6 @@ postgresql - org.projectlombok diff --git a/src/mokka/src/main/java/pl/hycom/mokka/stubbing/WireMockServerConfiguration.java b/src/mokka/src/main/java/pl/hycom/mokka/stubbing/WireMockServerConfiguration.java new file mode 100644 index 0000000..db6f008 --- /dev/null +++ b/src/mokka/src/main/java/pl/hycom/mokka/stubbing/WireMockServerConfiguration.java @@ -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 + */ +@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; + } +} diff --git a/src/mokka/src/main/java/pl/hycom/mokka/stubbing/WireMockServerRunner.java b/src/mokka/src/main/java/pl/hycom/mokka/stubbing/WireMockServerRunner.java new file mode 100644 index 0000000..a8d21b0 --- /dev/null +++ b/src/mokka/src/main/java/pl/hycom/mokka/stubbing/WireMockServerRunner.java @@ -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. + *

+ * May be disabled with `wiremock.enabled` property. + * + * @author Piotr Kulasek-Szwed + * @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()); + } +} diff --git a/src/mokka/src/main/resources/application-dev.properties b/src/mokka/src/main/resources/application-dev.properties index 799b84e..bffbc4e 100644 --- a/src/mokka/src/main/resources/application-dev.properties +++ b/src/mokka/src/main/resources/application-dev.properties @@ -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 = diff --git a/src/mokka/src/main/resources/application.properties b/src/mokka/src/main/resources/application.properties index 3680309..a6b34b4 100644 --- a/src/mokka/src/main/resources/application.properties +++ b/src/mokka/src/main/resources/application.properties @@ -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 diff --git a/src/mokka/src/test/java/pl/hycom/mokka/stubbing/WireMockServerRunnerTest.java b/src/mokka/src/test/java/pl/hycom/mokka/stubbing/WireMockServerRunnerTest.java new file mode 100644 index 0000000..50e4252 --- /dev/null +++ b/src/mokka/src/test/java/pl/hycom/mokka/stubbing/WireMockServerRunnerTest.java @@ -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 + */ +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()); + } + +}