hs.hsadmin/hsarback/test/de/hsadmin/remote/PacMigrationTest.java

287 lines
10 KiB
Java
Raw Normal View History

2013-04-29 20:01:09 +02:00
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;
2013-05-27 19:54:29 +02:00
import java.util.Calendar;
import java.util.Date;
2013-04-29 20:01:09 +02:00
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;
import de.hsadmin.core.util.Config;
2013-05-27 19:54:29 +02:00
import de.hsadmin.core.util.TextUtil;
import de.hsadmin.hostsharing.BasePacType;
2013-04-29 20:01:09 +02:00
public class PacMigrationTest {
private static final String MODULE = "pac";
private XmlRpcClient client;
private RemoteCASHelper cas;
private Config config;
@Before
public void setUp() throws Exception {
client = RemoteTestHelper.getClient();
cas = new RemoteCASHelper();
config = Config.getInstance();
}
@After
public void tearDown() throws Exception {
client = null;
cas = null;
}
@Test
2013-05-24 11:38:46 +02:00
public void testCreateDW() {
int count = getPacsCount();
String user = "ad";
try {
String grantingTicketURL = cas.getGrantingTicketURL(user);
Object execute = addPac(user, grantingTicketURL,
"aaa04", BasePacType.PAC_DW, "176.9.242.77",
640, 4, 1);
assertEquals(count + 1, getPacsCount());
validateAddedPac(execute, BasePacType.PAC_DW, 640, 4, 1);
setPacAdminPassword(user, grantingTicketURL, "aaa04", "test123");
} catch (XmlRpcException e) {
fail(e.getMessage());
}
}
@Test
public void testCreateSW() {
2013-04-29 20:01:09 +02:00
int count = getPacsCount();
String user = "ad";
try {
2013-05-24 11:38:46 +02:00
String grantingTicketURL = cas.getGrantingTicketURL(user);
Object execute = addPac(user, grantingTicketURL,
"aaa05", BasePacType.PAC_SW, "176.9.242.77",
1024, 8, -1);
2013-05-24 11:38:46 +02:00
assertEquals(count + 1, getPacsCount());
validateAddedPac(execute, BasePacType.PAC_SW, 1024, 8, -1);
setPacAdminPassword(user, grantingTicketURL, "aaa05", "test123");
} catch (XmlRpcException e) {
fail(e.getMessage());
}
}
@Test
public void testUpdateDW() {
String user = "ad";
try {
String grantingTicketURL = cas.getGrantingTicketURL(user);
Map<String, Object> setParams = new HashMap<String, Object>();
Map<String, String> components = new HashMap<String, String>();
setParams.put("basepac", BasePacType.PAC_WEB);
setParams.put("components", components);
// components.put("QUOTA", "2048"); // does not yet work
Map<String, String> whereParams = new HashMap<String, String>();
2013-05-01 22:23:13 +02:00
whereParams.put("name", "aaa04");
2013-05-24 11:38:46 +02:00
Object[] params = new Object[] { user,
cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()),
setParams, whereParams };
Object execute = client.execute(MODULE + ".update", params);
assertNotNull(execute);
2013-05-01 22:23:13 +02:00
params = new Object[] { user,
cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()),
whereParams };
execute = client.execute(MODULE + ".search", params);
assertNotNull(execute);
assertTrue(execute instanceof Object[]);
Object[] untypedResultArray = (Object[]) execute;
assertEquals(1, untypedResultArray.length);
assertTrue(untypedResultArray[0] instanceof Map<?,?>);
@SuppressWarnings("unchecked")
Map<String, ?> pacResultMap = (Map<String, ?>) untypedResultArray[0];
2013-05-24 11:38:46 +02:00
assertEquals(BasePacType.PAC_WEB, pacResultMap.get("basepac"));
2013-05-01 22:23:13 +02:00
Object compMapObj = pacResultMap.get("components");
assertTrue(compMapObj instanceof Map<?,?>);
@SuppressWarnings("unchecked")
Map<String,String> compMap = (Map<String, String>) compMapObj;
2013-05-24 11:38:46 +02:00
assertEquals("1024", compMap.get("QUOTA"));
// assertEquals("2048", compMap.get("QUOTA")); // does not yet work
assertEquals("5", compMap.get("TRAFFIC"));
2013-04-29 20:01:09 +02:00
} catch (XmlRpcException e) {
fail(e.getMessage());
}
}
@Test
2013-05-24 11:38:46 +02:00
public void testUpdateSW() {
2013-04-29 20:01:09 +02:00
String user = "ad";
try {
2013-05-24 11:38:46 +02:00
String grantingTicketURL = cas.getGrantingTicketURL(user);
Map<String, Object> setParams = new HashMap<String, Object>();
Map<String, String> components = new HashMap<String, String>();
setParams.put("basepac", BasePacType.PAC_WEB);
setParams.put("components", components);
// components.put("QUOTA", "2048"); // does not yet work
Map<String, String> whereParams = new HashMap<String, String>();
whereParams.put("name", "aaa05");
Object[] params = new Object[] { user,
cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()),
setParams, whereParams };
2013-04-29 20:01:09 +02:00
Object execute = client.execute(MODULE + ".update", params);
assertNotNull(execute);
params = new Object[] { user,
cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()),
whereParams };
execute = client.execute(MODULE + ".search", params);
assertNotNull(execute);
assertTrue(execute instanceof Object[]);
Object[] untypedResultArray = (Object[]) execute;
assertEquals(1, untypedResultArray.length);
assertTrue(untypedResultArray[0] instanceof Map<?,?>);
@SuppressWarnings("unchecked")
Map<String, ?> pacResultMap = (Map<String, ?>) untypedResultArray[0];
assertEquals(BasePacType.PAC_WEB, pacResultMap.get("basepac"));
2013-04-30 18:45:21 +02:00
Object compMapObj = pacResultMap.get("components");
assertTrue(compMapObj instanceof Map<?,?>);
2013-05-01 22:23:13 +02:00
@SuppressWarnings("unchecked")
2013-05-01 14:26:12 +02:00
Map<String,String> compMap = (Map<String, String>) compMapObj;
2013-05-02 17:52:16 +02:00
assertEquals("1024", compMap.get("QUOTA"));
2013-05-24 11:38:46 +02:00
// assertEquals("2048", compMap.get("QUOTA")); // does not yet work
assertEquals("10", compMap.get("TRAFFIC"));
2013-04-29 20:01:09 +02:00
} catch (XmlRpcException e) {
fail(e.getMessage());
}
}
2013-05-27 19:54:29 +02:00
@Test
public void testDeleteSW() {
String user = "ad";
try {
int count = getPacsCount();
String grantingTicketURL = cas.getGrantingTicketURL(user);
Map<String, Object> setParams = new HashMap<String, Object>();
setParams.put("created", TextUtil.format(daysBack(10)));
Map<String, String> whereParams = new HashMap<String, String>();
whereParams.put("name", "aaa05");
Object[] params = new Object[] { user,
cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()),
setParams, whereParams };
Object execute = client.execute(MODULE + ".update", params);
assertNotNull(execute);
setParams = new HashMap<String, Object>();
setParams.put("cancelled", TextUtil.format(daysBack(1)));
whereParams = new HashMap<String, String>();
whereParams.put("name", "aaa05");
params = new Object[] { user,
cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()),
setParams, whereParams };
execute = client.execute(MODULE + ".update", params);
assertNotNull(execute);
whereParams.put("name", "aaa05");
params = new Object[] { user,
cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()),
whereParams };
execute = client.execute(MODULE + ".delete", params);
assertEquals(count - 1, getPacsCount());
} catch (XmlRpcException e) {
fail(e.getMessage());
}
}
private Date daysBack(int days) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, -days);
return calendar.getTime();
}
2013-04-29 20:01:09 +02:00
private int getPacsCount() {
int count = 0;
String user = "hsh00-aaa";
String grantingTicketURL = cas.getGrantingTicketURL(user);
Map<String, String> whereParams = new HashMap<String, String>();
whereParams.put("customer", config.getProperty("accountprefix.customer") + "-aaa");
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;
}
2013-05-24 11:38:46 +02:00
private Object addPac(String user, String grantingTicketURL,
String pacName, String basepacName, String inetAddr,
int quota, int traffic, int daemons)
throws XmlRpcException {
Map<String, Object> setParams = new HashMap<String, Object>();
setParams.put("name", pacName);
setParams.put("hive", "h99");
setParams.put("customer", config.getProperty("accountprefix.customer") + "-aaa");
setParams.put("basepac", basepacName);
setParams.put("curinetaddr", inetAddr);
HashMap<String, String> components = new HashMap<String, String>();
components.put("QUOTA", Integer.toString(quota));
components.put("TRAFFIC", Integer.toString(traffic));
if (daemons >= 0) {
components.put("DAEMON", Integer.toString(daemons));
}
2013-05-24 11:38:46 +02:00
setParams.put("components", components);
Object[] params = new Object[] { user,
cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()),
setParams };
Object execute = client.execute(MODULE + ".add", params);
assertTrue(execute instanceof Map<?, ?>);
Map<String, Object> whereParams = new HashMap<String, Object>();
whereParams.put("name", pacName);
params = new Object[] { user,
cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()),
whereParams };
execute = client.execute(MODULE + ".search", params);
return execute;
}
private void validateAddedPac(Object execute, String basepacName, int quota, int traffic, int daemons) {
assertNotNull(execute);
assertTrue(execute instanceof Object[]);
Object[] untypedResultArray = (Object[]) execute;
assertEquals(1, untypedResultArray.length);
assertTrue(untypedResultArray[0] instanceof Map<?,?>);
@SuppressWarnings("unchecked")
Map<String, ?> pacResultMap = (Map<String, ?>) untypedResultArray[0];
assertEquals(basepacName, pacResultMap.get("basepac"));
Object compMapObj = pacResultMap.get("components");
assertTrue(compMapObj instanceof Map<?,?>);
@SuppressWarnings("unchecked")
Map<String,String> compMap = (Map<String, String>) compMapObj;
assertEquals(Integer.toString(quota), compMap.get("QUOTA"));
assertEquals(Integer.toString(traffic), compMap.get("TRAFFIC"));
if (daemons >= 0) {
assertEquals(Integer.toString(daemons), compMap.get("DAEMON"));
} else {
assertTrue(compMap.get("DAEMON") == null);
}
}
private void setPacAdminPassword(String user, String grantingTicketURL,
String pacName, String password)
throws XmlRpcException {
HashMap<String, Object> whereParams = new HashMap<String, Object>();
HashMap<String, Object> setParams = new HashMap<String, Object>();
whereParams.put("name", pacName);
setParams.put("password", password);
Object[] params = new Object[] { user,
cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()),
setParams, whereParams };
client.execute("user.update", params);
}
2013-04-29 20:01:09 +02:00
}