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 CustomerTest {

	private static final String MODULE = "member";
	
	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 testAddAsHostmaster() {
		String user = "ad";
		int membersCount = -9999;
		try {
			membersCount = getMembersCount(user);
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, String> setParams = new HashMap<String, String>();
		setParams.put("membercode", "hsh00-abc");
		setParams.put("password", "geheimnIs");
		setParams.put("memberno", "20002");
		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_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(MODULE + ".add", params);
			assertEquals(membersCount + 1, getMembersCount(user));
			for (int idx=11; idx < 100; idx++) {
				setParams.put("membercode", "hsh00-n" + idx);
				setParams.put("memberno", "200" + idx);
				setParams.put("contact_street", "Hauptstr. " + idx);	
				params[1] = cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL());
				client.execute(MODULE + ".add", params);
			}
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testSearchAsHostmaster() {
		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 };
		try {
			Object execute = client.execute(MODULE + ".search", params);
			Object[] result = (Object[]) execute;
			assertTrue(result.length > 90);
		} 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>();
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				whereParams };
		try {
			Object execute = client.execute(MODULE + ".search", params);
			Object[] result = (Object[]) execute;
			assertTrue(result.length == 0);
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testSearchAsPacUser() {
		String user = "aaa00-test2";
		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;
			assertTrue(result.length == 0);
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testSearchAsMember() {
		String user = "hsh00-aaa";
		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;
			assertTrue(result.length == 1);
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testUpdateAsMember() {
		String user = "hsh00-aaa";
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, String> whereParams = new HashMap<String, String>();
		Map<String, String> setParams = new HashMap<String, String>();
		whereParams.put("membercode", "hsh00-aaa");
		setParams.put("contact_firstname", "Hugo");
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				setParams, whereParams };
		try {
			client.execute(MODULE + ".update", params);
			fail("exception erwartet");
		} catch (XmlRpcException e) {
			// Exception erwartet
		}
	}

	@Test
	public void testUpdateAsHostmaster() {
		String user = "ad";
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, String> whereParams = new HashMap<String, String>();
		Map<String, String> setParams = new HashMap<String, String>();
		whereParams.put("membercode", "hsh00-aaa");
		setParams.put("contact_title", "Prof.");
		setParams.put("membersince", "Hugo");
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				setParams, whereParams };
		try {
			client.execute(MODULE + ".update", params);
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testDeleteAsMember() {
		String user = "hsh00-aaa";
		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(MODULE + ".delete", params);
			fail("exception erwartet");
		} catch (XmlRpcException e) {
			// Exception erwartet
		}
	}

	@Test
	public void testDeleteAsHostmaster() {
		String user = "ad";
		int membersCount = -9999;
		try {
			membersCount = getMembersCount(user);
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
		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(MODULE + ".delete", params);
			assertEquals(membersCount - 1, getMembersCount(user));
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
	}

	private int getMembersCount(String user) throws XmlRpcException {
		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;
		return result.length;
	}
}
