From 27cbaa4c22f90d169dfa39ce6ca987dfbe5f2e8c Mon Sep 17 00:00:00 2001 From: Georgios Gousios Date: Thu, 29 Sep 2011 15:30:18 +0300 Subject: [PATCH] First iteration of cost function working --- .../main/scala/gr/grnet/aquarium/logic/Bills.scala | 18 ++++++++++++------ .../gr/grnet/aquarium/logic/test/AccountsTest.scala | 2 +- .../gr/grnet/aquarium/logic/test/BillTest.scala | 17 ++++++++++++++--- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/logic/src/main/scala/gr/grnet/aquarium/logic/Bills.scala b/logic/src/main/scala/gr/grnet/aquarium/logic/Bills.scala index 66ef55d..a89a8bb 100644 --- a/logic/src/main/scala/gr/grnet/aquarium/logic/Bills.scala +++ b/logic/src/main/scala/gr/grnet/aquarium/logic/Bills.scala @@ -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 } } diff --git a/logic/src/test/scala/gr/grnet/aquarium/logic/test/AccountsTest.scala b/logic/src/test/scala/gr/grnet/aquarium/logic/test/AccountsTest.scala index 63ef904..d228358 100644 --- a/logic/src/test/scala/gr/grnet/aquarium/logic/test/AccountsTest.scala +++ b/logic/src/test/scala/gr/grnet/aquarium/logic/test/AccountsTest.scala @@ -11,7 +11,7 @@ class AccountsTest extends FixtureLoader { def before() = { if (!DB.getTransaction.isActive) DB.getTransaction.begin - loadFixture("data.json") + //loadFixture("data.json") } @Test diff --git a/logic/src/test/scala/gr/grnet/aquarium/logic/test/BillTest.scala b/logic/src/test/scala/gr/grnet/aquarium/logic/test/BillTest.scala index 9aab590..9e684d6 100644 --- a/logic/src/test/scala/gr/grnet/aquarium/logic/test/BillTest.scala +++ b/logic/src/test/scala/gr/grnet/aquarium/logic/test/BillTest.scala @@ -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 -- 1.7.10.4