package de.hsadmin.remote; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.IOException; import java.net.UnknownHostException; 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 DomainTest { private static final String MODULE = "domain"; 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 test1SearchAllAsPacAdmin() { 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(3, result.length); for (Object o : result) { if (o instanceof Map) { Map row = (Map) o; assertTrue("aaa00".equals(row.get("user")) || "aaa00-admin".equals(row.get("user"))); } else { fail("map expected"); } } } catch (XmlRpcException e) { fail(e.getMessage()); } } @Test public void test2Update() { String user = "aaa00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map setParams = new HashMap(); Map whereParams = new HashMap(); setParams.put("user", "aaa00"); whereParams.put("name", "example01.org"); Object[] params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), setParams, whereParams }; try { Object execute = client.execute(MODULE + ".update", params); assertNotNull(execute); fail("exception expected"); } catch (XmlRpcException e) { } } @Test public void test3Create() throws UnknownHostException, IOException { int count = getDomsCount(); String user = "aaa00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map setParams = new HashMap(); setParams.put("name", "f8n.de"); setParams.put("user", "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, getDomsCount()); } @Test public void test4CreateForeignSubdomain() throws UnknownHostException, IOException { int count = getDomsCount(); String user = "aaa00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map setParams = new HashMap(); setParams.put("name", "f6n.de"); setParams.put("user", "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, getDomsCount()); count = getDomsCount(); user = "aaa01"; grantingTicketURL = cas.getGrantingTicketURL(user); setParams = new HashMap(); setParams.put("name", "subdomain.f6n.de"); setParams.put("user", "aaa01"); params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), setParams }; try { Object execute = client.execute(MODULE + ".add", params); assertTrue(execute instanceof Map); fail("should throw exception"); } catch (XmlRpcException e) { assertEquals(count, getDomsCount()); } } private int getDomsCount() { int count = 0; 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; count = result.length; } catch (XmlRpcException e) { fail(e.getMessage()); } return count; } }