package cz.xtf.openshift;

import cz.xtf.TestConfiguration;

import java.net.MalformedURLException;

public class OpenShiftUtils {
	private static OpenShiftUtil adminUtil;
	private static OpenShiftUtil masterUtil;

	public static OpenShiftUtil admin() {
		if(adminUtil == null) {
			String masterUrl = TestConfiguration.masterUrl();
			String namespace = TestConfiguration.masterNamespace();
			String username = TestConfiguration.adminUsername();
			String password = TestConfiguration.adminPassword();

			adminUtil = getUtil(masterUrl, namespace, username, password);
		}
		return adminUtil;
	}

	public static OpenShiftUtil admin(String namespace) {
		String masterUrl = TestConfiguration.masterUrl();
		String username = TestConfiguration.adminUsername();
		String password = TestConfiguration.adminPassword();

		return getUtil(masterUrl, namespace, username, password);
	}

	public static OpenShiftUtil master() {
		if(masterUtil == null) {
		    if(!TestConfiguration.openshiftOnline()) {
		        String masterUrl = TestConfiguration.masterUrl();
		        String namespace = TestConfiguration.masterNamespace();
		        String username = TestConfiguration.masterUsername();
		        String password = TestConfiguration.masterPassword();

		        masterUtil = getUtil(masterUrl, namespace, username, password);
		    } else {
		        String namespace = TestConfiguration.masterNamespace();
		        String masterUrl = TestConfiguration.masterUrl();
		        masterUtil = getUtil(masterUrl, namespace, TestConfiguration.getMasterToken());
		    }
		}
		return masterUtil;
	}

	public static OpenShiftUtil master(String namespace) {
		String masterUrl = TestConfiguration.masterUrl();
		if(!TestConfiguration.openshiftOnline()) {
			String username = TestConfiguration.masterUsername();
			String password = TestConfiguration.masterPassword();
			return getUtil(masterUrl, namespace, username, password);
		} else {
			return getUtil(masterUrl, namespace, TestConfiguration.getMasterToken());
		}
	}

	public static OpenShiftUtil getUtil(String masterUrl, String namespace, String username, String password) {
		try {
			return new OpenShiftUtil(masterUrl, namespace, username, password);
		} catch (MalformedURLException e) {
			throw new IllegalStateException("OpenShift Master URL is malformed!");
		}
	}

	public static OpenShiftUtil getUtil(String masterUrl, String namespace, String token) {
		try {
			return new OpenShiftUtil(masterUrl, namespace, token);
		} catch (MalformedURLException e) {
			throw new IllegalStateException("OpenShift Master URL is malformed!");
		}
	}
}

@@ -31,74 +31,301 @@
 ort 
-cz.xtf.TestConfiguration;%0A%0Aimport java.net.MalformedURLException;%0A
+org.apache.commons.lang3.StringUtils;%0Aimport org.apache.http.Header;%0A%0Aimport cz.xtf.TestConfiguration;%0Aimport cz.xtf.http.HttpClient;%0Aimport lombok.extern.slf4j.Slf4j;%0A%0Aimport java.io.IOException;%0Aimport java.net.MalformedURLException;%0Aimport java.util.List;%0Aimport java.util.Optional;%0A%0A@Slf4j
 %0Apub
@@ -2819,11 +2819,1193 @@
 ;%0A%09%09%7D%0A%09%7D
+%0A%0A%09public static String getMasterToken() %7B%0A%09%09if(TestConfiguration.openshiftOnline() %7C%7C TestConfiguration.getMasterToken() != null) %7B%0A%09%09%09return TestConfiguration.getMasterToken();%0A%09%09%7D%0A%09%09else %7B%0A%09%09%09String masterUrl = TestConfiguration.masterUrl();%0A%09%09%09String username = TestConfiguration.masterUsername();%0A%09%09%09String password = TestConfiguration.masterPassword();%0A%0A%09%09%09try %7B%0A%09%09%09%09String url = masterUrl + %22/oauth/authorize?response_type=token&client_id=openshift-challenging-client%22;%0A%09%09%09%09List%3CHeader%3E headers = HttpClient.get(url)%0A%09%09%09%09%09%09.basicAuth(username, password)%0A%09%09%09%09%09%09.preemptiveAuth()%0A%09%09%09%09%09%09.disableRedirect()%0A%09%09%09%09%09%09.responseHeaders();%0A%09%09%09%09Optional%3CHeader%3E location = headers.stream().filter(h -%3E %22Location%22.equals(h.getName())).findFirst();%0A%09%09%09%09if (location.isPresent()) %7B%0A%09%09%09%09%09log.debug(%22Location: %7B%7D%22, location.get().getValue());%0A%09%09%09%09%09String token = StringUtils.substringBetween(location.get().getValue(), %22#access_token=%22, %22&%22);%0A%09%09%09%09%09log.debug(%22Oauth token: %7B%7D%22, token);%0A%09%09%09%09%09return token;%0A%09%09%09%09%7D%0A%09%09%09%09log.debug(%22Location header with token not found%22);%0A%09%09%09%09return null;%0A%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09log.error(%22Error getting token from master%22, e);%0A%09%09%09%09return null;%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

add getMasterToken static method