Implemented a narrow integration-test: DemoApplicationIT
[demos/testing] / src / test / java / de / juplo / demo / DemoApplicationIT.java
diff --git a/src/test/java/de/juplo/demo/DemoApplicationIT.java b/src/test/java/de/juplo/demo/DemoApplicationIT.java
new file mode 100644 (file)
index 0000000..15d3382
--- /dev/null
@@ -0,0 +1,158 @@
+package de.juplo.demo;
+
+import static de.juplo.demo.IntegrationTestBase.MOCK_SERVER;
+import java.util.regex.Pattern;
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Document;
+import org.junit.jupiter.api.DisplayName;
+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.EnableAutoConfiguration;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.http.MediaType;
+import org.springframework.test.web.reactive.server.WebTestClient;
+import org.springframework.web.reactive.function.client.WebClient;
+
+@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
+@EnableAutoConfiguration
+class DemoApplicationIT extends IntegrationTestBase
+{
+  static Pattern MISSING_PATTERN =
+      Pattern.compile("^404\\ Not\\ Found\\ from\\ GET\\ http:\\/\\/localhost:[0-9]*/missing.txt$");
+
+
+  @Autowired
+  WebTestClient webClient;
+
+
+  @Test
+  @DisplayName("HtmlController - Remote-Response: 200")
+  void testHtmlControllerResponse200() throws Exception
+  {
+    MOCK_SERVER
+        .when(request().withPath("/test.txt"), exactly(1))
+        .forward(forward()
+            .withHost(NGINX.getContainerIpAddress())
+            .withPort(NGINX.getMappedPort(80)));
+
+    webClient
+        .get()
+        .uri("/?path=test.txt")
+        .exchange()
+        .expectStatus().isOk()
+        .expectHeader().contentTypeCompatibleWith(MediaType.TEXT_HTML)
+        .expectBody(String.class).value(rendered ->
+        {
+          Document doc = Jsoup.parse(rendered);
+          assertThat(
+              doc.select("html > body > main > div > div > div > pre").text())
+              .isEqualTo("Hello World!");
+        });
+  }
+
+  @Test
+  @DisplayName("HtmlController - Remote-Response: 404")
+  void testHtmlControllerResponse404() throws Exception
+  {
+    MOCK_SERVER
+        .when(request().withPath("/missing.txt"), exactly(1))
+        .forward(forward()
+            .withHost(NGINX.getContainerIpAddress())
+            .withPort(NGINX.getMappedPort(80)));
+
+    webClient
+        .get()
+        .uri("/?path=missing.txt")
+        .exchange()
+        .expectStatus().isOk()
+        .expectHeader().contentTypeCompatibleWith(MediaType.TEXT_HTML)
+        .expectBody(String.class).value(rendered ->
+        {
+          Document doc = Jsoup.parse(rendered);
+          assertThat(
+              doc.select("html > body > main > div > div > div > pre").text())
+              .matches(MISSING_PATTERN);
+        });
+  }
+
+  @Test
+  @DisplayName("RestController - Remote-Response: 200")
+  void testRestController200() throws Exception
+  {
+    MOCK_SERVER
+        .when(request().withPath("/test.txt"), exactly(1))
+        .forward(forward()
+            .withHost(NGINX.getContainerIpAddress())
+            .withPort(NGINX.getMappedPort(80)));
+
+    webClient
+        .get()
+        .uri("/?path=test.txt")
+        .header("Accept", MediaType.TEXT_PLAIN_VALUE)
+        .exchange()
+        .expectStatus().isOk()
+        .expectHeader().contentTypeCompatibleWith(MediaType.TEXT_PLAIN)
+        .expectBody(String.class).isEqualTo("Hello World!\n");
+  }
+
+  @Test
+  @DisplayName("RestController - Remote-Response: 404")
+  void testRestController404() throws Exception
+  {
+    MOCK_SERVER
+        .when(request().withPath("/missing.txt"), exactly(1))
+        .forward(forward()
+            .withHost(NGINX.getContainerIpAddress())
+            .withPort(NGINX.getMappedPort(80)));
+
+    webClient
+        .get()
+        .uri("/?path=missing.txt")
+        .header("Accept", MediaType.TEXT_PLAIN_VALUE)
+        .exchange()
+        .expectStatus().isNotFound()
+        .expectHeader().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)
+        .expectBody()
+        .jsonPath("status").isEqualTo(404)
+        .jsonPath("error").isEqualTo("Not Found")
+        .jsonPath("message").isEqualTo(
+            "Cause: 404 Not Found from GET http://localhost:" +
+            MOCK_SERVER.getLocalPort() +
+            "/missing.txt")
+        .jsonPath("timestamp").exists();
+
+  }
+
+  @Configuration
+  static class Application
+  {
+    @Bean
+    RemoteContentService service()
+    {
+      return new RemoteContentService(
+          WebClient
+              .builder()
+              .baseUrl("http://localhost:" + MOCK_SERVER.getLocalPort())
+              .build());
+    }
+
+    @Bean
+    HtmlController htmlController(RemoteContentService service)
+    {
+      return new HtmlController(service);
+    }
+
+    @Bean
+    RestController restController(RemoteContentService service)
+    {
+      return new RestController(service);
+    }
+  }
+}