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>
<classpath refid="enhance.classpath"/>
</openjpac>
<!--
<mappingtool action="buildSchema">
<classpath refid="enhance.classpath"/>
</mappingtool> -->
<echo message="Enhancing complete."/>
</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>

View File

@ -27,9 +27,6 @@
-->
<properties>
<property name="openjpa.ConnectionDriverName" value="org.postgresql.Driver"/>
<!--
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
<property name="openjpa.RuntimeUnenhancedClasses" value="supported"/> -->
</properties>
</persistence-unit>
</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';
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';
--
-- 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;
--
-- 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() {
if (loginUser == null) {
log.debug("getting login user");
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());
loginUser = transaction.getLoginUser();
}
return loginUser;
}

View File

@ -12,6 +12,7 @@ import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import org.apache.commons.logging.Log;
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.qserv.QueueClient;
import de.hsadmin.core.qserv.QueueTask;
import de.hsadmin.mods.user.UnixUser;
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.Transient;
import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.core.model.EntityInfo;
import de.hsadmin.core.model.FieldValidation;
import de.hsadmin.mods.pac.Pac;
@ -24,7 +25,7 @@ import de.hsadmin.mods.pac.Pac;
@Table(name = "unixuser")
@SequenceGenerator(name = "UnixUsersSeqGen", sequenceName = "unixuser_unixuser_id_seq")
@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;
@ -269,7 +270,8 @@ public class UnixUser extends de.hsadmin.core.model.AbstractEntity implements Se
*/
public boolean hasHostmasterRole() {
// 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 {
if (authentication.login(user, ticket)) {
ModuleInterface module = new GenericModuleImpl(transaction);
UnixUser unixUser = null;
unixUser = (UnixUser) module.findByString(UnixUser.class, user);
UnixUser unixUser = transaction.getLoginUser();
List<AbstractEntity> list = module.search(getEntityClass(),
buildQueryCondition(whereParams), null);
if (list == null) {
@ -103,8 +102,7 @@ public abstract class AbstractRemote implements IRemote {
try {
if (authentication.login(user, ticket)) {
ModuleInterface module = new GenericModuleImpl(transaction);
UnixUser unixUser = null;
unixUser = (UnixUser) module.findByString(UnixUser.class, user);
UnixUser unixUser = transaction.getLoginUser();
String queryCondition = buildQueryCondition(whereParams);
if (queryCondition == null || queryCondition.length() == 0) {
throw new HSAdminException(
@ -141,8 +139,7 @@ public abstract class AbstractRemote implements IRemote {
try {
if (authentication.login(user, ticket)) {
ModuleInterface module = new GenericModuleImpl(transaction);
UnixUser unixUser = null;
unixUser = (UnixUser) module.findByString(UnixUser.class, user);
UnixUser unixUser = transaction.getLoginUser();
ArrayList<Map<String, String>> result = new ArrayList<Map<String, String>>();
String queryCondition = buildQueryCondition(whereParams);
if (queryCondition == null || queryCondition.length() == 0) {
@ -190,6 +187,18 @@ public abstract class AbstractRemote implements IRemote {
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) {
regularizeKeys(whereParams);
StringBuffer cond = new StringBuffer();
@ -211,16 +220,4 @@ public abstract class AbstractRemote implements IRemote {
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);
String pac = dom.getUser().getPac().getName();
resultMap.put("pac", pac);
Date sDate = dom.getSince();
Date sDate = dom.getFiled();
if (assertNotNull(sDate)) {
String since = df.format(sDate);
resultMap.put("since", since);

View File

@ -1,8 +1,6 @@
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;

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;
}
}