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 = "aaa00"; 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(6, 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 testSearchAsPacAdmin() { String user = "aaa00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); whereParams.put("domain", "example01.org"); Object[] params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; try { Object execute = client.execute(MODULE + ".search", params); Object[] result = (Object[]) execute; assertEquals(3, 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 testUpdate() { int count = getTargetCount(); String user = "aaa00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map setParams = new HashMap(); Map whereParams = new HashMap(); setParams.put("target", new String[] { "aaa00-test2" }); whereParams.put("domain", "example01.org"); whereParams.put("localpart", "webmaster"); 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", new String[] { "aaa00-admin" }); whereParams.put("domain", "example01.org"); whereParams.put("localpart", "webmaster"); 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 = "aaa00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); whereParams.put("domain", "example01.org"); whereParams.put("admin", "aaa00-admin"); Object[] params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; try { Object execute = client.execute(MODULE + ".search", params); Object[] result = (Object[]) execute; assertEquals(3, result.length); for (Object o : result) { if (o instanceof Map) { Map row = (Map) o; assertEquals("aaa00-admin", row.get("admin")); } else { fail("map expected"); } } } catch (XmlRpcException e) { fail(e.getMessage()); } } @Test public void testCreateAsPacAdminFails() { int count = getObjectCount(); String user = "aaa00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map setParams = new HashMap(); setParams.put("localpart", "f6n"); setParams.put("domain", "example02.org"); setParams.put("target", new String[] { "aaa00" }); 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 = "aaa00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map setParams = new HashMap(); setParams.put("localpart", "f6n"); setParams.put("domain", "example01.org"); setParams.put("target", new String[] { "aaa00-admin" } ); 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", "example01.org"); params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; try { Object execute = client.execute(MODULE + ".search", params); assertTrue(execute instanceof Object[]); whereParams = new HashMap(); Object[] resArray = (Object[]) execute; assertEquals(1, resArray.length); Object res = resArray[0]; if (res instanceof Map) { Map map = (Map) res; Object idVal = map.get("id"); whereParams.put("id", idVal); } else { fail("unexpected type"); } } catch (XmlRpcException e) { fail(e.getMessage()); } try { 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 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 = "aaa00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); whereParams.put("target", "aaa00-test2"); 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; } }