leere Datenbank fuer Tests initialisieren

This commit is contained in:
Peter Hormanns 2011-03-25 16:47:54 +00:00
parent 991317dc89
commit f94bfc9e85
11 changed files with 256 additions and 42 deletions

View File

@ -67,11 +67,22 @@
<openjpac> <openjpac>
<classpath refid="enhance.classpath"/> <classpath refid="enhance.classpath"/>
</openjpac> </openjpac>
<!--
<mappingtool action="buildSchema">
<classpath refid="enhance.classpath"/>
</mappingtool> -->
<echo message="Enhancing complete."/> <echo message="Enhancing complete."/>
</target> </target>
<target name="init-db" description="init empty database">
<sql
classpath="/usr/share/java/postgresql-jdbc3-8.2.jar"
driver="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/hsh02_hsdb1"
userid="hsh02_hsdb" password="Phoh9t"
src="database/schema.sql" />
<sql
classpath="/usr/share/java/postgresql-jdbc3-8.2.jar"
driver="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/hsh02_hsdb1"
userid="hsh02_hsdb" password="Phoh9t"
src="database/data.sql" />
</target>
</project> </project>

View File

@ -27,9 +27,6 @@
--> -->
<properties> <properties>
<property name="openjpa.ConnectionDriverName" value="org.postgresql.Driver"/> <property name="openjpa.ConnectionDriverName" value="org.postgresql.Driver"/>
<!--
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
<property name="openjpa.RuntimeUnenhancedClasses" value="supported"/> -->
</properties> </properties>
</persistence-unit> </persistence-unit>
</persistence> </persistence>

View File

