From 5028419aa3c51b06418b7c80b9859e811959afc4 Mon Sep 17 00:00:00 2001
From: Peter Hormanns <peter.hormanns@jalin.de>
Date: Mon, 06 May 2013 19:59:23 +0200
Subject: [PATCH] new tests to show errors in pac module

---
 hsarback/test/de/hsadmin/remote/PacTest.java        |   63 +++++++++++++++++++++++++++++++
 hsarback/src/de/hsadmin/mods/pac/PacComponent.java  |    5 ++
 hsarback/src/de/hsadmin/remote/PacRemote.java       |    2 
 hsarback/src/de/hsadmin/mods/pac/Pac.java           |   14 +++++--
 hsarback/database/data.sql                          |    8 ++--
 hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java |    8 +++-
 6 files changed, 88 insertions(+), 12 deletions(-)

diff --git a/hsarback/database/data.sql b/hsarback/database/data.sql
index ad2c5fb..e759383 100644
--- a/hsarback/database/data.sql
+++ b/hsarback/database/data.sql
@@ -14,7 +14,7 @@
 -- Table: basecomponent
 --
 INSERT INTO basecomponent (basecomponent_code, description, sorting, valid)
-  VALUES ('MULTI', 'Monatliches Datenvolumen in GB', 100, true);
+  	VALUES ('MULTI', 'Accounts', 100, true);
 INSERT INTO basecomponent (basecomponent_code, description, sorting, valid)
 	VALUES ('TRAFFIC', 'Monatliches Datenvolumen in GB', 200, true);
 INSERT INTO basecomponent (basecomponent_code, description, sorting, valid)
@@ -34,11 +34,11 @@
 -- Table: component
 --
 INSERT INTO component (basepacket_id, basecomponent_id, min_quantity, max_quantity, default_quantity, increment_quantity, include_quantity, admin_only, article_number)
-  SELECT basepacket_id, basecomponent_id, 1, 1, 1, 1, 1, false, 100 FROM basepacket, basecomponent WHERE basepacket_code='PAC/DW' AND  basecomponent_code='MULTI';
+	SELECT basepacket_id, basecomponent_id, 1, 10, 1, 1, 1, false, 100 FROM basepacket, basecomponent WHERE basepacket_code='PAC/DW' AND  basecomponent_code='MULTI';
 INSERT INTO component (basepacket_id, basecomponent_id, min_quantity, max_quantity, default_quantity, increment_quantity, include_quantity, admin_only, article_number)
-  SELECT basepacket_id, basecomponent_id, 1, 1, 1, 1, 1, false, 200 FROM basepacket, basecomponent WHERE basepacket_code='PAC/SW' AND  basecomponent_code='MULTI';
+	SELECT basepacket_id, basecomponent_id, 1, 10, 1, 1, 1, false, 200 FROM basepacket, basecomponent WHERE basepacket_code='PAC/SW' AND  basecomponent_code='MULTI';
 INSERT INTO component (basepacket_id, basecomponent_id, min_quantity, max_quantity, default_quantity, increment_quantity, include_quantity, admin_only, article_number)
-  SELECT basepacket_id, basecomponent_id, 1, 1, 1, 1, 1, false, 300 FROM basepacket, basecomponent WHERE basepacket_code='PAC/WEB' AND  basecomponent_code='MULTI';
+	SELECT basepacket_id, basecomponent_id, 1, 10, 1, 1, 1, false, 300 FROM basepacket, basecomponent WHERE basepacket_code='PAC/WEB' AND  basecomponent_code='MULTI';
 INSERT INTO component (basepacket_id, basecomponent_id, min_quantity, max_quantity, default_quantity, increment_quantity, include_quantity, admin_only, article_number)
 	SELECT basepacket_id, basecomponent_id, 128, 40960, 128, 128, 128, false, 101 FROM basepacket, basecomponent WHERE basepacket_code='PAC/DW' AND  basecomponent_code='QUOTA';
 INSERT INTO component (basepacket_id, basecomponent_id, min_quantity, max_quantity, default_quantity, increment_quantity, include_quantity, admin_only, article_number)
