2 * Copyright 2011-2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
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.
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.
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.
36 package gr.grnet.aquarium.logic.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}
45 class DSLUtilsTest extends DSLTestBase with DSLUtils with TestMethods {
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
52 var result = findDays(start, end, {
54 c.get(Calendar.DAY_OF_WEEK) == 5}
56 assertEquals(53, result.size)
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
64 val result = adjustToTime(d, 1, 45)
65 assertEquals(target, result)
67 assertThrows(adjustToTime(d, 1, 62))
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
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)
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)
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)
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)
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)
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)
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)
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
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)
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)
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
141 var repeat = DSLTimeFrameRepeat(
142 parseCronString("00 12 * * *"),
143 parseCronString("00 14 * * *"),
148 var result = effectiveTimeslots(repeat, from,to)
150 assertNotEmpty(result)
151 testSuccessiveTimeslots(result)
152 assertEquals(31, result.size)
154 //Expansion outside timeframe
155 repeat = DSLTimeFrameRepeat(
156 parseCronString("00 12 * May *"),
157 parseCronString("00 14 * Sep *"),
160 result = effectiveTimeslots(repeat, from,to)
161 assertEquals(0, result.size)
163 repeat = DSLTimeFrameRepeat(
164 parseCronString("00 12 * * 5"),
165 parseCronString("00 14 * * 1"),
168 result = effectiveTimeslots(repeat, from, to)
169 //testSuccessiveTimeslots(result)
170 assertEquals(4, result.size)
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)
181 repeat = DSLTimeFrameRepeat(
182 parseCronString("00 00 * May *"),
183 parseCronString("59 23 * Sep *"),
186 result = effectiveTimeslots(repeat, new Date(1304121600000L),
187 new Date(1319932800000L))
188 assertNotEmpty(result)
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
196 val repeat1 = DSLTimeFrameRepeat(
197 parseCronString("00 12 * * *"),
198 parseCronString("00 14 * * *"),
201 val repeat2 = DSLTimeFrameRepeat(
202 parseCronString("00 18 * * 5"),
203 parseCronString("00 20 * * 5"),
206 // val tf = DSLTimeFrame(from, None, List(repeat1, repeat2),Nil)
208 /*var result = allEffectiveTimeslots(tf, Timeslot(from, to))
209 assertEquals(36, result.size)
210 testSuccessiveTimeslots(result)
212 result = allEffectiveTimeslots(DSLTimeFrame(new Date(0), None, List(),Nil),
213 Timeslot(new Date(14), new Date(40)))
214 assertEquals(1, result.size)*/
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
222 var repeat = DSLTimeFrameRepeat(
223 parseCronString("00 12 * * *"),
224 parseCronString("00 14 * * *"),
228 var result = ineffectiveTimeslots(repeat, from, Some(to))
229 assertEquals(30, result.size)
230 testSuccessiveTimeslots(result)
231 //printTimeslots(result)
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
239 var repeat = DSLTimeFrameRepeat(
240 parseCronString("00 12 * * *"),
241 parseCronString("00 14 * * *"),
246 val continuum = effectiveTimeslots(repeat, from, Some(to)) ++
247 ineffectiveTimeslots(repeat, from, Some(to)) sortWith sorter
249 testSuccessiveTimeslots(continuum)
250 testNoGaps(continuum)
254 private def times[R](times:Int,block: => R): R = {
256 val (ret,s) = time(block)
258 for { i <- 2 to times} {
259 val (_,t) = time(block)
261 //System.err.println("time" + t + " count " + count)
263 //Console.err.println("Elapsed time: " + (sum/count)/1000000L + "\tms")
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()
276 def testFindEffective = {
278 val agr = dsl.findAgreement("scaledbandwidth").get
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
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))
291 assertEquals(2, pricelists.keySet.size)
292 assertNotNone(pricelists.get(check))
293 assertEquals("foobar", pricelists.head._2.name)
295 pricelists = resolveEffectivePricelistsForTimeslot(Timeslot(new Date(ts2), new Date(ts3)), agr)
296 assertEquals(1, pricelists.keySet.size)
297 assertEquals("default", pricelists.head._2.name)
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)
304 pricelists = resolveEffectivePricelistsForTimeslot(Timeslot(new Date(ts1), new Date(ts5)), agr)
305 assertEquals(4, pricelists.keySet.size)
307 pricelists = times(1,resolveEffectivePricelistsForTimeslot(Timeslot(new Date(ts6), new Date(ts5)), agr))
308 assertEquals(9, pricelists.keySet.size)
312 private def printTimeslots(result: List[Timeslot]) = {
313 result.foreach(p => print("from:%s to:%s\n".format(p.from, p.to)))