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 = "peh00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); Object[] params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; try { Object execute = client.execute(MODULE + ".search", params); Object[] result = (Object[]) execute; assertEquals(262, result.length); for (Object o : result) { if (o instanceof Map) { Map row = (Map) o; assertEquals("peh00", row.get("pac")); } else { fail("map expected"); } } } catch (XmlRpcException e) { fail(e.getMessage()); } } @Test public void testSearchAsPacAdmin() { String user = "peh00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); 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("peh00", row.get("pac")); } else { fail("map expected"); } } } catch (XmlRpcException e) { fail(e.getMessage()); } } @Test public void testUpdate() { int count = getTargetCount(); String user = "peh00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map setParams = new HashMap(); Map whereParams = new HashMap(); setParams.put("target", "peh00-phor"); 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(); whereParams = new HashMap(); setParams.put("target", "peh00-hotline"); 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 whereParams = new HashMap(); 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 setParams = new HashMap(); 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 setParams = new HashMap(); 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 whereParams = new HashMap(); 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 + ".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 whereParams = new HashMap(); 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 whereParams = new HashMap(); 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; } }