-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add MockServer module * CHANGELOG.md * remove delegate
- Loading branch information
Showing
4 changed files
with
81 additions
and
0 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
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,8 @@ | ||
description = "Testcontainers :: MockServer" | ||
|
||
dependencies { | ||
compile project(':testcontainers') | ||
|
||
compileOnly 'org.mock-server:mockserver-client-java:5.3.0' | ||
testCompile 'org.mock-server:mockserver-client-java:5.3.0' | ||
} |
38 changes: 38 additions & 0 deletions
38
modules/mockserver/src/main/java/org/testcontainers/containers/MockServerContainer.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,38 @@ | ||
package org.testcontainers.containers; | ||
|
||
import com.github.dockerjava.api.command.InspectContainerResponse; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.mockserver.client.server.MockServerClient; | ||
|
||
@Slf4j | ||
public class MockServerContainer extends GenericContainer<MockServerContainer> { | ||
|
||
public static final String VERSION = "5.3.0"; | ||
|
||
public static final int PORT = 80; | ||
|
||
@Getter | ||
private MockServerClient client; | ||
|
||
public MockServerContainer() { | ||
this(VERSION); | ||
} | ||
|
||
public MockServerContainer(String version) { | ||
super("jamesdbloom/mockserver:mockserver-" + version); | ||
withCommand("/opt/mockserver/run_mockserver.sh -logLevel INFO -serverPort " + PORT); | ||
addExposedPorts(PORT); | ||
} | ||
|
||
@Override | ||
protected void containerIsStarted(InspectContainerResponse containerInfo) { | ||
super.containerIsStarted(containerInfo); | ||
|
||
client = new MockServerClient(getContainerIpAddress(), getMappedPort(PORT)); | ||
} | ||
|
||
public String getEndpoint() { | ||
return String.format("http://%s:%d", getContainerIpAddress(), getMappedPort(PORT)); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
modules/mockserver/src/test/java/org/testcontainers/containers/MockServerContainerTest.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,34 @@ | ||
package org.testcontainers.containers; | ||
|
||
import lombok.Cleanup; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
|
||
import static org.mockserver.model.HttpRequest.request; | ||
import static org.mockserver.model.HttpResponse.response; | ||
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue; | ||
|
||
public class MockServerContainerTest { | ||
|
||
@ClassRule | ||
public static MockServerContainer mockServer = new MockServerContainer(); | ||
|
||
@Test | ||
public void testBasicScenario() throws Exception { | ||
mockServer.getClient() | ||
.when(request("/hello")) | ||
.respond(response("Hello World!")); | ||
|
||
URLConnection urlConnection = new URL(mockServer.getEndpoint() + "/hello").openConnection(); | ||
@Cleanup BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); | ||
String line = reader.readLine(); | ||
System.out.println(line); | ||
|
||
assertTrue("MockServer returns correct result", line.contains("Hello World!")); | ||
} | ||
} |