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