package de.hsadmin.remote;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
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;

public class EMailAddressTest {

	private static final String MODULE = "emailaddress";
	
	private XmlRpcClient client;
	private RemoteCASHelper cas;

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

	@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(242, result.length);
			for (Object o : result) {
				if (o instanceof Map<?, ?>) {
					Map<?, ?> row = (Map<?, ?>) o;
					assertEquals("aaa00", row.get("pac"));
				} else {
					fail("map expected");
				}
			}
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testSearchAsPacAdmin() {
		String user = "aaa00";
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, String> whereParams = new HashMap<String, String>();
		whereParams.put("domain", "herzensklaenge.de");
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				whereParams };
		try {
			Object execute = client.execute(MODULE + ".search", params);
			Object[] result = (Object[]) execute;
			assertEquals(2, result.length);
			for (Object o : result) {
				if (o instanceof Map<?, ?>) {
					Map<?, ?> row = (Map<?, ?>) o;
					assertEquals("aaa00", row.get("pac"));
				} else {
					fail("map expected");
				}
			}
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testUpdate() {
		int count = getTargetCount();
		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("target", "aaa00-test2");
		whereParams.put("domain", "jalin.de");
		whereParams.put("localpart", "fax");
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				setParams, whereParams };
		try {
			Object execute = client.execute(MODULE + ".update", params);
			Object[] result = (Object[]) execute;
			assertEquals(1, result.length);
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
		assertEquals(count + 1, getTargetCount());
		setParams = new HashMap<String, String>();
		whereParams = new HashMap<String, String>();
		setParams.put("target", "aaa00-test2");
		whereParams.put("domain", "jalin.de");
		whereParams.put("localpart", "fax");
		params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				setParams, whereParams };
		try {
			Object execute = client.execute(MODULE + ".update", params);
			Object[] result = (Object[]) execute;
			assertEquals(1, result.length);
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
		assertEquals(count, getTargetCount());
	}

	@Test
	public void testSearchAsDomAdmin() {
		String user = "peh00";
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, String> whereParams = new HashMap<String, String>();
		whereParams.put("domain", "herzensklaenge.de");
		whereParams.put("admin", "peh00");
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				whereParams };
		try {
			Object execute = client.execute(MODULE + ".search", params);
			Object[] result = (Object[]) execute;
			assertEquals(2, result.length);
			for (Object o : result) {
				if (o instanceof Map<?, ?>) {
					Map<?, ?> row = (Map<?, ?>) o;
					assertEquals("peh00", row.get("admin"));
				} else {
					fail("map expected");
				}
			}
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testCreateAsPacAdminFails() {
		int count = getObjectCount();
		String user = "peh00";
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, String> setParams = new HashMap<String, String>();
		setParams.put("localpart", "f6n");
		setParams.put("domain", "f6n.de");
		setParams.put("target", "peh00-phor");
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				setParams };
		try {
			Object execute = client.execute(MODULE + ".add", params);
			assertTrue(execute instanceof Map<?, ?>);
			fail("exception expected");
		} catch (XmlRpcException e) {
		}
		assertEquals(count, getObjectCount());
	}

	@Test
	public void testCreateAndDelete() {
		int count = getObjectCount();
		String user = "peh00";
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, String> setParams = new HashMap<String, String>();
		setParams.put("localpart", "f6n");
		setParams.put("domain", "jalin.de");
		setParams.put("target", "peh00-phor");
		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, getObjectCount());
		count = getObjectCount();
		Map<String, Object> whereParams = new HashMap<String, Object>();
		whereParams.put("localpart", "f6n");
		whereParams.put("domain", "jalin.de");
		params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				whereParams };
		try {
			Object execute = client.execute(MODULE + ".search", params);
			assertTrue(execute instanceof Object[]);
			whereParams = new HashMap<String, Object>();
			Object[] resArray = (Object[]) execute;
			assertEquals(1, resArray.length);
			Object res = resArray[0];
			if (res instanceof Map<?, ?>) {
				Map<?, ?> map = (Map<?, ?>) res;
				Object idVal = map.get("id");
				whereParams.put("id", idVal);
			} else {
				fail("unexpected type");
			}
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
		try {
			params = new Object[] { user, 
					cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
					whereParams };
			Object execute = client.execute(MODULE + ".delete", params);
			assertNull(execute);
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
		assertEquals(count - 1, getObjectCount());
	}

	private int getObjectCount() {
		int count = -1;
		try {
			String user = "peh00";
			String grantingTicketURL = cas.getGrantingTicketURL(user);
			Map<String, String> whereParams = new HashMap<String, String>();
			Object[] params = new Object[] { user, 
					cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
					whereParams };
			Object execute = client.execute(MODULE + ".search", params);
			Object[] result = (Object[]) execute;
			count = result.length;
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
		return count;
	}

	private int getTargetCount() {
		int count = -1;
		try {
			String user = "peh00";
			String grantingTicketURL = cas.getGrantingTicketURL(user);
			Map<String, String> whereParams = new HashMap<String, String>();
			whereParams.put("target", "peh00-phor");
			Object[] params = new Object[] { user, 
					cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
					whereParams };
			Object execute = client.execute(MODULE + ".search", params);
			Object[] result = (Object[]) execute;
			count = result.length;
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
		return count;
	}

}
