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