First iteration of cost function working
authorGeorgios Gousios <gousiosg@gmail.com>
Thu, 29 Sep 2011 12:30:18 +0000 (15:30 +0300)
committerGeorgios Gousios <gousiosg@gmail.com>
Thu, 29 Sep 2011 12:30:18 +0000 (15:30 +0300)
logic/src/main/scala/gr/grnet/aquarium/logic/Bills.scala
logic/src/test/scala/gr/grnet/aquarium/logic/test/AccountsTest.scala
logic/src/test/scala/gr/grnet/aquarium/logic/test/BillTest.scala

index 66ef55d..a89a8bb 100644 (file)
@@ -1,8 +1,9 @@
 package gr.grnet.aquarium.logic
 
 import gr.grnet.aquarium.model._
-import java.util.Date
 import collection.JavaConversions._
+import java.util.{Date}
+import collection.immutable.HashSet
 
 trait Bills {
 
@@ -12,14 +13,19 @@ trait Bills {
    * Calculate the bills for all resources used by the user
    *
    * @param e The entity to calculate the bill for
-   * @param res The resource to calculate the bill for. If emtpy
+   * @param res The resource to calculate the bill for.
    * @param from The date to start the calculation from.
    * @param to The date up to which the calculation should be done.
    */
   def calcBill(e: Entity, res: Option[ServiceItem],
                from: Option[Date], to: Option[Date]): Float = {
 
-    asScalaSet(e.serviceItems).map {
+    val items = res match {
+      case Some(x) => (new HashSet[ServiceItem]) + x
+      case None => asScalaSet(e.serviceItems)
+    }
+
+    items.map {
       si => asScalaSet(si.configItems).map {
         ci => asScalaSet(ci.runtime).filter {
           rt => {
@@ -28,13 +34,13 @@ trait Bills {
           }
         }.map {
           rt => ci.resource.cost * rt.measurement
-        }.reduce { // Per config item
+        }.fold(0F) { // Per config item
           (total, cost) => total + cost
         }
-      }.reduce { // Per service item
+      }.fold(0F) { // Per service item
         (total, cost) => total + cost
       }
-    }.reduce { // Per entity
+    }.fold(0F) { // Per entity
       (total, cost) => total + cost
     }
   }
index 63ef904..d228358 100644 (file)
@@ -11,7 +11,7 @@ class AccountsTest extends FixtureLoader {
   def before() = {
     if (!DB.getTransaction.isActive)
       DB.getTransaction.begin
-    loadFixture("data.json")
+    //loadFixture("data.json")
   }
 
   @Test
index 9aab590..9e684d6 100644 (file)
@@ -1,9 +1,12 @@
 package gr.grnet.aquarium.logic.test
 
-import gr.grnet.aquarium.model.DB
-import org.junit.{After, Before, Test}
+import org.junit._
+import Assert._
+import gr.grnet.aquarium.logic.Bills
+import gr.grnet.aquarium.model.{Entity, DB}
 
-class BillTest extends FixtureLoader {
+class BillTest
+  extends FixtureLoader with Bills {
 
   @Before
   def before() = {
@@ -14,7 +17,15 @@ class BillTest extends FixtureLoader {
 
   @Test
   def testCalcBill = {
+    var e = DB.find[Entity](classOf[Entity], 1L)
+    assert(e.get != None)
+    var bill = calcBill(e.get)
+    assert(bill == 0F)
 
+    e = DB.find[Entity](classOf[Entity], 6L)
+    assert(e.get != None)
+    bill = calcBill(e.get)
+    assert(bill != 0F)
   }
 
   @After