e42425d0d8652e3c4705632db46ef6a708d5932c
[aquarium] / src / test / scala / gr / grnet / aquarium / logic / test / DSLUtilsTest.scala
1 /*
2  * Copyright 2011-2012 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials
15  *      provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and
31  * documentation are those of the authors and should not be
32  * interpreted as representing official policies, either expressed
33  * or implied, of GRNET S.A.
34  */
35
36 package gr.grnet.aquarium.logic.test
37
38 import org.junit.Test
39 import org.junit.Assert._
40 import gr.grnet.aquarium.util.TestMethods
41 import gr.grnet.aquarium.logic.accounting.dsl._
42 import annotation.tailrec
43 import java.util.{Calendar, Date}
44
45 class DSLUtilsTest extends DSLTestBase with DSLUtils with TestMethods {
46
47  /* @Test
48   def testFindDays() = {
49     var start = new Date(1321530829000L) // 17/11/2011 13:54:02
50     var end = new Date(1353160515000L)   // 17/11/2012 13:55:15
51
52     var result = findDays(start, end, {
53       c =>
54         c.get(Calendar.DAY_OF_WEEK) == 5}
55     )
56     assertEquals(53, result.size)
57   }
58
59   @Test
60   def testAdjustTime() = {
61     var d = new Date(1321615962000L)        // 18/11/2011 13:32:42
62     var target = new Date(1321573542000L)   // 18/11/2011 01:45:42
63
64     val result = adjustToTime(d, 1, 45)
65     assertEquals(target, result)
66
67     assertThrows(adjustToTime(d, 1, 62))
68   }*/
69
70   @Test
71   def testExpandTimeSpec = {
72     val from =  new Date(1321621969000L) //Fri Nov 18 15:12:49 +0200 2011
73     val to =  new Date(1324214719000L)   //Sun Dec 18 15:25:19 +0200 2011
74
75     var a = DSLTimeSpec(33, 12, -1, -1, 3)
76     var b = DSLTimeSpec(34, 12, -1, -1, 3)
77     var result = DSLTimeSpec.expandTimeSpec(a,b,from, to)
78     assertEquals(4, result.size)
79
80     a = DSLTimeSpec(33, 12, -1, 10, 3)   // Timespec falling outside from-to
81     b = DSLTimeSpec(34, 12, -1, -1, 3)
82     result = DSLTimeSpec.expandTimeSpec(a,b,from, to)
83     assertEquals(0, result.size)
84
85     // Would only return an entry if the 1rst of Dec 2011 is Thursday
86     a = DSLTimeSpec(33, 12, 1, -1, 3)
87     b = DSLTimeSpec(34, 12, 1, -1, 3)
88     result = DSLTimeSpec.expandTimeSpec(a,b,from, to)
89     assertEquals(0, result.size)
90
91     // The 9th of Dec 2011 is Friday
92     //Console.err.println("\n\nBEGIN CALCULATION\t\t" + from + "\t\t" + to +  "\n\n")
93     a = DSLTimeSpec(33, 12, 9, -1, 5)
94     b = DSLTimeSpec(34, 12, 9, -1, 5)
95     result = DSLTimeSpec.expandTimeSpec(a,b,from, to)
96     //Console.err.println("\n\nEND CALCULATION: " + result +"\n\n")
97     assertEquals(1, result.size)
98
99     // Every day
100     a = DSLTimeSpec(33, 12, -1, -1, -1)
101     b = DSLTimeSpec(34, 12, -1, -1, -1)
102     result = DSLTimeSpec.expandTimeSpec(a,b,from, to)
103     assertEquals(31, result.size)
104
105     from.setTime(1340614800000L) //  06/25/2012 12:00:00
106     to.setTime(1340982000000L)   // 06/29/2012 18:00:00
107     a = DSLTimeSpec(00, 12, -1, -1, 1 ) // monday at 12:00
108     b = DSLTimeSpec(00, 19, -1, -1, 5) //  friday at 19:00
109     result = DSLTimeSpec.expandTimeSpec(a,b,from, to)
110     assert(result.size==0)
111
112     from.setTime(1340614800000L) //  06/25/2012 12:00:00
113     to.setTime(1341068400000L)   // 06/30/2012 18:00:00
114     a = DSLTimeSpec(00, 12, -1, -1, 1 ) // monday at 12:00
115     b = DSLTimeSpec(00, 19, -1, -1, 5) //  friday at 19:00
116     result = DSLTimeSpec.expandTimeSpec(a,b,from, to)
117     assert(result.size==1)
118   }
119
120   /*@Test
121   def testExpandTimeSpecs = {
122     val from =  new Date(1321621969000L) //Fri Nov 18 15:12:49 +0200 2011
123     val to =  new Date(1324214719000L)   //Sun Dec 18 15:25:19 +0200 2011
124
125     val a = DSLTimeSpec(33, 12, -1, -1, 3)
126     var result = expandTimeSpecs(List(a), from, to)
127     assertNotEmpty(result)
128     assertEquals(4, result.size)
129
130     val b = DSLTimeSpec(00, 18, -1, -1, -1)
131     result = expandTimeSpecs(List(a,b), from, to)
132     assertNotEmpty(result)
133     assertEquals(34, result.size)
134   }*/
135
136   /*@Test
137   def testEffectiveTimeslots = {
138     val from =  new Date(1321621969000L) //Fri Nov 18 15:12:49 +0200 2011
139     val to =  new Date(1324214719000L)   //Sun Dec 18 15:25:19 +0200 2011
140
141     var repeat = DSLTimeFrameRepeat(
142       parseCronString("00 12 * * *"),
143       parseCronString("00 14 * * *"),
144       "00 12 * * *",
145       "00 14 * * *"
146     )
147
148     var result = effectiveTimeslots(repeat, from,to)
149
150     assertNotEmpty(result)
151     testSuccessiveTimeslots(result)
152     assertEquals(31, result.size)
153
154     //Expansion outside timeframe
155     repeat = DSLTimeFrameRepeat(
156       parseCronString("00 12 * May *"),
157       parseCronString("00 14 * Sep *"),
158       "00 12 * May *",
159       "00 14 * Sep *")
160     result = effectiveTimeslots(repeat, from,to)
161     assertEquals(0, result.size)
162
163     repeat = DSLTimeFrameRepeat(
164       parseCronString("00 12 * * 5"),
165       parseCronString("00 14 * * 1"),
166       "00 12 * * 5",
167       "00 14 * * 1")
168     result = effectiveTimeslots(repeat, from, to)
169     //testSuccessiveTimeslots(result)
170     assertEquals(4, result.size)
171
172     repeat = DSLTimeFrameRepeat(
173       parseCronString("00 12 * * Mon,Wed,Fri"),
174       parseCronString("00 14 * * Tue,Thu,Sat"),
175       "00 12 * * Mon,Wed,Fri",
176       "00 14 * * Tue,Thu,Sat")
177     result = effectiveTimeslots(repeat, from, to)
178     //testSuccessiveTimeslots(result)
179     assertEquals(13, result.size)
180
181     repeat = DSLTimeFrameRepeat(
182       parseCronString("00 00 * May *"),
183       parseCronString("59 23 * Sep *"),
184       "00 00 * May *",
185       "59 23 * Sep *")
186     result = effectiveTimeslots(repeat, new Date(1304121600000L),
187       new Date(1319932800000L))
188     assertNotEmpty(result)
189   } */
190
191   /*@Test
192   def testAllEffectiveTimeslots = {
193     var from = new Date(1321621969000L) //Fri Nov 18 15:12:49 +0200 2011
194     val to =  new Date(1324214719000L)   //Sun Dec 18 15:25:19 +0200 2011
195
196     val repeat1 = DSLTimeFrameRepeat(
197       parseCronString("00 12 * * *"),
198       parseCronString("00 14 * * *"),
199       "00 12 * * *",
200       "00 14 * * *")
201     val repeat2 = DSLTimeFrameRepeat(
202       parseCronString("00 18 * * 5"),
203       parseCronString("00 20 * * 5"),
204       "00 18 * * 5",
205       "00 20 * * 5")
206    // val tf = DSLTimeFrame(from, None, List(repeat1, repeat2),Nil)
207
208     /*var result = allEffectiveTimeslots(tf, Timeslot(from, to))
209     assertEquals(36, result.size)
210     testSuccessiveTimeslots(result)
211
212     result = allEffectiveTimeslots(DSLTimeFrame(new Date(0), None, List(),Nil),
213       Timeslot(new Date(14), new Date(40)))
214     assertEquals(1, result.size)*/
215   } */
216
217  /* @Test
218   def testNonEffectiveTimeslots = {
219     val from =  new Date(1321621969000L) //Fri Nov 18 15:12:49 +0200 2011
220     val to =  new Date(1324214719000L)   //Sun Dec 18 15:25:19 +0200 2011
221
222     var repeat = DSLTimeFrameRepeat(
223       parseCronString("00 12 * * *"),
224       parseCronString("00 14 * * *"),
225       "00 12 * * *",
226       "00 14 * * *")
227
228     var result = ineffectiveTimeslots(repeat, from, Some(to))
229     assertEquals(30, result.size)
230     testSuccessiveTimeslots(result)
231     //printTimeslots(result)
232   }*/
233
234   /*@Test
235   def testTimeContinuum : Unit = {
236     val from =  new Date(1321621969000L) //Fri Nov 18 15:12:49 +0200 2011
237     val to =  new Date(1324214719000L)   //Sun Dec 18 15:25:19 +0200 2011
238
239     var repeat = DSLTimeFrameRepeat(
240       parseCronString("00 12 * * *"),
241       parseCronString("00 14 * * *"),
242       "00 12 * * *",
243       "00 14 * * *"
244     )
245
246     val continuum = effectiveTimeslots(repeat, from, Some(to)) ++
247       ineffectiveTimeslots(repeat, from, Some(to)) sortWith sorter
248
249     testSuccessiveTimeslots(continuum)
250     testNoGaps(continuum)
251
252     return
253   } */
254     private def times[R](times:Int,block: => R): R = {
255      val count = times
256      val (ret,s) =  time(block)
257      var sum = s
258      for { i <- 2 to times} {
259        val (_,t) = time(block)
260        sum += t
261        //System.err.println("time" + t +  " count " + count)
262      }
263      //Console.err.println("Elapsed time: " + (sum/count)/1000000L + "\tms")
264      ret
265    }
266
267     private def time[R](block: => R): (R,Long) = {
268      val t0 = System.nanoTime()
269      val result = block    // call-by-name
270      val t1 = System.nanoTime()
271      (result,t1-t0)
272     }
273
274
275   @Test
276   def testFindEffective = {
277     before
278     val agr = dsl.findAgreement("scaledbandwidth").get
279
280     val ts1 = 1322649482000L //Wed, 30 Nov 2011 12:38:02 EET
281     val ts2 = 1322656682000L //Wed, 30 Nov 2011 14:38:02 EET
282     val ts3 = 1322660282000L //Wed, 30 Nov 2011 15:38:02 EET
283     val ts4 = 1322667482000L //Wed, 30 Nov 2011 17:38:02 EET
284     val ts5 = 1322689082000L //Wed, 30 Nov 2011 23:38:02 EET
285     val ts6 = 1322555880000L //Tue, 29 Nov 2011 10:38:00 EET
286
287     var check = new Timeslot(new Date(1322649482000L), new Date(1322654400000L))
288     //Console.err.println("Check " + check)
289     var pricelists = times(1,resolveEffectivePricelistsForTimeslot(Timeslot(new Date(ts1), new Date(ts2)), agr))
290
291     assertEquals(2, pricelists.keySet.size)
292     assertNotNone(pricelists.get(check))
293     assertEquals("foobar", pricelists.head._2.name)
294
295     pricelists = resolveEffectivePricelistsForTimeslot(Timeslot(new Date(ts2), new Date(ts3)), agr)
296     assertEquals(1, pricelists.keySet.size)
297     assertEquals("default", pricelists.head._2.name)
298
299     pricelists = resolveEffectivePricelistsForTimeslot(Timeslot(new Date(ts1), new Date(ts4)), agr)
300     assertEquals(2, pricelists.keySet.size)
301     assertEquals("foobar", pricelists.head._2.name)
302     assertEquals("default", pricelists.tail.head._2.name)
303
304     pricelists = resolveEffectivePricelistsForTimeslot(Timeslot(new Date(ts1), new Date(ts5)), agr)
305     assertEquals(4, pricelists.keySet.size)
306
307     pricelists = times(1,resolveEffectivePricelistsForTimeslot(Timeslot(new Date(ts6), new Date(ts5)), agr))
308     assertEquals(9, pricelists.keySet.size)
309   }
310
311
312   private def printTimeslots(result: List[Timeslot]) = {
313     result.foreach(p => print("from:%s to:%s\n".format(p.from, p.to)))
314   }
315 }