/*
 * The MIT License
 *
 * Copyright (c) 2017, Carlos Sanchez
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.csanchez.jenkins.plugins.kubernetes;

import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static io.fabric8.kubernetes.client.Config.*;
import io.fabric8.kubernetes.client.KubernetesClient;

/**
 * Test the creation of clients
 */
public class KubernetesFactoryAdapterTest {

    private static final String[] SYSTEM_PROPERTY_NAMES = new String[] { KUBERNETES_KUBECONFIG_FILE,
            KUBERNETES_NAMESPACE_FILE };

    private Map<String, String> systemProperties = new HashMap<>();

    @Before
    public void saveSystemProperties() {
        for (String key : SYSTEM_PROPERTY_NAMES) {
            systemProperties.put(key, System.getProperty(key));
            // use bogus values to avoid influence from environment
            System.setProperty(key, "src/test/resources/void");
        }
    }

    @After
    public void restoreSystemProperties() {
        for (String key : systemProperties.keySet()) {
            String s = systemProperties.get(key);
            if (s != null) {
                System.setProperty(key, systemProperties.get(key));
            } else {
                System.clearProperty(key);
            }
        }
    }

    @Test
    public void defaultNamespace() throws Exception {
        KubernetesFactoryAdapter factory = new KubernetesFactoryAdapter(null, null, null, false);
        KubernetesClient client = factory.createClient();
        assertEquals("default", client.getNamespace());
    }

    @Test
    public void autoConfigNamespace() throws Exception {
        System.setProperty(KUBERNETES_NAMESPACE_FILE, "src/test/resources/kubenamespace");
        KubernetesFactoryAdapter factory = new KubernetesFactoryAdapter(null, null, null, false);
        KubernetesClient client = factory.createClient();
        assertEquals("test-namespace", client.getNamespace());
    }
}

@@ -1608,16 +1608,31 @@
 ring%5B%5D %7B
+ //%0A           
  KUBERNE
@@ -1651,16 +1651,19 @@
 IG_FILE,
+ //
 %0A       
@@ -1692,18 +1692,555 @@
 ACE_FILE
- %7D
+, //%0A            KUBERNETES_HTTP_PROXY, //%0A            KUBERNETES_HTTPS_PROXY, //%0A            KUBERNETES_NO_PROXY, //%0A            KUBERNETES_PROXY_USERNAME, //%0A            KUBERNETES_PROXY_PASSWORD //%0A    %7D;%0A%0A    private static final String HTTP_PROXY = %22http://example.com:123%22;%0A    private static final String HTTPS_PROXY = %22https://example.com:123%22;%0A    private static final String NO_PROXY = %22noproxy%22;%0A    private static final String PROXY_USERNAME = %22proxy_username%22;%0A    private static final String PROXY_PASSWORD = %22proxy_password%22
 ;%0A%0A    p
@@ -2604,32 +2604,397 @@
 oid%22);%0A        %7D
+%0A%0A        // proxy system properties%0A        System.setProperty(KUBERNETES_HTTP_PROXY, HTTP_PROXY);%0A        System.setProperty(KUBERNETES_HTTPS_PROXY, HTTPS_PROXY);%0A        System.setProperty(KUBERNETES_NO_PROXY, NO_PROXY);%0A        System.setProperty(KUBERNETES_PROXY_USERNAME, PROXY_USERNAME);%0A        System.setProperty(KUBERNETES_PROXY_PASSWORD, PROXY_PASSWORD);
 %0A    %7D%0A%0A    @Aft
@@ -3656,25 +3656,16 @@
 toConfig
-Namespace
 () throw
@@ -3917,32 +3917,32 @@
 createClient();%0A
-
         assertEq
@@ -3984,20 +3984,1102 @@
 mespace());%0A
+        assertEquals(HTTP_PROXY, client.getConfiguration().getHttpProxy());%0A        assertEquals(HTTPS_PROXY, client.getConfiguration().getHttpsProxy());%0A        assertArrayEquals(new String%5B%5D %7B NO_PROXY %7D, client.getConfiguration().getNoProxy());%0A        assertEquals(PROXY_USERNAME, client.getConfiguration().getProxyUsername());%0A        assertEquals(PROXY_PASSWORD, client.getConfiguration().getProxyPassword());%0A    %7D%0A%0A    @Test%0A    public void autoConfigWithMasterUrl() throws Exception %7B%0A        KubernetesFactoryAdapter factory = new KubernetesFactoryAdapter(%22http://example.com%22, null, null, false);%0A        KubernetesClient client = factory.createClient();%0A        assertEquals(HTTP_PROXY, client.getConfiguration().getHttpProxy());%0A        assertEquals(HTTPS_PROXY, client.getConfiguration().getHttpsProxy());%0A        assertArrayEquals(new String%5B%5D %7B NO_PROXY %7D, client.getConfiguration().getNoProxy());%0A        assertEquals(PROXY_USERNAME, client.getConfiguration().getProxyUsername());%0A        assertEquals(PROXY_PASSWORD, client.getConfiguration().getProxyPassword());%0A
     %7D%0A%7D%0A

Add test for proxy configuration