@ -43,3 +43,81 @@ INSERT INTO component (basepacket_id, basecomponent_id, min_quantity, max_quanti
SELECT basepacket_id, basecomponent_id, 0, 4, 0, 1, 0, false FROM basepacket, basecomponent WHERE basepacket_code='DW/B' AND basecomponent_code='APACHE'; SELECT basepacket_id, basecomponent_id, 0, 4, 0, 1, 0, false FROM basepacket, basecomponent WHERE basepacket_code='DW/B' AND basecomponent_code='APACHE';
INSERT INTO component (basepacket_id, basecomponent_id, min_quantity, max_quantity, default_quantity, increment_quantity, include_quantity, admin_only) INSERT INTO component (basepacket_id, basecomponent_id, min_quantity, max_quantity, default_quantity, increment_quantity, include_quantity, admin_only)
SELECT basepacket_id, basecomponent_id, 0, 0, 0, 1, 0, false FROM basepacket, basecomponent WHERE basepacket_code='SW/B' AND basecomponent_code='APACHE'; SELECT basepacket_id, basecomponent_id, 0, 0, 0, 1, 0, false FROM basepacket, basecomponent WHERE basepacket_code='SW/B' AND basecomponent_code='APACHE';
--
-- Table: business_partner
--
INSERT INTO business_partner (member_id, member_code, member_since, shares_signed)
VALUES (10000, 'hsh00-hsh', current_date, 0);
--
-- table: contact
--
INSERT INTO contact (bp_id, salut, first_name, last_name, firma, email)
SELECT bp_id, 'Herr', 'Uwe', 'Mueller', 'Hostsharing eG', 'service@hostsharing.net' FROM business_partner WHERE member_id=10000;
--
-- table: bank_account
--
INSERT INTO bank_account (bp_id)
SELECT bp_id FROM business_partner WHERE member_id=10000;
--
-- table: billdata
--
INSERT INTO billdata (bp_id)
SELECT bp_id FROM business_partner WHERE member_id=10000;
--
-- Table: inet_addr
--
INSERT INTO inet_addr (inet_addr, description)
VALUES (inet '192.168.108.200', 'address 200');
INSERT INTO inet_addr (inet_addr, description)
VALUES (inet '192.168.108.201', 'address 201');
INSERT INTO inet_addr (inet_addr, description)
VALUES (inet '192.168.108.202', 'address 202');
INSERT INTO inet_addr (inet_addr, description)
VALUES (inet '192.168.108.203', 'address 203');
INSERT INTO inet_addr (inet_addr, description)
VALUES (inet '192.168.108.204', 'address 204');
INSERT INTO inet_addr (inet_addr, description)
VALUES (inet '192.168.108.205', 'address 205');
INSERT INTO inet_addr (inet_addr, description)
VALUES (inet '192.168.108.206', 'address 206');
INSERT INTO inet_addr (inet_addr, description)
VALUES (inet '192.168.108.207', 'address 207');
INSERT INTO inet_addr (inet_addr, description)
VALUES (inet '192.168.108.208', 'address 208');
INSERT INTO inet_addr (inet_addr, description)
VALUES (inet '192.168.108.209', 'address 209');
INSERT INTO inet_addr (inet_addr, description)
VALUES (inet '192.168.108.210', 'address 210');
--
-- Table: Hive
--
INSERT INTO hive (hive_name, inet_addr_id, description)
SELECT 'h81', inet_addr_id, 'Test Hive h081' FROM inet_addr WHERE inet_addr = inet '192.168.108.200';
--
-- table: packet
--
INSERT INTO packet (packet_name, bp_id, hive_id, created, cur_inet_addr_id)
SELECT 'hsh00', business_partner.bp_id, hive.hive_id, current_date, inet_addr.inet_addr_id FROM business_partner, hive, inet_addr
WHERE hive_name='h81' AND inet_addr = inet '192.168.108.201' AND member_id = 10000;
INSERT INTO packet (packet_name, bp_id, hive_id, created, cur_inet_addr_id)
SELECT 'hsh01', business_partner.bp_id, hive.hive_id, current_date, inet_addr.inet_addr_id FROM business_partner, hive, inet_addr
WHERE hive_name='h81' AND inet_addr = inet '192.168.108.202' AND member_id = 10000;
--
-- table: unixuser
--
INSERT INTO unixuser (name, comment, shell, homedir, locked, packet_id, userid)
SELECT 'hsh00', 'packet hsh00', '/bin/bash', '/home/pacs/hsh00', FALSE, packet_id, 10001 FROM packet
WHERE packet_name='hsh00';
INSERT INTO unixuser (name, comment, shell, homedir, locked, packet_id, userid)
SELECT 'hsh01', 'packet hsh01', '/bin/bash', '/home/pacs/hsh01', FALSE, packet_id, 10002 FROM packet
WHERE packet_name='hsh01';
INSERT INTO unixuser (name, comment, shell, homedir, locked, packet_id, userid)
SELECT 'hsh01-pe', 'hostmaster pe', '/bin/bash', '/home/pacs/hsh01/users/pe', FALSE, packet_id, 10003 FROM packet
WHERE packet_name='hsh01';

View File

@ -930,10 +930,3 @@ ALTER TABLE ONLY queue_task
ADD CONSTRAINT queue_task_user_id_fkey FOREIGN KEY (user_id) REFERENCES unixuser(unixuser_id) ON DELETE SET NULL; ADD CONSTRAINT queue_task_user_id_fkey FOREIGN KEY (user_id) REFERENCES unixuser(unixuser_id) ON DELETE SET NULL;
--
-- Name: queue_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY queue
ADD CONSTRAINT queue_user_id_fkey FOREIGN KEY (user_id) REFERENCES unixuser(unixuser_id) DEFERRABLE;

View File

@ -301,11 +301,7 @@ public abstract class AbstractModuleImpl implements ModuleInterface {
*/ */
public UnixUser getLoginUser() { public UnixUser getLoginUser() {
if (loginUser == null) { if (loginUser == null) {
log.debug("getting login user"); loginUser = transaction.getLoginUser();
Query userQuery = transaction.getEntityManager().createQuery("SELECT u FROM UnixUsers u WHERE u.name=:name");
userQuery.setParameter("name", transaction.getLoginName());
loginUser = (UnixUser) userQuery.getSingleResult();
log.debug("found login user: " + loginUser.getName());
} }
return loginUser; return loginUser;
} }

View File

@ -12,6 +12,7 @@ import javax.naming.InitialContext;
import javax.naming.NamingException; import javax.naming.NamingException;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction; import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -22,6 +23,7 @@ import de.hsadmin.cliClientConnector.TechnicalException;
import de.hsadmin.core.model.onetier.PersistenceManager; import de.hsadmin.core.model.onetier.PersistenceManager;
import de.hsadmin.core.qserv.QueueClient; import de.hsadmin.core.qserv.QueueClient;
import de.hsadmin.core.qserv.QueueTask; import de.hsadmin.core.qserv.QueueTask;
import de.hsadmin.mods.user.UnixUser;
public class Transaction { public class Transaction {
@ -176,4 +178,15 @@ public class Transaction {
} }
} }
public UnixUser getLoginUser() {
String loginName = getLoginName();
if (loginName != null && loginName.length() == 2) {
loginName = "hsh01-" + loginName;
}
Query userQuery = getEntityManager().createQuery("SELECT u FROM UnixUsers u WHERE u.name = :username");
userQuery.setParameter("username", loginName);
UnixUser unixUser = (UnixUser) userQuery.getSingleResult();
return unixUser;
}
} }

