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 {
* 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 => {
}
}.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
}
}
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() = {
@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