2 * Copyright 2011 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
45 class DSLUtilsTest extends DSLTestBase with DSLUtils with TestMethods {
48 def testExpandTimeSpec = {
49 val from = new Date(1321621969000L) //Fri Nov 18 15:12:49 +0200 2011
50 val to = new Date(1324214719000L) //Sun Dec 18 15:25:19 +0200 2011
52 var a = DSLTimeSpec(33, 12, -1, -1, 3)
53 var result = expandTimeSpec(a, from, to)
54 assertEquals(4, result.size)
56 a = DSLTimeSpec(33, 12, -1, 10, 3) // Timespec falling outside from-to
57 result = expandTimeSpec(a, from, to)
58 assertEquals(0, result.size)
60 // Would only return an entry if the 1rst of Dec 2011 is Thursday
61 a = DSLTimeSpec(33, 12, 1, -1, 3)
62 result = expandTimeSpec(a, from, to)
63 assertEquals(0, result.size)
65 // The 9th of Dec 2011 is Friday
66 a = DSLTimeSpec(33, 12, 9, -1, 5)
67 result = expandTimeSpec(a, from, to)
68 assertEquals(1, result.size)
71 a = DSLTimeSpec(33, 12, -1, -1, -1)
72 result = expandTimeSpec(a, from, to)
73 assertEquals(31, result.size)
77 def testExpandTimeSpecs = {
78 val from = new Date(1321621969000L) //Fri Nov 18 15:12:49 +0200 2011
79 val to = new Date(1324214719000L) //Sun Dec 18 15:25:19 +0200 2011
81 val a = DSLTimeSpec(33, 12, -1, -1, 3)
82 var result = expandTimeSpecs(List(a), from, to)
83 assertNotEmpty(result)
84 assertEquals(4, result.size)
86 val b = DSLTimeSpec(00, 18, -1, -1, -1)
87 result = expandTimeSpecs(List(a,b), from, to)
88 assertNotEmpty(result)
89 assertEquals(34, result.size)
93 def testEffectiveTimeslots = {
94 val from = new Date(1321621969000L) //Fri Nov 18 15:12:49 +0200 2011
95 val to = new Date(1324214719000L) //Sun Dec 18 15:25:19 +0200 2011
97 var repeat = DSLTimeFrameRepeat(parseCronString("00 12 * * *"),
98 parseCronString("00 14 * * *"))
100 var result = effectiveTimeslots(repeat, from, Some(to))
102 assertNotEmpty(result)
103 testSuccessiveTimeslots(result)
104 assertEquals(31, result.size)
106 //Expansion outside timeframe
107 repeat = DSLTimeFrameRepeat(parseCronString("00 12 * May *"),
108 parseCronString("00 14 * Sep *"))
109 result = effectiveTimeslots(repeat, from, Some(to))
110 assertEquals(0, result.size)
112 repeat = DSLTimeFrameRepeat(parseCronString("00 12 * * 5"),
113 parseCronString("00 14 * * 1"))
114 result = effectiveTimeslots(repeat, from, Some(to))
115 testSuccessiveTimeslots(result)
116 assertEquals(4, result.size)
118 repeat = DSLTimeFrameRepeat(parseCronString("00 12 * * Mon,Wed,Fri"),
119 parseCronString("00 14 * * Tue,Thu,Sat"))
120 result = effectiveTimeslots(repeat, from, Some(to))
121 testSuccessiveTimeslots(result)
122 assertEquals(13, result.size)
124 repeat = DSLTimeFrameRepeat(parseCronString("00 00 * May *"),
125 parseCronString("59 23 * Sep *"))
126 result = effectiveTimeslots(repeat, new Date(1304121600000L),
127 Some(new Date(1319932800000L)))
128 assertNotEmpty(result)
132 def testAllEffectiveTimeslots = {
133 val from = new Date(1321621969000L) //Fri Nov 18 15:12:49 +0200 2011
134 val to = new Date(1324214719000L) //Sun Dec 18 15:25:19 +0200 2011
136 val repeat1 = DSLTimeFrameRepeat(parseCronString("00 12 * * *"),
137 parseCronString("00 14 * * *"))
138 val repeat2 = DSLTimeFrameRepeat(parseCronString("00 18 * * 5"),
139 parseCronString("00 20 * * 5"))
140 val tf = DSLTimeFrame(from, None, List(repeat1, repeat2))
142 val result = allEffectiveTimeslots(tf, from, to)
143 assertEquals(36, result.size)
144 testSuccessiveTimeslots(result)
148 def testNonEffectiveTimeslots = {
149 val from = new Date(1321621969000L) //Fri Nov 18 15:12:49 +0200 2011
150 val to = new Date(1324214719000L) //Sun Dec 18 15:25:19 +0200 2011
152 var repeat = DSLTimeFrameRepeat(parseCronString("00 12 * * *"),
153 parseCronString("00 14 * * *"))
155 var result = ineffectiveTimeslots(repeat, from, Some(to))
156 assertEquals(30, result.size)
157 testSuccessiveTimeslots(result)
158 //printTimeslots(result)
162 def testTimeContinuum : Unit = {
163 val from = new Date(1321621969000L) //Fri Nov 18 15:12:49 +0200 2011
164 val to = new Date(1324214719000L) //Sun Dec 18 15:25:19 +0200 2011
166 var repeat = DSLTimeFrameRepeat(parseCronString("00 12 * * *"),
167 parseCronString("00 14 * * *"))
169 val continuum = effectiveTimeslots(repeat, from, Some(to)) ++
170 ineffectiveTimeslots(repeat, from, Some(to)) sortWith sorter
172 testSuccessiveTimeslots(continuum)
176 if(a.to.getTime - b.from.getTime > 1)
177 fail("Effectivity timeslots leave gaps: %s %s".format(a, b))
184 def testFindEffective = {
187 val agr = creditpolicy.findAgreement("scaledbandwidth").get
189 val ts1 = 1322649482000L //Wed, 30 Nov 2011 10:38:02 GMT
190 val ts2 = 1322656682000L //Wed, 30 Nov 2011 12:38:02 GMT
191 val ts3 = 1322660282000L //Wed, 30 Nov 2011 13:38:02 GMT
192 val ts4 = 1322667482000L //Wed, 30 Nov 2011 15:38:02 GMT
193 val ts5 = 1322689082000L //Wed, 30 Nov 2011 21:38:02 GMT
195 var pricelists = resolveEffectivePricelistsForTimeslot(Timeslot(new Date(ts1), new Date(ts2)), agr)
196 //assertEquals(2, pricelists.keySet.size)
198 pricelists = resolveEffectivePricelistsForTimeslot(Timeslot(new Date(ts2), new Date(ts3)), agr)
199 //assertEquals(1, pricelists.keySet.size)
201 pricelists = resolveEffectivePricelistsForTimeslot(Timeslot(new Date(ts1), new Date(ts4)), agr)
202 //assertEquals(3, pricelists.keySet.size)
204 pricelists = resolveEffectivePricelistsForTimeslot(Timeslot(new Date(ts1), new Date(ts5)), agr)
205 //assertEquals(5, pricelists.keySet.size)
209 private def testSuccessiveTimeslots(result: List[Timeslot]): Unit = {
210 if (result.isEmpty) return
211 if (result.tail.isEmpty) return
212 if (result.head.to.after(result.tail.head.from))
213 fail("Effectivity timeslots not successive: %s %s".format(result.head, result.tail.head))
214 testSuccessiveTimeslots(result.tail)
217 private def printTimeslots(result: List[(Date, Date)]) = {
218 result.foreach(p => print("from:%s to:%s\n".format(p._1, p._2)))