View File

@ -16,6 +16,7 @@ import javax.persistence.SequenceGenerator;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Transient; import javax.persistence.Transient;
import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.core.model.EntityInfo; import de.hsadmin.core.model.EntityInfo;
import de.hsadmin.core.model.FieldValidation; import de.hsadmin.core.model.FieldValidation;
import de.hsadmin.mods.pac.Pac; import de.hsadmin.mods.pac.Pac;
@ -24,7 +25,7 @@ import de.hsadmin.mods.pac.Pac;
@Table(name = "unixuser") @Table(name = "unixuser")
@SequenceGenerator(name = "UnixUsersSeqGen", sequenceName = "unixuser_unixuser_id_seq") @SequenceGenerator(name = "UnixUsersSeqGen", sequenceName = "unixuser_unixuser_id_seq")
@EntityInfo(name = "UNIX-Zugang/Mailbox") @EntityInfo(name = "UNIX-Zugang/Mailbox")
public class UnixUser extends de.hsadmin.core.model.AbstractEntity implements Serializable { public class UnixUser extends AbstractEntity implements Serializable {
private static final long serialVersionUID = 7823071611805642906L; private static final long serialVersionUID = 7823071611805642906L;
@ -269,7 +270,8 @@ public class UnixUser extends de.hsadmin.core.model.AbstractEntity implements Se
*/ */
public boolean hasHostmasterRole() { public boolean hasHostmasterRole() {
// TODO: hardcoded Hostsharing conventions // TODO: hardcoded Hostsharing conventions
return getName().length() == 2; String login = getName();
return login.length() == 2 || ((login.startsWith("hsh01-") && login.length() == 8));
} }
/** /**

View File

@ -41,8 +41,7 @@ public abstract class AbstractRemote implements IRemote {
try { try {
if (authentication.login(user, ticket)) { if (authentication.login(user, ticket)) {
ModuleInterface module = new GenericModuleImpl(transaction); ModuleInterface module = new GenericModuleImpl(transaction);
UnixUser unixUser = null; UnixUser unixUser = transaction.getLoginUser();
unixUser = (UnixUser) module.findByString(UnixUser.class, user);
List<AbstractEntity> list = module.search(getEntityClass(), List<AbstractEntity> list = module.search(getEntityClass(),
buildQueryCondition(whereParams), null); buildQueryCondition(whereParams), null);
if (list == null) { if (list == null) {
@ -103,8 +102,7 @@ public abstract class AbstractRemote implements IRemote {
try { try {
if (authentication.login(user, ticket)) { if (authentication.login(user, ticket)) {
ModuleInterface module = new GenericModuleImpl(transaction); ModuleInterface module = new GenericModuleImpl(transaction);
UnixUser unixUser = null; UnixUser unixUser = transaction.getLoginUser();
unixUser = (UnixUser) module.findByString(UnixUser.class, user);
String queryCondition = buildQueryCondition(whereParams); String queryCondition = buildQueryCondition(whereParams);
if (queryCondition == null || queryCondition.length() == 0) { if (queryCondition == null || queryCondition.length() == 0) {
throw new HSAdminException( throw new HSAdminException(
@ -141,8 +139,7 @@ public abstract class AbstractRemote implements IRemote {
try { try {
if (authentication.login(user, ticket)) { if (authentication.login(user, ticket)) {
ModuleInterface module = new GenericModuleImpl(transaction); ModuleInterface module = new GenericModuleImpl(transaction);
UnixUser unixUser = null; UnixUser unixUser = transaction.getLoginUser();
unixUser = (UnixUser) module.findByString(UnixUser.class, user);
ArrayList<Map<String, String>> result = new ArrayList<Map<String, String>>(); ArrayList<Map<String, String>> result = new ArrayList<Map<String, String>>();
String queryCondition = buildQueryCondition(whereParams); String queryCondition = buildQueryCondition(whereParams);
if (queryCondition == null || queryCondition.length() == 0) { if (queryCondition == null || queryCondition.length() == 0) {
@ -190,6 +187,18 @@ public abstract class AbstractRemote implements IRemote {
return integ != null; return integ != null;
} }
protected void replaceKey(Map<String, String> whereParams, String shortKey, String regularKey) {
if (whereParams.containsKey(shortKey)) {
String value = whereParams.get(shortKey);
whereParams.remove(shortKey);
whereParams.put(regularKey, value);
}
}
protected boolean assertNotNull(Date aDate) {
return aDate != null;
}
private String buildQueryCondition(Map<String, String> whereParams) { private String buildQueryCondition(Map<String, String> whereParams) {
regularizeKeys(whereParams); regularizeKeys(whereParams);
StringBuffer cond = new StringBuffer(); StringBuffer cond = new StringBuffer();
@ -211,16 +220,4 @@ public abstract class AbstractRemote implements IRemote {
return cond.toString(); return cond.toString();
} }
protected void replaceKey(Map<String, String> whereParams, String shortKey, String regularKey) {
if (whereParams.containsKey(shortKey)) {
String value = whereParams.get(shortKey);
whereParams.remove(shortKey);
whereParams.put(regularKey, value);
}
}
protected boolean assertNotNull(Date aDate) {
return aDate != null;
}
} }

View File

@ -31,7 +31,7 @@ public class DomainRemote extends AbstractRemote {
resultMap.put("hive", hive); resultMap.put("hive", hive);
String pac = dom.getUser().getPac().getName(); String pac = dom.getUser().getPac().getName();
resultMap.put("pac", pac); resultMap.put("pac", pac);
Date sDate = dom.getSince(); Date sDate = dom.getFiled();
if (assertNotNull(sDate)) { if (assertNotNull(sDate)) {
String since = df.format(sDate); String since = df.format(sDate);
resultMap.put("since", since); resultMap.put("since", since);

View File

@ -1,8 +1,6 @@
package de.hsadmin.remote; package de.hsadmin.remote;
import static org.junit.Assert.assertEquals; 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 static org.junit.Assert.fail;
import java.util.HashMap; import java.util.HashMap;

View File

@ -0,0 +1,129 @@
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 InitDataTest {
private static final String CUST_MODULE = "member";
private static final String PAC_MODULE = "pac";
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 testAddMember() {
String user = "pe";
int membersCount = -9999;
try {
membersCount = getMembersCount(user);
} catch (XmlRpcException e) {
fail(e.getMessage());
}
String grantingTicketURL = cas.getGrantingTicketURL(user);
Map<String, String> setParams = new HashMap<String, String>();
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(CUST_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 testAddPac() {
int count = getPacsCount();
String user = "pe";
String grantingTicketURL = cas.getGrantingTicketURL(user);
Map<String, String> setParams = new HashMap<String, String>();
setParams.put("name", "aaa00");
setParams.put("hive", "h81");
setParams.put("customer", "hsh00-aaa");
setParams.put("basepac", "DW/B");
setParams.put("curinetaddr", "192.168.108.203");
Object[] params = new Object[] { user,
cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()),
setParams };
try {
Object execute = client.execute(PAC_MODULE + ".add", params);
assertTrue(execute instanceof Map<?, ?>);
} catch (XmlRpcException e) {
fail(e.getMessage());
}
assertEquals(count + 1, getPacsCount());
}
private int getMembersCount(String user) throws XmlRpcException {
String grantingTicketURL = cas.getGrantingTicketURL(user);
Map<String, String> whereParams = new HashMap<String, String>();
Object[] params = new Object[] { user,
cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()),
whereParams };
Object execute = client.execute(CUST_MODULE + ".search", params);
Object[] result = (Object[]) execute;
return result.length;
}
private int getPacsCount() {
int count = 0;
String user = "pe";
String grantingTicketURL = cas.getGrantingTicketURL(user);
Map<String, String> whereParams = new HashMap<String, String>();
whereParams.put("customer", "hsh00-aaa");
Object[] params = new Object[] { user,
cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()),
whereParams };
try {
Object execute = client.execute(PAC_MODULE + ".search", params);
Object[] result = (Object[]) execute;
count = result.length;
} catch (XmlRpcException e) {
fail(e.getMessage());
}
return count;
}
}