package io.fabric8.maven.docker.access;

import java.io.File;
import java.io.IOException;

import org.apache.maven.plugin.MojoExecutionException;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class DockerConnectionDetectorTest {

    DockerConnectionDetector detector = new DockerConnectionDetector(null);

    @Test
    public void testGetUrlFromHostConfig() throws MojoExecutionException, IOException {
        DockerConnectionDetector.ConnectionParameter param = detector.detectConnectionParameter("hostconfig", "certpath");
        assertEquals("hostconfig", param.getUrl());
        assertEquals("certpath", param.getCertPath());
    }

    @Test
    public void testGetUrlFromEnvironment() throws MojoExecutionException, IOException {
        String dockerHost = System.getenv("DOCKER_HOST");
        if (dockerHost != null) {
            assertEquals(dockerHost.replaceFirst("^tcp:/",""), detector.detectConnectionParameter(null, null).getUrl().replaceFirst("^https?:/", ""));
        } else if (System.getProperty("os.name").equalsIgnoreCase("Windows 10")) {
        	try {
                assertEquals("npipe:////./pipe/docker_engine", detector.detectConnectionParameter(null, null).getUrl());
            } catch (IllegalArgumentException expectedIfNoUnixSocket) {
            }
        } else {
            try {
                assertEquals("unix:///var/run/docker.sock", detector.detectConnectionParameter(null, null).getUrl());
            } catch (IllegalArgumentException expectedIfNoUnixSocket) {
            }
        }
    }

    @Test
    public void testOrderDefaultDockerHostProviders() {
        Class[] expectedProviders = new Class[] {
            DockerConnectionDetector.EnvDockerHostProvider.class,
            DockerConnectionDetector.UnixSocketDockerHostProvider.class,
            DockerConnectionDetector.WindowsPipeDockerHostProvider.class
        };

        int i = 0;
        for (DockerConnectionDetector.DockerHostProvider provider : detector.dockerHostProviders) {
            assertEquals(expectedProviders[i++], provider.getClass());
        }
    }

    @Test
    public void testGetCertPathFromEnvironment() throws MojoExecutionException, IOException {
        String certPath = System.getenv("DOCKER_CERT_PATH");
        DockerConnectionDetector.ConnectionParameter param = detector.detectConnectionParameter(null, null);
        if (certPath != null) {
            assertEquals(certPath, param.getCertPath());
        } else {
            String maybeUserDocker = param.getCertPath();
            if (maybeUserDocker != null) {
                assertEquals(new File(System.getProperty("user.home"), ".docker").getAbsolutePath(),
                        maybeUserDocker);
            }
        }
    }

    // any further testing requires a 'docker-machine' on the build host
}

@@ -2232,61 +2232,18 @@
     
-S
 tr
-ing certPath = System.getenv(%22DOCKER_CERT_PATH%22);%0A
+y %7B%0A    
     
@@ -2347,16 +2347,85 @@
  null);%0A
+            String certPath = System.getenv(%22DOCKER_CERT_PATH%22);%0A    
         
@@ -2440,32 +2440,36 @@
 Path != null) %7B%0A
+    
             asse
@@ -2509,32 +2509,36 @@
 ath());%0A        
+ 
+   
 %7D else %7B%0A       
@@ -2522,32 +2522,36 @@
        %7D else %7B%0A
+    
             Stri
@@ -2600,24 +2600,28 @@
             
+   
+ 
 if (maybeUse
@@ -2631,32 +2631,36 @@
 cker != null) %7B%0A
+    
                 
@@ -2768,16 +2768,25 @@
         
+         
 maybeUse
@@ -2799,33 +2799,190 @@
 r);%0A            
-%7D
+    %7D%0A            %7D%0A        %7D catch (IllegalArgumentException exp) %7B%0A            // Can happen if there is now docker connection configured in the environment
 %0A        %7D%0A    %7D
@@ -3055,11 +3055,15 @@
 ild host
+ ...
 %0A%7D%0A

Fix test when no docker is installed