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 CustomerTest { private static final String MODULE = "member"; 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 testAddAsHostmaster() { String user = "pe"; int membersCount = -9999; try { membersCount = getMembersCount(user); } catch (XmlRpcException e) { fail(e.getMessage()); } String grantingTicketURL = cas.getGrantingTicketURL(user); Map setParams = new HashMap(); setParams.put("membercode", "hsh00-aaa"); setParams.put("password", "geheimnIs"); setParams.put("memberno", "20001"); setParams.put("membersince", "01.10.2010"); setParams.put("contact_salut", "Herr"); setParams.put("contact_title", "Dr."); setParams.put("contact_firstname", "Ömer Günther"); setParams.put("contact_lastname", "Janßen-Müller"); setParams.put("contact_salut", "Herr"); setParams.put("contact_street", "Hauptstr. 1"); setParams.put("contact_zipcode", "99998"); setParams.put("contact_city", "Musterstadt"); setParams.put("contact_country", "D"); setParams.put("contact_phone_private", "+49 9999 123456"); setParams.put("contact_email", "rainer.mustermann@example.org"); Object[] params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), setParams }; try { client.execute(MODULE + ".add", params); // Object execute = client.execute(MODULE + ".add", params); // Map result = (Map) execute; // System.out.println(result); assertEquals(membersCount + 1, getMembersCount(user)); } catch (XmlRpcException e) { fail(e.getMessage()); } } @Test public void testSearchAsHostmaster() { String user = "pe"; 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; assertTrue(result.length > 100); } catch (XmlRpcException e) { fail(e.getMessage()); } } @Test public void testSearchAsPacAdmin() { 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; assertTrue(result.length == 0); } catch (XmlRpcException e) { fail(e.getMessage()); } } @Test public void testSearchAsPacUser() { String user = "peh00-phor"; 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; assertTrue(result.length == 0); } catch (XmlRpcException e) { fail(e.getMessage()); } } @Test public void testSearchAsMember() { String user = "hsh00-peh"; 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; assertTrue(result.length == 1); } catch (XmlRpcException e) { fail(e.getMessage()); } } @Test public void testUpdateAsMember() { String user = "hsh00-aaa"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); Map setParams = new HashMap(); whereParams.put("membercode", "hsh00-aaa"); setParams.put("contact_firstname", "Hugo"); Object[] params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), setParams, whereParams }; try { client.execute(MODULE + ".update", params); fail("exception erwartet"); } catch (XmlRpcException e) { // Exception erwartet } } @Test public void testUpdateAsHostmaster() { String user = "pe"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); Map setParams = new HashMap(); whereParams.put("membercode", "hsh00-aaa"); setParams.put("contact_title", "Prof."); setParams.put("membersince", "Hugo"); Object[] params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), setParams, whereParams }; try { client.execute(MODULE + ".update", params); } catch (XmlRpcException e) { fail(e.getMessage()); } } @Test public void testDeleteAsMember() { String user = "hsh00-aaa"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); whereParams.put("membercode", "hsh00-aaa"); Object[] params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; try { client.execute(MODULE + ".delete", params); fail("exception erwartet"); } catch (XmlRpcException e) { // Exception erwartet } } @Test public void testDeleteAsHostmaster() { String user = "pe"; int membersCount = -9999; try { membersCount = getMembersCount(user); } catch (XmlRpcException e) { fail(e.getMessage()); } String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); whereParams.put("membercode", "hsh00-aaa"); Object[] params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; try { client.execute(MODULE + ".delete", params); assertEquals(membersCount - 1, getMembersCount(user)); } catch (XmlRpcException e) { fail(e.getMessage()); } } private int getMembersCount(String user) throws XmlRpcException { 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; return result.length; } }