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
38 import gr.grnet.aquarium.util.TestMethods
39 import org.junit.{Test}
40 import gr.grnet.aquarium.logic.accounting.dsl.Timeslot
42 import junit.framework.Assert._
43 import gr.grnet.aquarium.logic.accounting.{Accounting}
44 import gr.grnet.aquarium.event.{WalletEntry, ResourceEvent}
45 import com.ckkloverdos.maybe.Just
48 * Tests for the methods that do accounting
50 * @author Georgios Gousios <gousiosg@gmail.com>
52 class AccountingTest extends DSLTestBase with Accounting with TestMethods {
55 def testAlignTimeslots() {
56 var a = List(Timeslot(0,1))
57 var b = List(Timeslot(0,2))
58 var result = alignTimeslots(a, b)
59 assertEquals(2, result.size)
60 assertEquals(result.head, Timeslot(0,1))
61 assertEquals(result.tail.head, Timeslot(1,2))
63 a = List(Timeslot(0,10))
64 b = List(Timeslot(0,4), Timeslot(4,12))
65 result = alignTimeslots(a, b)
66 assertEquals(3, result.size)
67 assertEquals(result.head, Timeslot(0,4))
68 assertEquals(result.tail.head, Timeslot(4,10))
69 assertEquals(result.last, Timeslot(10,12))
71 a = List(Timeslot(0,1), Timeslot(1,3), Timeslot(3,4))
72 b = List(Timeslot(0,2), Timeslot(2,4))
73 result = alignTimeslots(a, b)
74 assertEquals(4, result.size)
75 assertEquals(result.head, Timeslot(0,1))
76 assertEquals(result.tail.head, Timeslot(1,2))
77 assertEquals(result.tail.tail.head, Timeslot(2,3))
78 assertEquals(result.last, Timeslot(3,4))
81 val from = new Date(1322555880000L) //Tue, 29 Nov 2011 10:38:00 EET
82 val to = new Date(1322689082000L) //Wed, 30 Nov 2011 23:38:02 EET
83 val agr = dsl.findAgreement("complextimeslots").get
84 a = resolveEffectiveAlgorithmsForTimeslot(Timeslot(from, to), agr).keySet.toList
85 b = resolveEffectivePricelistsForTimeslot(Timeslot(from, to), agr).keySet.toList
87 result = alignTimeslots(a, b)
88 assertEquals(9, result.size)
89 assertEquals(result.last, b.last)
93 def testSplitChargeChunks() = {
95 val from = new Date(1322555880000L) //Tue, 29 Nov 2011 10:38:00 EET
96 val to = new Date(1322689082000L) //Wed, 30 Nov 2011 23:38:02 EET
98 val agr = dsl.findAgreement("scaledbandwidth").get
100 val alg = resolveEffectiveAlgorithmsForTimeslot(Timeslot(from, to), agr)
101 val price = resolveEffectivePricelistsForTimeslot(Timeslot(from, to), agr)
102 val chunks = splitChargeChunks(alg, price)
103 val algChunks = chunks._1
104 val priceChunks = chunks._2
106 assertEquals(algChunks.size, priceChunks.size)
108 testSuccessiveTimeslots(algChunks.keySet.toList)
109 testSuccessiveTimeslots(priceChunks.keySet.toList)
111 algChunks.keySet.zip(priceChunks.keySet).foreach {
112 t => assertEquals(t._1, t._2)
117 def testChargeEvent(): Unit = {
119 val agr = dsl.findAgreement("scaledbandwidth").get
121 //Simple, continuous resource
122 var evt = ResourceEvent("123", 1325762772000L, 1325762774000L, "12", "1", "bandwidthup", "1", "1", 123, Map())
123 var wallet = chargeEvent(evt, agr, 112, new Date(1325755902000L), List(), None)
125 case Just(x) => assertEquals(2, x.size)
126 case _ => fail("No results returned")
129 //Complex resource event without details, should fail
130 evt = ResourceEvent("123", 1325762772000L, 1325762774000L, "12", "1", "vmtime", "1", "1", 1, Map())
131 assertFailed[Exception, List[WalletEntry]](chargeEvent(evt, agr, 1, new Date(1325755902000L), List(), None))
133 //Complex, onoff resource
134 evt = ResourceEvent("123", 1325762772000L, 1325762774000L, "12", "1", "vmtime", "1", "1", 1, Map("vmid" -> "3"))
135 wallet = chargeEvent(evt, agr, 0, new Date(1325755902000L), List(), None)
137 case Just(x) => assertEquals(2, x.size)
138 case _ => fail("No results returned")
141 //Complex, onoff resource, with wrong states, should fail
142 evt = ResourceEvent("123", 1325762772000L, 1325762774000L, "12", "1", "vmtime", "1", "1", 1, Map("vmid" -> "3"))
143 assertFailed[Exception, List[WalletEntry]](chargeEvent(evt, agr, 1, new Date(1325755902000L), List(), None))
145 //Simple, discrete resource
146 evt = ResourceEvent("123", 1325762772000L, 1325762774000L, "12", "1", "bookpages", "1", "1", 120, Map())
147 wallet = chargeEvent(evt, agr, 15, new Date(1325755902000L), List(), None)
149 case Just(x) => assertEquals(1, x.size)
150 case _ => fail("No results returned")
153 //Simple, discrete resource, time of last update equal to current event's occurred time
154 evt = ResourceEvent("123", 1325762772000L, 1325762774000L, "12", "1", "bookpages", "1", "1", 120, Map())
155 wallet = chargeEvent(evt, agr, 15, new Date(1325762772000L), List(), None)
156 assertEquals(1, wallet.getOr(List(WalletEntry.zero, WalletEntry.zero)).size)
158 //Simple, continuous resource, time of last update equal to current event's occurred time
159 evt = ResourceEvent("123", 1325762772000L, 1325762774000L, "12", "1", "bandwidthup", "1", "1", 123, Map())
160 wallet = chargeEvent(evt, agr, 15, new Date(1325762772000L), List(), None)
161 assertEquals(0, wallet.getOr(List(WalletEntry.zero)).size)