root / src / test / scala / gr / grnet / aquarium / logic / test / DSLUtilsTest.scala @ 92f5e0d5
History | View | Annotate | Download (9.2 kB)
1 |
/* |
---|---|
2 |
* Copyright 2011 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.Date |
44 |
|
45 |
class DSLUtilsTest extends DSLTestBase with DSLUtils with TestMethods { |
46 |
|
47 |
@Test |
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 |
51 |
|
52 |
var a = DSLTimeSpec(33, 12, -1, -1, 3) |
53 |
var result = expandTimeSpec(a, from, to) |
54 |
assertEquals(4, result.size) |
55 |
|
56 |
a = DSLTimeSpec(33, 12, -1, 10, 3) // Timespec falling outside from-to |
57 |
result = expandTimeSpec(a, from, to) |
58 |
assertEquals(0, result.size) |
59 |
|
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) |
64 |
|
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) |
69 |
|
70 |
// Every day |
71 |
a = DSLTimeSpec(33, 12, -1, -1, -1) |
72 |
result = expandTimeSpec(a, from, to) |
73 |
assertEquals(31, result.size) |
74 |
} |
75 |
|
76 |
@Test |
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 |
80 |
|
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) |
85 |
|
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) |
90 |
} |
91 |
|
92 |
@Test |
93 |
def testMergeOverlaps = { |
94 |
var l = List(Timeslot(new Date(12345000), new Date(13345000)), |
95 |
Timeslot(new Date(12845000), new Date(13845000))) |
96 |
|
97 |
var result = mergeOverlaps(l) |
98 |
assertEquals(1, result.size) |
99 |
assertEquals(Timeslot(new Date(12345000), new Date(13845000)), result.head) |
100 |
|
101 |
l = l ++ List(Timeslot(new Date(13645000), new Date(14845000))) |
102 |
result = mergeOverlaps(l) |
103 |
assertEquals(1, result.size) |
104 |
assertEquals(Timeslot(new Date(12345000), new Date(14845000)), result.head) |
105 |
|
106 |
l = l ++ List(Timeslot(new Date(15845000), new Date(16845000))) |
107 |
result = mergeOverlaps(l) |
108 |
assertEquals(2, result.size) |
109 |
assertEquals(Timeslot(new Date(12345000), new Date(14845000)), result.head) |
110 |
assertEquals(Timeslot(new Date(15845000), new Date(16845000)), result.tail.head) |
111 |
} |
112 |
|
113 |
@Test |
114 |
def testEffectiveTimeslots = { |
115 |
val from = new Date(1321621969000L) //Fri Nov 18 15:12:49 +0200 2011 |
116 |
val to = new Date(1324214719000L) //Sun Dec 18 15:25:19 +0200 2011 |
117 |
|
118 |
var repeat = DSLTimeFrameRepeat( |
119 |
parseCronString("00 12 * * *"), |
120 |
parseCronString("00 14 * * *"), |
121 |
"00 12 * * *", |
122 |
"00 14 * * *" |
123 |
) |
124 |
|
125 |
var result = effectiveTimeslots(repeat, from, Some(to)) |
126 |
|
127 |
assertNotEmpty(result) |
128 |
testSuccessiveTimeslots(result) |
129 |
assertEquals(31, result.size) |
130 |
|
131 |
//Expansion outside timeframe |
132 |
repeat = DSLTimeFrameRepeat( |
133 |
parseCronString("00 12 * May *"), |
134 |
parseCronString("00 14 * Sep *"), |
135 |
"00 12 * May *", |
136 |
"00 14 * Sep *") |
137 |
result = effectiveTimeslots(repeat, from, Some(to)) |
138 |
assertEquals(0, result.size) |
139 |
|
140 |
repeat = DSLTimeFrameRepeat( |
141 |
parseCronString("00 12 * * 5"), |
142 |
parseCronString("00 14 * * 1"), |
143 |
"00 12 * * 5", |
144 |
"00 14 * * 1") |
145 |
result = effectiveTimeslots(repeat, from, Some(to)) |
146 |
testSuccessiveTimeslots(result) |
147 |
assertEquals(4, result.size) |
148 |
|
149 |
repeat = DSLTimeFrameRepeat( |
150 |
parseCronString("00 12 * * Mon,Wed,Fri"), |
151 |
parseCronString("00 14 * * Tue,Thu,Sat"), |
152 |
"00 12 * * Mon,Wed,Fri", |
153 |
"00 14 * * Tue,Thu,Sat") |
154 |
result = effectiveTimeslots(repeat, from, Some(to)) |
155 |
testSuccessiveTimeslots(result) |
156 |
assertEquals(13, result.size) |
157 |
|
158 |
repeat = DSLTimeFrameRepeat( |
159 |
parseCronString("00 00 * May *"), |
160 |
parseCronString("59 23 * Sep *"), |
161 |
"00 00 * May *", |
162 |
"59 23 * Sep *") |
163 |
result = effectiveTimeslots(repeat, new Date(1304121600000L), |
164 |
Some(new Date(1319932800000L))) |
165 |
assertNotEmpty(result) |
166 |
} |
167 |
|
168 |
@Test |
169 |
def testAllEffectiveTimeslots = { |
170 |
var from = new Date(1321621969000L) //Fri Nov 18 15:12:49 +0200 2011 |
171 |
val to = new Date(1324214719000L) //Sun Dec 18 15:25:19 +0200 2011 |
172 |
|
173 |
val repeat1 = DSLTimeFrameRepeat( |
174 |
parseCronString("00 12 * * *"), |
175 |
parseCronString("00 14 * * *"), |
176 |
"00 12 * * *", |
177 |
"00 14 * * *") |
178 |
val repeat2 = DSLTimeFrameRepeat( |
179 |
parseCronString("00 18 * * 5"), |
180 |
parseCronString("00 20 * * 5"), |
181 |
"00 18 * * 5", |
182 |
"00 20 * * 5") |
183 |
val tf = DSLTimeFrame(from, None, List(repeat1, repeat2)) |
184 |
|
185 |
var result = allEffectiveTimeslots(tf, Timeslot(from, to)) |
186 |
assertEquals(36, result.size) |
187 |
testSuccessiveTimeslots(result) |
188 |
|
189 |
result = allEffectiveTimeslots(DSLTimeFrame(new Date(0), None, List()), |
190 |
Timeslot(new Date(14), new Date(40))) |
191 |
assertEquals(1, result.size) |
192 |
} |
193 |
|
194 |
@Test |
195 |
def testNonEffectiveTimeslots = { |
196 |
val from = new Date(1321621969000L) //Fri Nov 18 15:12:49 +0200 2011 |
197 |
val to = new Date(1324214719000L) //Sun Dec 18 15:25:19 +0200 2011 |
198 |
|
199 |
var repeat = DSLTimeFrameRepeat( |
200 |
parseCronString("00 12 * * *"), |
201 |
parseCronString("00 14 * * *"), |
202 |
"00 12 * * *", |
203 |
"00 14 * * *") |
204 |
|
205 |
var result = ineffectiveTimeslots(repeat, from, Some(to)) |
206 |
assertEquals(30, result.size) |
207 |
testSuccessiveTimeslots(result) |
208 |
//printTimeslots(result) |
209 |
} |
210 |
|
211 |
@Test |
212 |
def testTimeContinuum : Unit = { |
213 |
val from = new Date(1321621969000L) //Fri Nov 18 15:12:49 +0200 2011 |
214 |
val to = new Date(1324214719000L) //Sun Dec 18 15:25:19 +0200 2011 |
215 |
|
216 |
var repeat = DSLTimeFrameRepeat( |
217 |
parseCronString("00 12 * * *"), |
218 |
parseCronString("00 14 * * *"), |
219 |
"00 12 * * *", |
220 |
"00 14 * * *" |
221 |
) |
222 |
|
223 |
val continuum = effectiveTimeslots(repeat, from, Some(to)) ++ |
224 |
ineffectiveTimeslots(repeat, from, Some(to)) sortWith sorter |
225 |
|
226 |
testSuccessiveTimeslots(continuum) |
227 |
testNoGaps(continuum) |
228 |
|
229 |
return |
230 |
} |
231 |
|
232 |
@Test |
233 |
def testFindEffective = { |
234 |
before |
235 |
val agr = dsl.findAgreement("scaledbandwidth").get |
236 |
|
237 |
val ts1 = 1322649482000L //Wed, 30 Nov 2011 12:38:02 EET |
238 |
val ts2 = 1322656682000L //Wed, 30 Nov 2011 14:38:02 EET |
239 |
val ts3 = 1322660282000L //Wed, 30 Nov 2011 15:38:02 EET |
240 |
val ts4 = 1322667482000L //Wed, 30 Nov 2011 17:38:02 EET |
241 |
val ts5 = 1322689082000L //Wed, 30 Nov 2011 23:38:02 EET |
242 |
val ts6 = 1322555880000L //Tue, 29 Nov 2011 10:38:00 EET |
243 |
|
244 |
var pricelists = resolveEffectivePricelistsForTimeslot(Timeslot(new Date(ts1), new Date(ts2)), agr) |
245 |
assertEquals(2, pricelists.keySet.size) |
246 |
assertNotNone(pricelists.get(new Timeslot(new Date(1322654402000L), new Date(1322656682000L)))) |
247 |
assertEquals("foobar", pricelists.head._2.name) |
248 |
|
249 |
pricelists = resolveEffectivePricelistsForTimeslot(Timeslot(new Date(ts2), new Date(ts3)), agr) |
250 |
assertEquals(1, pricelists.keySet.size) |
251 |
assertEquals("default", pricelists.head._2.name) |
252 |
|
253 |
pricelists = resolveEffectivePricelistsForTimeslot(Timeslot(new Date(ts1), new Date(ts4)), agr) |
254 |
assertEquals(2, pricelists.keySet.size) |
255 |
assertEquals("foobar", pricelists.head._2.name) |
256 |
assertEquals("default", pricelists.tail.head._2.name) |
257 |
|
258 |
pricelists = resolveEffectivePricelistsForTimeslot(Timeslot(new Date(ts1), new Date(ts5)), agr) |
259 |
assertEquals(4, pricelists.keySet.size) |
260 |
|
261 |
pricelists = resolveEffectivePricelistsForTimeslot(Timeslot(new Date(ts6), new Date(ts5)), agr) |
262 |
assertEquals(9, pricelists.keySet.size) |
263 |
} |
264 |
|
265 |
|
266 |
private def printTimeslots(result: List[Timeslot]) = { |
267 |
result.foreach(p => print("from:%s to:%s\n".format(p.from, p.to))) |
268 |
} |
269 |
} |