package de.hsadmin.remote;

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

	private static final String 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 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;
			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 testAddAsPacAdmin() {
		int count = getObjectCount();
		try {
			String user = "aaa00";
			String grantingTicketURL = cas.getGrantingTicketURL(user);
			Map<String, String> setParams = new HashMap<String, String>();
			setParams.put("name", "aaa00-test3");
			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(MODULE + ".add", params);
			if (execute instanceof Map<?, ?>) {
				Map<?, ?> entry = (Map<?, ?>) execute;
				assertEquals("aaa00-test3", 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, getObjectCount());
	}

	@Test
	public void testAddAsPacAdminFail() {
		int count = getObjectCount();
		try {
			String user = "aaa00";
			String grantingTicketURL = cas.getGrantingTicketURL(user);
			Map<String, String> setParams = new HashMap<String, String>();
			setParams.put("name", "aaa01-testfail");
			setParams.put("password", "test123");
			Object[] params = new Object[] { user, 
					cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
					setParams };
			Object execute = client.execute(MODULE + ".add", params);
			assertNull(execute);
			fail("should fail");
		} catch (XmlRpcException e) {
			
		}
		assertEquals(count, getObjectCount());
	}

	@Test
	public void testAddWithFalseName() {
		int count = getObjectCount();
		try {
			String user = "aaa00";
			String grantingTicketURL = cas.getGrantingTicketURL(user);
			Map<String, String> setParams = new HashMap<String, String>();
			setParams.put("name", "aaa00-langer-name");
			setParams.put("password", "test123");
			Object[] params = new Object[] { user, 
					cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
					setParams };
			Object execute = client.execute(MODULE + ".add", params);
			assertNull(execute);
			fail("exception expected");
		} catch (XmlRpcException e) {
			
		}
		assertEquals(count, getObjectCount());
	}

	@Test
	public void testUpdateAsPacAdmin() {
		int count = getObjectCount();
		try {
			String user = "aaa00";
			String grantingTicketURL = cas.getGrantingTicketURL(user);
			Map<String, String> whereParams = new HashMap<String, String>();
			Map<String, String> setParams = new HashMap<String, String>();
			whereParams.put("name", "aaa00-test3");
			setParams.put("password", "test1234");
			Object[] params = new Object[] { user, 
					cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
					setParams, whereParams };
			Object execute = client.execute(MODULE + ".update", params);
			if (execute instanceof Object[]) {
				Object[] a = (Object[]) execute;
				for (Object o : a) {
					if (o instanceof Map<?, ?>) {
						Map<?, ?> entry = (Map<?, ?>) o;
						assertNull(entry.get("password"));
						assertEquals("aaa00-test3", entry.get("name"));
					} else {
						fail("map expected");
					}
				}
			} else {
				fail("Object[] axpected");
			}
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
		assertEquals(count, getObjectCount());
	}

	@Test
	public void testDeleteWithLongName() {
		int count = getObjectCount();
		try {
			String user = "aaa00";
			String grantingTicketURL = cas.getGrantingTicketURL(user);
			Map<String, String> whereParams = new HashMap<String, String>();
			whereParams.put("name", "aaa00-langer-name");
			Object[] 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, getObjectCount());
	}

//	@Test
//	public void testDeleteAsPacAdminFail() {
//		int count = getObjectCount();
//		try {
//			String user = "aaa01";
//			String grantingTicketURL = cas.getGrantingTicketURL(user);
//			Map<String, String> whereParams = new HashMap<String, String>();
//			whereParams.put("name", "aaa00-test2");
//			Object[] 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, getObjectCount());
//	}

	@Test
	public void testDeleteAsPacAdmin() {
		int count = getObjectCount();
		try {
			String user = "aaa00";
			String grantingTicketURL = cas.getGrantingTicketURL(user);
			Map<String, String> whereParams = new HashMap<String, String>();
			whereParams.put("name", "aaa00-test3");
			Object[] 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 = "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(MODULE + ".search", params);
			Object[] result = (Object[]) execute;
			count = result.length;
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
		return count;
	}

}
