/*
 * Copyright 2016 Palantir Technologies, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.palantir.docker.compose;

import com.palantir.docker.compose.connection.Container;
import com.palantir.docker.compose.connection.State;
import com.palantir.docker.compose.connection.waiting.ClusterHealthCheck;
import com.palantir.docker.compose.connection.waiting.ClusterWait;
import com.palantir.docker.compose.connection.waiting.HealthChecks;
import com.palantir.docker.compose.execution.DockerExecutionException;
import org.joda.time.Duration;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.IOException;

import static com.palantir.docker.compose.connection.waiting.ClusterHealthCheck.serviceHealthCheck;
import static com.palantir.docker.compose.connection.waiting.HealthChecks.toHaveAllPortsOpen;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

public class DockerComposeRuleUpContainerIntegrationTest {

    private static final String DOCKER_COMPOSE_YAML_PATH = "src/test/resources/docker-compose.yaml";

    private DockerComposeRule dockerComposeRule;

    @Before
    public void setUp() throws Exception {
        dockerComposeRule = DockerComposeRule.builder().file(DOCKER_COMPOSE_YAML_PATH).build();
    }

    @After
    public void tearDown() throws Exception {
        dockerComposeRule.after();
    }

    @Test
    public void test_docker_compose_rule_up_container() throws IOException, InterruptedException {
        Container container = dockerComposeRule.containers().container("db");

        container.up();

        assertThat(container.state(), is(State.Up));
    }

    @Test
    public void test_docker_compose_rule_up_container_with_healthcheck() throws IOException, InterruptedException {
        Container container = dockerComposeRule.containers().container("db");

        container.up();

        // to prove that we can use healthcheck manually after starting a single container
        new ClusterWait(serviceHealthCheck("db", toHaveAllPortsOpen()), Duration.standardSeconds(5))
                .waitUntilReady(dockerComposeRule.containers());

        assertThat(container.state(), is(State.Up));
    }
}

@@ -662,32 +662,39 @@
 ompose;%0A%0Aimport 
+static 
 com.palantir.doc
@@ -720,70 +720,53 @@
 ion.
-Container;%0Aimport com.palantir.docker.compose.connection.State
+waiting.ClusterHealthCheck.serviceHealthCheck
 ;%0Aim
@@ -762,32 +762,39 @@
 thCheck;%0Aimport 
+static 
 com.palantir.doc
@@ -816,39 +816,32 @@
 nection.waiting.
-Cluster
 HealthCheck;%0Aimp
@@ -839,145 +839,123 @@
 heck
-;%0Aimport com.palantir.docker.compose.connection.waiting.ClusterWait;%0Aimport com.palantir.docker.compose.connection.waiting.HealthCheck
+s.toHaveAllPortsOpen;%0Aimport static org.hamcrest.MatcherAssert.assertThat;%0Aimport static org.hamcrest.core.Is.i
 s;%0A
+%0A
 impo
@@ -989,526 +989,279 @@
 ose.
-execution.DockerEx
+conn
 ec
-u
 tion
-Exception;%0Aimport org.joda.time.Duration;%0Aimport org.junit.After;%0Aimport org.junit.Before;%0Aimport org.junit.Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.ExpectedException;%0A%0Aimport java.io.IOException;%0A%0Aimport static com.palantir.docker.compose.connection.waiting.ClusterHealthCheck.serviceHealthCheck;%0Aimport static com.palantir.docker.compose.connection.waiting.HealthChecks.toHaveAllPortsOpen;%0Aimport static org.hamcrest.MatcherAssert.assertThat;%0Aimport static org.hamcrest.core.Is.is
+.Container;%0Aimport com.palantir.docker.compose.connection.State;%0Aimport com.palantir.docker.compose.connection.waiting.ClusterWait;%0Aimport java.io.IOException;%0Aimport org.joda.time.Duration;%0Aimport org.junit.After;%0Aimport org.junit.Before;%0Aimport org.junit.Test
 ;%0A%0Ap

Clean up imports in DockerComposeRuleUpContainerIntegrationTest