diff --git a/hsarback/src/de/hsadmin/mods/pac/Pac.java b/hsarback/src/de/hsadmin/mods/pac/Pac.java
index c4eedb3..2ec6038 100644
--- a/hsarback/src/de/hsadmin/mods/pac/Pac.java
+++ b/hsarback/src/de/hsadmin/mods/pac/Pac.java
@@ -36,13 +36,15 @@
 @AnnModuleImpl(de.hsadmin.mods.pac.PacModuleImpl.class)
 public class Pac extends AbstractEntity implements Serializable {
 	
+	private static final long serialVersionUID = 1L;
+
+	public static final int UNDEFINED_QUANTITY = -1;
+	
 	public static final String PAC_DW = "PAC/DW";
 	public static final String PAC_SW = "PAC/SW";	
 	public static final String PAC_WEB = "PAC/WEB";
 	public static final String PAC_SRV = "SRV/MGD";	
 	
-	private static final long serialVersionUID = 1L;
-
 	@Id
 	@GeneratedValue(strategy = SEQUENCE, generator = "PacsSeqGen")
 	@Column(name = "packet_id", columnDefinition = "integer")
@@ -87,7 +89,7 @@
 	@Transient
 	private BasePac basepac;
 
-	public void initPacComponents(EntityManager em, BasePac aBasepac) {
+	public void initPacComponents(EntityManager em, BasePac aBasepac, boolean setDefaults) {
 		Query qAttachedBasepac = em.createQuery("SELECT b FROM BasePacs b WHERE b.valid = :valid AND b.name = :name");
 		qAttachedBasepac.setParameter("valid", Boolean.TRUE);
 		qAttachedBasepac.setParameter("name", aBasepac.getName());
@@ -100,7 +102,11 @@
 			pacComp.setCreated(today);
 			pacComp.setComponent(comp);
 			pacComp.setPac(this);
-			pacComp.setQuantity(comp.getDefaultQuantity());
+			if (setDefaults) {
+				pacComp.setQuantity(comp.getDefaultQuantity());
+			} else {
+				pacComp.setQuantity(UNDEFINED_QUANTITY);
+			}
 			pacComponents.add(pacComp);
 		}
 	}
diff --git a/hsarback/src/de/hsadmin/mods/pac/PacComponent.java b/hsarback/src/de/hsadmin/mods/pac/PacComponent.java
index 02d60f7..901b626 100644
--- a/hsarback/src/de/hsadmin/mods/pac/PacComponent.java
+++ b/hsarback/src/de/hsadmin/mods/pac/PacComponent.java
@@ -93,4 +93,9 @@
 		this.component = component;
 	}
 
+	@Override
+	public String toString() {
+		return "pac=" + pac.getName() + ";comp=" + component.getBaseComponent().getFeature() + ";quantity=" + getQuantity();
+	}
+
 }
diff --git a/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java b/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java
index ef377cd..3a86c08 100644
--- a/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java
+++ b/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java
@@ -84,7 +84,11 @@
 			pacComponent.setPac(pac);
 			String feature = baseComponent.getFeature();
 			int requestedQuantity = pac.getPacComponent(feature).getQuantity();
-			pacComponent.setQuantity(requestedQuantity);
+			if (requestedQuantity == Pac.UNDEFINED_QUANTITY) {
+				pacComponent.setQuantity(comp.getDefaultQuantity());
+			} else {
+				pacComponent.setQuantity(requestedQuantity);
+			}
 			pacComponents.add(pacComponent);
 		}
 		pac.setPacComponents(pacComponents);
@@ -145,7 +149,7 @@
 			newState.getPacComponents().clear();
 			em.flush();
 			newState.setBasepac(newBasepac);
