package de.hsadmin.remote;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

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

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import de.hsadmin.core.qserv.CommandShell;
import de.hsadmin.core.qserv.ShellException;
import de.hsadmin.core.util.Config;
import de.hsadmin.hostsharing.BasePacType;

public class SSLCertDomainTest {

	private static final String MODULE = "pac";
	
	private XmlRpcClient client;
	private RemoteCASHelper cas;
	private Config config;

	@Before
	public void setUp() throws Exception {
		client = RemoteTestHelper.getClient();
		cas = new RemoteCASHelper();
		config = Config.getInstance();
	}

	@After
	public void tearDown() throws Exception {
		client = null;
		cas = null;
		config = null;
	}

	@Test
	public void testSSLCertWithoutChain() {
		String user = "ad";
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, String> setParams = new HashMap<String, String>();
		setParams.put("name", "aaa02");
		setParams.put("hive", "h99");
		setParams.put("customer", config.getProperty("accountprefix.customer") + "-aaa");
		setParams.put("basepac", BasePacType.PAC_WEB);
		setParams.put("curinetaddr", "176.9.242.77");
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				setParams };
		try {
			Object execute = client.execute(MODULE + ".add", params);
			assertTrue(execute instanceof Map<?, ?>);
			Thread.sleep(5000L);
			String out = CommandShell.execute("( ls /var/local/lxc/hive/etc/apache2/sites-generated/aaa02.hostsharing.net >/dev/null 2>&1 " +
					" && grep 'SSLCertificateChainFile' /var/local/lxc/hive/etc/apache2/sites-generated/aaa02.hostsharing.net >/dev/null 2>&1 " +
					" && echo fail )" +
					" || echo Ok");
			assertTrue(out.startsWith("Ok"));		
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		} catch (ShellException e) {
			fail(e.getMessage());
		} catch (InterruptedException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testSSLCertWithChain() {
		String user = "ad";
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, String> setParams = new HashMap<String, String>();
		setParams.put("name", "aaa03");
		setParams.put("hive", "h99");
		setParams.put("customer", config.getProperty("accountprefix.customer") + "-aaa");
		setParams.put("basepac", BasePacType.PAC_DW);
		setParams.put("curinetaddr", "176.9.242.78");
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				setParams };
		try {
			Object execute = client.execute(MODULE + ".add", params);
			assertTrue(execute instanceof Map<?, ?>);
			Thread.sleep(5000L);
			String out = CommandShell.execute("( ls /var/local/lxc/hive/etc/apache2/sites-generated/aaa03.hostsharing.net >/dev/null 2>&1 " +
					" && grep 'SSLCertificate.*default' /var/local/lxc/hive/etc/apache2/sites-generated/aaa03.hostsharing.net >/dev/null 2>&1 " +
					" && echo fail )" +
					" || echo Ok");
			assertTrue(out.startsWith("Ok"));		
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		} catch (ShellException e) {
			fail(e.getMessage());
		} catch (InterruptedException e) {
			fail(e.getMessage());
		}
	}

}
