/*
 * 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.configuration.DockerComposeFiles;
import com.palantir.docker.compose.connection.ContainerCache;
import com.palantir.docker.compose.connection.DockerMachine;
import com.palantir.docker.compose.connection.DockerPort;
import com.palantir.docker.compose.connection.waiting.ServiceWait;
import com.palantir.docker.compose.execution.DefaultDockerCompose;
import com.palantir.docker.compose.execution.DockerCompose;
import com.palantir.docker.compose.logging.LogCollector;
import org.junit.rules.ExternalResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;

import static com.google.common.collect.ImmutableList.copyOf;

public class DockerComposition extends ExternalResource {

    private static final Logger log = LoggerFactory.getLogger(DockerComposition.class);

    private final DockerCompose dockerCompose;
    private final ContainerCache containers;
    private final List<ServiceWait> serviceWaits;
    private final LogCollector logCollector;

    public DockerComposition(
            DockerCompose dockerCompose,
            List<ServiceWait> serviceWaits,
            LogCollector logCollector,
            ContainerCache containers) {
        this.dockerCompose = dockerCompose;
        this.serviceWaits = copyOf(serviceWaits);
        this.logCollector = logCollector;
        this.containers = containers;
    }

    @Override
    public void before() throws IOException, InterruptedException {
        log.debug("Starting docker-compose cluster");
        dockerCompose.build();
        dockerCompose.up();

        log.debug("Starting log collection");

        logCollector.startCollecting(dockerCompose);
        log.debug("Waiting for services");
        serviceWaits.forEach(ServiceWait::waitTillServiceIsUp);
        log.debug("docker-compose cluster started");
    }

    @Override
    public void after() {
        try {
            log.debug("Killing docker-compose cluster");
            dockerCompose.down();
            dockerCompose.kill();
            dockerCompose.rm();
            logCollector.stopCollecting();
        } catch (IOException | InterruptedException e) {
            throw new RuntimeException("Error cleaning up docker compose cluster", e);
        }
    }

    public DockerPort portOnContainerWithExternalMapping(String container, int portNumber) throws IOException, InterruptedException {
        return containers.get(container)
                         .portMappedExternallyTo(portNumber);
    }

    public DockerPort portOnContainerWithInternalMapping(String container, int portNumber) throws IOException, InterruptedException {
        return containers.get(container)
                         .portMappedInternallyTo(portNumber);
    }

    public static DockerCompositionBuilder of(String dockerComposeFile) {
        return of(DockerComposeFiles.from(dockerComposeFile));
    }

    public static DockerCompositionBuilder of(DockerComposeFiles dockerComposeFiles) {
        return of(dockerComposeFiles, DockerMachine.localMachine().build());
    }

    public static DockerCompositionBuilder of(String dockerComposeFile, DockerMachine dockerMachine) {
        return of(DockerComposeFiles.from(dockerComposeFile), dockerMachine);
    }

    public static DockerCompositionBuilder of(DockerComposeFiles dockerComposeFiles, DockerMachine dockerMachine) {
        return of(new DefaultDockerCompose(dockerComposeFiles, dockerMachine));
    }

    public static DockerCompositionBuilder of(DockerCompose executable) {
        return new DockerCompositionBuilder(executable);
    }

}

@@ -663,16 +663,64 @@
 mpose;%0A%0A
+import com.google.common.collect.ImmutableList;%0A
 import c
@@ -780,16 +780,16 @@
 eFiles;%0A
-
 import c
@@ -1367,71 +1367,8 @@
 t;%0A%0A
-import static com.google.common.collect.ImmutableList.copyOf;%0A%0A
 publ
@@ -1934,24 +1934,24 @@
 kerCompose;%0A
-
         this
@@ -1966,16 +1966,30 @@
 Waits = 
+ImmutableList.
 copyOf(s

Use qualified name