package de.hsadmin.remote;

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

	private static final String CUST_MODULE = "member";
	private static final String PAC_MODULE = "pac";
	private static final String USER_MODULE = "user";
	
	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 testAddMember() {
		String user = "ad";
		int membersCount = -9999;
		membersCount = getMembersCount();
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, String> setParams = new HashMap<String, String>();
		setParams.put("membercode", "hsh00-aaa");
		setParams.put("password", "geheimnIs");
		setParams.put("memberno", "20001");
		setParams.put("membersince", "01.10.2010");
		setParams.put("contact_salut", "Herr");
		setParams.put("contact_title", "Dr.");
		setParams.put("contact_firstname", "Ömer Günther");
		setParams.put("contact_lastname", "Janßen-Müller");
		setParams.put("contact_salut", "Herr");
		setParams.put("contact_street", "Hauptstr. 1");
		setParams.put("contact_zipcode", "99998");
		setParams.put("contact_city", "Musterstadt");
		setParams.put("contact_country", "D");
		setParams.put("contact_phone_private", "+49 9999 123456");
		setParams.put("contact_email", "rainer.mustermann@example.org");
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				setParams };
		try {
			client.execute(CUST_MODULE + ".add", params);
//			Object execute = client.execute(CUST_MODULE + ".add", params);
//			Map<?, ?> result = (Map<?, ?>) execute;
//			System.out.println(result);
			assertEquals(membersCount + 1, getMembersCount());
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testAddPac() {
		int count = getPacsCount();
		String user = "ad";
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, String> setParams = new HashMap<String, String>();
		setParams.put("name", "aaa00");
		setParams.put("hive", "h81");
		setParams.put("customer", "hsh00-aaa");
		setParams.put("basepac", "DW/B");
		setParams.put("curinetaddr", "192.168.108.201");
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				setParams };
		try {
			Object execute = client.execute(PAC_MODULE + ".add", params);
			assertTrue(execute instanceof Map<?, ?>);
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
		assertEquals(count + 1, getPacsCount());
	}

	@Test
	public void testAddUser() {
		int count = getUsersCount();
		try {
			String user = "aaa00";
			String grantingTicketURL = cas.getGrantingTicketURL(user);
			Map<String, String> setParams = new HashMap<String, String>();
			setParams.put("name", "aaa00-test2");
			setParams.put("password", "test123");
			setParams.put("quota", "128");
			setParams.put("quotalimit", "192");
			Object[] params = new Object[] { user, 
					cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
					setParams };
			Object execute = client.execute(USER_MODULE + ".add", params);
			if (execute instanceof Map<?, ?>) {
				Map<?, ?> entry = (Map<?, ?>) execute;
				assertEquals("aaa00-test2", entry.get("name"));
				assertEquals("aaa00", entry.get("pac"));
				assertEquals(null, entry.get("password"));
			} else {
				fail("map expected");
			}
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
		assertEquals(count + 1, getUsersCount());
	}

//	@Test
	public void testDelPac() {
		int count = getPacsCount();
		String user = "ad";
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, String> whereParams = new HashMap<String, String>();
		whereParams.put("name", "aaa00");
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				whereParams };
		try {
			client.execute(PAC_MODULE + ".delete", params);
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
		assertEquals(count - 1, getPacsCount());
	}

//	@Test
	public void testDelMember() {
		int count = getMembersCount();
		String user = "ad";
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, String> whereParams = new HashMap<String, String>();
		whereParams.put("membercode", "hsh00-aaa");
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				whereParams };
		try {
			client.execute(CUST_MODULE + ".delete", params);
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
		assertEquals(count - 1, getMembersCount());
	}

	private int getMembersCount() {
		int count = 0;
		String user = "ad";
		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;
		try {
			execute = client.execute(CUST_MODULE + ".search", params);
			Object[] result = (Object[]) execute;
			count = result.length;
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
		return count;
	}

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

	private int getUsersCount() {
		int count = -1;
		try {
			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 };
			Object execute = client.execute(USER_MODULE + ".search", params);
			Object[] result = (Object[]) execute;
			count = result.length;
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
		return count;
	}

}
