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

	private static final String MODULE = "role";
	
	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 testPacAdmin() {
		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 == 1);
			String role = (String) ((Map<?, ?>) result[0]).get("role");
			assertEquals("PAC_ADMIN", role);
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testDomAdmin() {
		String user = "aaa00-admin";
		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);
			String role = (String) ((Map<?, ?>) result[0]).get("role");
			assertEquals("DOM_ADMIN", role);
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testCustomer() {
		String user = "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);
			String role = (String) ((Map<?, ?>) result[0]).get("role");
			assertEquals("CUSTOMER", role);
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testHostmaster() {
		String user = "pe";
		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);
			String role = (String) ((Map<?, ?>) result[0]).get("role");
			assertEquals("HOSTMASTER", role);
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
	}

}
