From 4931f31d915120ea3c4c312ebc88a71bdc7c65f2 Mon Sep 17 00:00:00 2001 From: Bahtiyar Kaba Date: Mon, 11 Jun 2018 23:19:11 +0300 Subject: [PATCH] Added docker compose tests for testcontainers module --- .../DockerComposeContainerTests.java | 42 +++++++++++++++++++ .../src/test/resources/test-compose.yml | 3 ++ 2 files changed, 45 insertions(+) create mode 100644 testing-modules/test-containers/src/test/java/com/baeldung/testconainers/DockerComposeContainerTests.java create mode 100644 testing-modules/test-containers/src/test/resources/test-compose.yml diff --git a/testing-modules/test-containers/src/test/java/com/baeldung/testconainers/DockerComposeContainerTests.java b/testing-modules/test-containers/src/test/java/com/baeldung/testconainers/DockerComposeContainerTests.java new file mode 100644 index 0000000000..32345a486c --- /dev/null +++ b/testing-modules/test-containers/src/test/java/com/baeldung/testconainers/DockerComposeContainerTests.java @@ -0,0 +1,42 @@ +package com.baeldung.testconainers; + +import static org.junit.Assert.assertEquals; + +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +import org.junit.ClassRule; +import org.junit.Test; +import org.testcontainers.containers.DockerComposeContainer; + +public class DockerComposeContainerTests { +@ClassRule +public static DockerComposeContainer compose = new DockerComposeContainer(new File("src/test/resources/test-compose.yml")) + .withExposedService("simpleWebServer_1", 80); + + @Test + public void when() throws Exception { + String address ="http://" + compose.getServiceHost("simpleWebServer_1", 80)+ ":"+ compose.getServicePort("simpleWebServer_1", 80); + String response = simpleGetRequest(address); + assertEquals(response, "Hello World!"); + } + + private String simpleGetRequest(String address) throws Exception { + URL url = new URL(address); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod("GET"); + + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); + String inputLine; + StringBuffer content = new StringBuffer(); + while ((inputLine = in.readLine()) != null) { + content.append(inputLine); + } + in.close(); + + return content.toString(); + } +} diff --git a/testing-modules/test-containers/src/test/resources/test-compose.yml b/testing-modules/test-containers/src/test/resources/test-compose.yml new file mode 100644 index 0000000000..3810c1c589 --- /dev/null +++ b/testing-modules/test-containers/src/test/resources/test-compose.yml @@ -0,0 +1,3 @@ +simpleWebServer: + image: alpine:3.2 + command: ["/bin/sh", "-c", "while true; do echo 'HTTP/1.1 200 OK\n\nHello World!' | nc -l -p 80; done"]