Expand timeframe with no repetition defined
authorGeorgios Gousios <gousiosg@gmail.com>
Mon, 5 Dec 2011 15:41:46 +0000 (17:41 +0200)
committerGeorgios Gousios <gousiosg@gmail.com>
Mon, 5 Dec 2011 15:46:46 +0000 (17:46 +0200)
logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLUtils.scala
logic/src/test/scala/gr/grnet/aquarium/logic/test/DSLUtilsTest.scala

index a0bca5b..c06e9bc 100644 (file)
@@ -155,6 +155,13 @@ trait DSLUtils extends DateUtils {
   def allEffectiveTimeslots(spec: DSLTimeFrame, from: Date, to: Date):
   List[Timeslot] = {
 
+    //A timeframe with no repetition defined
+    if (spec.repeat.isEmpty) {
+      val fromDate = if (spec.from.before(from)) from else spec.from
+      val toDate = if (spec.to.getOrElse(to).after(to)) to else spec.to.getOrElse(to)
+      return List(Timeslot(fromDate, toDate))
+    }
+
     val l = spec.repeat.flatMap {
       r => effectiveTimeslots(r, from, Some(to))
     } sortWith sorter
index e158ed9..f27d67d 100644 (file)
@@ -151,7 +151,7 @@ class DSLUtilsTest extends DSLTestBase with DSLUtils with TestMethods {
 
   @Test
   def testAllEffectiveTimeslots = {
-    val from =  new Date(1321621969000L) //Fri Nov 18 15:12:49 +0200 2011
+    var from = new Date(1321621969000L) //Fri Nov 18 15:12:49 +0200 2011
     val to =  new Date(1324214719000L)   //Sun Dec 18 15:25:19 +0200 2011
 
     val repeat1 = DSLTimeFrameRepeat(parseCronString("00 12 * * *"),
@@ -160,9 +160,13 @@ class DSLUtilsTest extends DSLTestBase with DSLUtils with TestMethods {
       parseCronString("00 20 * * 5"))
     val tf = DSLTimeFrame(from, None, List(repeat1, repeat2))
 
-    val result = allEffectiveTimeslots(tf, from, to)
+    var result = allEffectiveTimeslots(tf, from, to)
     assertEquals(36, result.size)
     testSuccessiveTimeslots(result)
+
+    result = allEffectiveTimeslots(DSLTimeFrame(new Date(0), None, List()),
+      new Date(14), new Date(40))
+    assertEquals(1, result.size)
   }
 
   @Test
@@ -206,23 +210,26 @@ class DSLUtilsTest extends DSLTestBase with DSLUtils with TestMethods {
     before
     val agr = creditpolicy.findAgreement("scaledbandwidth").get
 
-    val ts1 = 1322649482000L //Wed, 30 Nov 2011 10:38:02 GMT
-    val ts2 = 1322656682000L //Wed, 30 Nov 2011 12:38:02 GMT
-    val ts3 = 1322660282000L //Wed, 30 Nov 2011 13:38:02 GMT
-    val ts4 = 1322667482000L //Wed, 30 Nov 2011 15:38:02 GMT
-    val ts5 = 1322689082000L //Wed, 30 Nov 2011 21:38:02 GMT
+    val ts1 = 1322649482000L //Wed, 30 Nov 2011 12:38:02 EET
+    val ts2 = 1322656682000L //Wed, 30 Nov 2011 14:38:02 EET
+    val ts3 = 1322660282000L //Wed, 30 Nov 2011 15:38:02 EET
+    val ts4 = 1322667482000L //Wed, 30 Nov 2011 17:38:02 EET
+    val ts5 = 1322689082000L //Wed, 30 Nov 2011 23:38:02 EET
 
     var pricelists = resolveEffectivePricelistsForTimeslot(Timeslot(new Date(ts1), new Date(ts2)), agr)
-    //assertEquals(2, pricelists.keySet.size)
+    assertEquals(2, pricelists.keySet.size)
+    assertNotNone(pricelists.get(new Timeslot(new Date(1322654402000L), new Date(1322656682000L))))
+    assertEquals("foobar", pricelists.head._2.name)
 
     pricelists = resolveEffectivePricelistsForTimeslot(Timeslot(new Date(ts2), new Date(ts3)), agr)
-    //assertEquals(1, pricelists.keySet.size)
+    assertEquals(1, pricelists.keySet.size)
+    assertEquals("default", pricelists.head._2.name)
 
     pricelists = resolveEffectivePricelistsForTimeslot(Timeslot(new Date(ts1), new Date(ts4)), agr)
-    //assertEquals(3, pricelists.keySet.size)
+    assertEquals(3, pricelists.keySet.size)
 
     pricelists = resolveEffectivePricelistsForTimeslot(Timeslot(new Date(ts1), new Date(ts5)), agr)
-    //assertEquals(5, pricelists.keySet.size)
+    assertEquals(5, pricelists.keySet.size)
   }
 
   @tailrec