WIP:integrationtest old
authorKai Moritz <kai@juplo.de>
Fri, 10 Jan 2020 19:07:51 +0000 (20:07 +0100)
committerKai Moritz <kai@juplo.de>
Sat, 11 Jan 2020 08:29:22 +0000 (09:29 +0100)
pom.xml
src/test/java/de/juplo/demo/IntegrationTestBase.java [new file with mode: 0644]
src/test/java/de/juplo/demo/IntegrationtestApplicationIT.java [new file with mode: 0644]
src/test/resources/index.html [new file with mode: 0644]
src/test/resources/test.txt [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 74fd65f..15775ad 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -16,6 +16,8 @@
 
        <properties>
                <java.version>1.8</java.version>
+    <mockserver.version>5.8.1</mockserver.version>
+    <testcontainers-nginx.version>1.12.4</testcontainers-nginx.version>
        </properties>
 
        <dependencies>
                                </exclusion>
                        </exclusions>
                </dependency>
+    <dependency>
+      <groupId>org.mock-server</groupId>
+      <artifactId>mockserver-netty</artifactId>
+      <version>${mockserver.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.testcontainers</groupId>
+      <artifactId>nginx</artifactId>
+      <version>${testcontainers-nginx.version}</version>
+      <scope>test</scope>
+    </dependency>
                <dependency>
                        <groupId>io.projectreactor</groupId>
                        <artifactId>reactor-test</artifactId>
                                <groupId>org.springframework.boot</groupId>
                                <artifactId>spring-boot-maven-plugin</artifactId>
                        </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-failsafe-plugin</artifactId>
+      </plugin>
                </plugins>
        </build>
 
diff --git a/src/test/java/de/juplo/demo/IntegrationTestBase.java b/src/test/java/de/juplo/demo/IntegrationTestBase.java
new file mode 100644 (file)
index 0000000..94a91a1
--- /dev/null
@@ -0,0 +1,77 @@
+package de.juplo.demo;
+
+
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.mockserver.integration.ClientAndServer;
+import org.testcontainers.containers.NginxContainer;
+import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;
+
+
+
+/**
+ * This class starts a
+ * {@link https://www.testcontainers.org/modules/nginx/ NginX-Webserver}
+ * via a {@link https://www.testcontainers.org/ Testcontainer} and a
+ * standalone {@link http://www.mock-server.com/#what-is-mockserver MockServer},
+ * that can be used to intercept requests for assertions / verifications.
+ * <p>
+ * We use the {@link
+ * https://www.testcontainers.org/test_framework_integration/manual_lifecycle_control/#singleton-containers
+ * Singleton Pattern} to start the NginX-Testcontainer and start the MockServer
+ * programmatically via the {@link
+ * http://www.mock-server.com/mock_server/running_mock_server.html#client_api
+ * Client-API}.
+ * @author Kai Moritz
+ */
+public abstract class IntegrationTestBase
+{
+  static final NginxContainer NGINX;
+  static final ClientAndServer MOCK_SERVER;
+
+
+  static
+  {
+    NGINX = new NginxContainer().withCustomContent("src/test/resources/");
+    MOCK_SERVER = ClientAndServer.startClientAndServer();
+  }
+
+
+  @BeforeAll
+  static void startMockServer()
+  {
+    NGINX.waitingFor(new HttpWaitStrategy()).start();
+  }
+
+  @AfterAll
+  static void stopMockServer()
+  {
+    MOCK_SERVER.stop();
+  }
+
+
+  String getNginxIP()
+  {
+    return NGINX.getContainerIpAddress();
+  }
+
+  int getNginxPort()
+  {
+    return NGINX.getMappedPort(80);
+  }
+
+  String getNginxUri()
+  {
+    return "http://" + getNginxIP() + ":" + getNginxPort();
+  }
+
+  int getMockServerPort()
+  {
+    return MOCK_SERVER.getLocalPort();
+  }
+
+  String getMockServerUri()
+  {
+    return "http://localhost:" + getMockServerPort();
+  }
+}
diff --git a/src/test/java/de/juplo/demo/IntegrationtestApplicationIT.java b/src/test/java/de/juplo/demo/IntegrationtestApplicationIT.java
new file mode 100644 (file)
index 0000000..6b0b8d9
--- /dev/null
@@ -0,0 +1,48 @@
+package de.juplo.demo;
+
+import de.juplo.integrationtest.IntegrationtestApplicationIT.Application;
+import java.net.URI;
+import org.junit.jupiter.api.Test;
+import static org.mockserver.matchers.Times.exactly;
+import static org.mockserver.model.HttpForward.forward;
+import static org.mockserver.model.HttpRequest.request;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.Bean;
+import org.springframework.test.web.servlet.MockMvc;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+@SpringBootTest(classes = Application.class)
+@AutoConfigureWebMvc
+class IntegrationtestApplicationIT extends IntegrationTestBase {
+
+  @Autowired
+  MockMvc mockMvc;
+
+
+  @Test
+       void contextLoads() throws Exception {
+    MOCK_SERVER
+        .when(request().withPath("/?path=test.txt"), exactly(1))
+        .forward(forward().withHost(getNginxIP()).withPort(getNginxPort()));
+    mockMvc
+        .perform(get(URI.create("http://sut/?path=test.txt")))
+        .andExpect(status().isOk());
+       }
+
+  @SpringBootApplication
+  static class Application {
+
+    @Bean
+    public RemoteContentController remoteContentController()
+    {
+      return new RemoteContentController(
+          "http://localhost:" +
+          IntegrationtestApplicationIT.MOCK_SERVER.getLocalPort()
+          );
+    }
+  }
+}
diff --git a/src/test/resources/index.html b/src/test/resources/index.html
new file mode 100644 (file)
index 0000000..f747925
--- /dev/null
@@ -0,0 +1 @@
+DUMMY
diff --git a/src/test/resources/test.txt b/src/test/resources/test.txt
new file mode 100644 (file)
index 0000000..980a0d5
--- /dev/null
@@ -0,0 +1 @@
+Hello World!