-			newState.initPacComponents(em, newBasepac);
+			newState.initPacComponents(em, newBasepac, false);
 			Set<PacComponent> newPacComponents = newState.getPacComponents();
 			for (PacComponent pc : newPacComponents) {
 				BaseComponent bc = pc.getBaseComponent();
diff --git a/hsarback/src/de/hsadmin/remote/PacRemote.java b/hsarback/src/de/hsadmin/remote/PacRemote.java
index 69d6f14..2142e31 100644
--- a/hsarback/src/de/hsadmin/remote/PacRemote.java
+++ b/hsarback/src/de/hsadmin/remote/PacRemote.java
@@ -94,7 +94,7 @@
 			hive.setName(hiveName);
 			pac.setHive(hive);
 		}
-		pac.initPacComponents(tx.getEntityManager(), basePac);
+		pac.initPacComponents(tx.getEntityManager(), basePac, false);
 		Object componentsObj = setParams.get("components");
 		if (componentsObj != null && componentsObj instanceof Map) {
 			Map<?, ?> componentsMap = (Map<?, ?>) componentsObj;
diff --git a/hsarback/test/de/hsadmin/remote/PacTest.java b/hsarback/test/de/hsadmin/remote/PacTest.java
index bf4bb18..8335f3e 100644
--- a/hsarback/test/de/hsadmin/remote/PacTest.java
+++ b/hsarback/test/de/hsadmin/remote/PacTest.java
@@ -64,7 +64,7 @@
 	}
 
 	@Test
-	public void testUpdate() {
+	public void testUpdateByPacadminFails() {
 		String user = "aaa00";
 		String grantingTicketURL = cas.getGrantingTicketURL(user);
 		Map<String, String> setParams = new HashMap<String, String>();
@@ -84,6 +84,67 @@
 	}
 
 	@Test
+	public void testUpdateInvalidValueFails() {
+		String user = "ad";
+		String grantingTicketURL = cas.getGrantingTicketURL(user);
+		Map<String, String> componentsMap = new HashMap<String,String>();
+		componentsMap.put("QUOTA", "524");
+		Map<String, Object> setParams = new HashMap<String,Object>();
+		Map<String, Object> whereParams = new HashMap<String,Object>();
+		setParams.put("components", componentsMap);
+		whereParams.put("name", "aaa00");
+		Object[] params = new Object[] { user, 
+				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
+				setParams, whereParams };
+		try {
+			Object execute = client.execute(MODULE + ".update", params);
+			assertNotNull(execute);
+			fail("exception expected");
+		} catch (XmlRpcException e) {
+		}
+	}
+
+	@Test
+	public void testUpdateValidValue() {
+		String user = "ad";
+		String grantingTicketURL = cas.getGrantingTicketURL(user);
+		Map<String, String> componentsMap = new HashMap<String,String>();
+		componentsMap.put("QUOTA", "640");
+		Map<String, Object> setParams = new HashMap<String,Object>();
+		Map<String, Object> whereParams = new HashMap<String,Object>();
+		setParams.put("components", componentsMap);
+		whereParams.put("name", "aaa00");
+		Object[] params = new Object[] { user, 
+				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
+				setParams, whereParams };
+		try {
+			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(Pac.PAC_WEB, pacResultMap.get("basepac"));
+			Object compMapObj = pacResultMap.get("components");
+			assertTrue(compMapObj instanceof Map<?,?>);
+			@SuppressWarnings("unchecked")
+			Map<String,String> compMap = (Map<String, String>) compMapObj;
+			assertEquals("640", compMap.get("QUOTA"));
+			assertEquals("2", compMap.get("TRAFFIC"));
+			assertEquals("1", compMap.get("MULTI"));
+		} catch (XmlRpcException e) {
+			fail(e.getMessage());
+		}
+	}
+
+	@Test
 	public void testCreateWeb() {
 		int count = getPacsCount();
 		String user = "ad";

--
Gitblit v1.9.0-SNAPSHOT