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 PgsqlDbTest { private static final String MODULE = "postgresqldb"; 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 testCreate() { int count = getDBsCount(); String user = "aaa00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map setParams = new HashMap(); setParams.put("name", "aaa00_db1"); setParams.put("owner", "aaa00_dba"); setParams.put("encoding", "UTF-8"); Object[] params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), setParams }; try { Object execute = client.execute(MODULE + ".add", params); assertTrue(execute instanceof Map); setParams.put("name", "aaa00_db2"); params[1] = cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()); execute = client.execute(MODULE + ".add", params); assertTrue(execute instanceof Map); } catch (XmlRpcException e) { fail(e.getMessage()); } assertEquals(count + 2, getDBsCount()); } @Test public void testDelete() { int count = getDBsCount(); String user = "aaa00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); whereParams.put("name", "aaa00_db1"); Object[] 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, getDBsCount()); } @Test public void testSearch() { 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(1, result.length); } catch (XmlRpcException e) { fail(e.getMessage()); } } private int getDBsCount() { 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; } }