package demo;

import com.jayway.restassured.RestAssured;
import com.palantir.docker.compose.DockerComposeRule;
import com.palantir.docker.compose.connection.DockerPort;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.util.function.Function;

import static com.jayway.restassured.RestAssured.get;
import static com.palantir.docker.compose.connection.waiting.HealthChecks.toRespondOverHttp;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isA;

public class IntegrationTest {

    private static final Function<DockerPort, String> TO_EXTERNAL_URI =
            (port) -> port.inFormat("http://$HOST:$EXTERNAL_PORT");

    @Rule
    public DockerComposeRule docker = DockerComposeRule.builder()
            .file("../docker-compose.yml")
            .waitingForService("greeting-service", toRespondOverHttp(8080, TO_EXTERNAL_URI))
            .waitingForService("counter-service", toRespondOverHttp(8080, TO_EXTERNAL_URI))
            .waitingForService("master-service", toRespondOverHttp(8080, TO_EXTERNAL_URI))
            .saveLogsTo("build/docker-logs")
            .build();

    @Before
    public void setUp() throws Exception {
        docker.dockerCompose().up();
        RestAssured.port = 8083;
    }

    @After
    public void tearDown() throws Exception {
        docker.dockerCompose().down();
    }

    @Test
    public void shouldReturnData() throws Exception {
        get("/info")
            .then().assertThat()
            .body("counter", isA(Number.class))
            .body("greeting", is("Hello World"));
    }

    @Test
    public void shouldReturnDefaultGreetingWhenGreetingServiceDown() throws Exception {
        docker.dockerCompose().container("greeting-service").stop();

        get("/info")
            .then().assertThat()
            .body("greeting", is("Hola!"));
    }

    @Test
    public void shouldReturnDefaultCountWhenCounterServiceDown() throws Exception {
        docker.dockerCompose().container("counter-service").stop();

        get("/info")
            .then().assertThat()
            .body("counter", is(42));
    }
}

@@ -184,84 +184,9 @@
 nit.
-After;%0Aimport org.junit.Before;%0Aimport org.junit.Rule;%0Aimport org.junit.Test
+*
 ;%0A%0Ai
@@ -482,16 +482,68 @@
 Test %7B%0A%0A
+    private static final int INTERNAL_PORT = 8080;%0A%0A
     priv
@@ -855,36 +855,45 @@
 RespondOverHttp(
-8080
+INTERNAL_PORT
 , TO_EXTERNAL_UR
@@ -956,36 +956,45 @@
 RespondOverHttp(
-8080
+INTERNAL_PORT
 , TO_EXTERNAL_UR
@@ -1064,20 +1064,29 @@
 verHttp(
-8080
+INTERNAL_PORT
 , TO_EXT
@@ -1261,39 +1261,179 @@
 ();%0A
-        RestAssured.port = 8083
+%0A        DockerPort masterService = docker.containers().container(%22master-service%22).port(INTERNAL_PORT);%0A        RestAssured.baseURI = TO_EXTERNAL_URI.apply(masterService)
 ;%0A  

Use master service's uri as rest-assured base uri