package de.hsadmin.remote;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
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.util.Config;
import de.hsadmin.hostsharing.BasePacType;

public class PacTest {

	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;
	}

	@Test
	public void testSearchAllAsPacAdmin() {
		String user = "aaa00";
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, String> whereParams = new HashMap<String, String>();
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				whereParams };
		try {
			Object execute = client.execute(MODULE + ".search", params);
			Object[] result = (Object[]) execute;
			assertEquals(1, result.length);
			for (Object o : result) {
				if (o instanceof Map<?, ?>) {
					Map<?, ?> row = (Map<?, ?>) o;
					assertEquals(config.getProperty("accountprefix.customer") + "-aaa", row.get("customer"));
				} else {
					fail("map expected");
				}
			}
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testUpdateByPacadminFails() {
		String user = "aaa00";
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, String> setParams = new HashMap<String, String>();
		Map<String, String> whereParams = new HashMap<String, String>();
		setParams.put("curinetaddr", "176.9.242.75");
		whereParams.put("name", "aaa00");
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				setParams, whereParams };
		try {
			Object execute = client.execute(MODULE + ".update", params);
			assertNotNull(execute);
			fail("exception expected");
		} catch (XmlRpcException e) {

		}
	}

	@Test
	public void testUpdateInvalidValueFails() {
		String user = "ad";
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, String> componentsMap = new HashMap<String,String>();
		componentsMap.put("QUOTA", "524");
		Map<String, Object> setParams = new HashMap<String,Object>();
		Map<String, Object> whereParams = new HashMap<String,Object>();
		setParams.put("components", componentsMap);
		whereParams.put("name", "aaa00");
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				setParams, whereParams };
		try {
			Object execute = client.execute(MODULE + ".update", params);
			assertNotNull(execute);
			fail("exception expected");
		} catch (XmlRpcException e) {
		}
	}

	@Test
	public void testUpdateValidValue() {
		String user = "ad";
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, String> componentsMap = new HashMap<String,String>();
		componentsMap.put("QUOTA", "4096");
		Map<String, Object> setParams = new HashMap<String,Object>();
		Map<String, Object> whereParams = new HashMap<String,Object>();
		setParams.put("components", componentsMap);
		whereParams.put("name", "aaa00");
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				setParams, whereParams };
		try {
			Object execute = client.execute(MODULE + ".update", params);
			assertNotNull(execute);
			params = new Object[] { user, 
					cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
					whereParams };
			execute = client.execute(MODULE + ".search", params);
			assertNotNull(execute);
			assertTrue(execute instanceof Object[]);
			Object[] untypedResultArray = (Object[]) execute;
			assertEquals(1, untypedResultArray.length);
			assertTrue(untypedResultArray[0] instanceof Map<?,?>);
			@SuppressWarnings("unchecked")
			Map<String, ?> pacResultMap = (Map<String, ?>) untypedResultArray[0]; 
			assertEquals(BasePacType.PAC_WEB, pacResultMap.get("basepac"));
			Object compMapObj = pacResultMap.get("components");
			assertTrue(compMapObj instanceof Map<?,?>);
			@SuppressWarnings("unchecked")
			Map<String,String> compMap = (Map<String, String>) compMapObj;
			assertEquals("4096", compMap.get("QUOTA"));
			assertEquals("5", compMap.get("TRAFFIC"));
			assertEquals("1", compMap.get("MULTI"));
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testCreateWeb() {
		int count = getPacsCount();
		String user = "ad";
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, Object> setParams = new HashMap<String, Object>();
		setParams.put("name", "aaa01");
		setParams.put("hive", "h99");
		setParams.put("customer", config.getProperty("accountprefix.customer") + "-aaa");
		setParams.put("basepac", BasePacType.PAC_WEB);
		setParams.put("curinetaddr", "176.9.242.76");
		HashMap<String, String> components = new HashMap<String, String>();
		components.put("QUOTA", "512");
		components.put("TRAFFIC", "5");
		components.put("DAEMON", "1");
		setParams.put("components", components);
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				setParams };
		try {
			Object execute = client.execute(MODULE + ".add", params);
			assertTrue(execute instanceof Map<?, ?>);
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
		assertEquals(count + 1, getPacsCount());
		setParams = new HashMap<String, Object>();
		Map<String, String> whereParams = new HashMap<String, String>();
		setParams.put("password", "test123");
		whereParams.put("name", "aaa01");
		params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				setParams, whereParams };
		try {
			client.execute("user.update", params);
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testCreateMdg() {
		int count = getPacsCount();
		String user = "ad";
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, Object> setParams = new HashMap<String, Object>();
		setParams.put("name", "aaa90");
		setParams.put("hive", "h99");
		setParams.put("customer", config.getProperty("accountprefix.customer") + "-aaa");
		setParams.put("basepac", BasePacType.PAC_SRV);
		setParams.put("curinetaddr", "176.9.242.76");
		setParams.put("free", "true");
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				setParams };
		try {
			Object execute = client.execute(MODULE + ".add", params);
			assertTrue(execute instanceof Map<?, ?>);
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
		assertEquals(count + 1, getPacsCount());
		Map<String, String> whereParams = new HashMap<String, String>();
		whereParams.put("name", "aaa90");
		params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				whereParams };
		try {
			Object execute = client.execute(MODULE + ".search", params);
			assertTrue(execute instanceof Object[]);
			Object[] resultArr = (Object[]) execute;
			assertEquals(1, resultArr.length);
			assertTrue(resultArr[0] instanceof Map<?, ?>);
			@SuppressWarnings("unchecked")
			Map<String, Object> pacHash = (Map<String, Object>) resultArr[0];
			assertEquals("aaa90", pacHash.get("name"));
			assertNotNull(pacHash.get("components"));
			assertTrue(pacHash.get("components") instanceof Map<?, ?>);
			@SuppressWarnings("unchecked")
			Map<String, String> components = (Map<String, String>) pacHash.get("components");
			assertEquals(3,  components.size());
			assertEquals("25", components.get("DISK"));
			assertEquals("1", components.get("CPU"));
			assertEquals("10", components.get("TRAFFIC"));
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
	}

	private int getPacsCount() {
		int count = 0;
		String user = "hsh00-aaa";
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, String> whereParams = new HashMap<String, String>();
		whereParams.put("customer", config.getProperty("accountprefix.customer") + "-aaa");
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				whereParams };
		try {
			Object execute = client.execute(MODULE + ".search", params);
			Object[] result = (Object[]) execute;
			count = result.length;
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
		return count;
	}

}
