-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Piotr Kulasek-Szwed
committed
Feb 18, 2020
1 parent
a803f88
commit 6eac1f7
Showing
6 changed files
with
146 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/mokka/src/main/java/pl/hycom/mokka/stubbing/WireMockServerConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/mokka/src/main/java/pl/hycom/mokka/stubbing/WireMockServerRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/mokka/src/test/java/pl/hycom/mokka/stubbing/WireMockServerRunnerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |