Termination condition for billing month calculation
[aquarium] / src / test / scala / gr / grnet / aquarium / user / UserStateComputationsTest.scala
1 package gr.grnet.aquarium.user
2
3 import org.junit.Test
4 import gr.grnet.aquarium.Configurator
5 import gr.grnet.aquarium.store.memory.MemStore
6 import gr.grnet.aquarium.util.date.DateCalculator
7 import gr.grnet.aquarium.logic.accounting.dsl._
8 import java.util.Date
9 import gr.grnet.aquarium.logic.accounting.Accounting
10 import com.ckkloverdos.maybe.NoVal
11 import simulation.{ConcurrentVMLocalUIDGenerator, ClientServiceSim, UserSim}
12
13
14 /**
15  *
16  * @author Christos KK Loverdos <loverdos@gmail.com>
17  */
18 class UserStateComputationsTest {
19   @Test
20   def testOne: Unit = {
21     val StartOfBillingYearDateCalc = new DateCalculator(2012, 1, 1)
22 //    println("StartOfBillingYearDateCalc = %s".format(StartOfBillingYearDateCalc))
23     val UserCreationDateCalc = StartOfBillingYearDateCalc.copy.goMinusMonths(2)
24 //    println("UserCreationDateCalc = %s".format(UserCreationDateCalc))
25
26     // TODO: integrate this with the rest of the simulation stuff
27     // TODO: since, right now, the resource strings have to be given twice
28     val VMTIME_RESOURCE    = DSLComplexResource("vmtime",    "Hr",    OnOffCostPolicy,      "")
29     val DISKSPACE_RESOURCE = DSLComplexResource("diskspace", "MB/Hr", ContinuousCostPolicy, "")
30
31     val DEFAULT_RESOURCES_MAP = new DSLResourcesMap(VMTIME_RESOURCE :: DISKSPACE_RESOURCE :: Nil)
32
33     val FOR_EVER = DSLTimeFrame(new Date(0), None, Nil)
34     val THE_PRICES_MAP: Map[DSLResource, Double] = Map(VMTIME_RESOURCE -> 1.0, DISKSPACE_RESOURCE -> 1.0)
35
36     val THE_PRICE_LIST  = DSLPriceList("default", None, THE_PRICES_MAP, FOR_EVER)
37     val THE_CREDIT_PLAN = DSLCreditPlan("default", None, 100, Nil, "", FOR_EVER)
38     val THE_ALGORITHM   = DSLAlgorithm("default", None, Map(), FOR_EVER)
39     val THE_AGREEMENT   = DSLAgreement("default", None, THE_ALGORITHM, THE_PRICE_LIST, THE_CREDIT_PLAN)
40
41     val THE_RESOURCES = DEFAULT_RESOURCES_MAP.toResourcesList
42
43     val DEFAULT_POLICY = DSLPolicy(
44       Nil,
45       THE_PRICE_LIST :: Nil,
46       THE_RESOURCES,
47       THE_CREDIT_PLAN :: Nil,
48       THE_AGREEMENT :: Nil
49     )
50
51     val computer = new UserStateComputations
52
53     val mc = Configurator.MasterConfigurator.withStoreProviderClass(classOf[MemStore])
54     val storeProvider = mc.storeProvider
55     val userStateStore = storeProvider.userStateStore
56     val resourceEventStore = storeProvider.resourceEventStore
57     val policyStore = storeProvider.policyStore
58
59     // A new user is created on 2012-01-15 00:00:00.000
60     val christos  = UserSim("Christos", UserCreationDateCalc.toDate, storeProvider.resourceEventStore)
61
62     // There are two client services, synnefo and pithos.
63     val uidGenerator = new ConcurrentVMLocalUIDGenerator
64     val synnefo = ClientServiceSim("synnefo")(uidGenerator)
65     val pithos  = ClientServiceSim("pithos")(uidGenerator)
66
67     // By convention
68     // - synnefo is for VMTime and
69     // - pithos is for Diskspace
70     val vm   = synnefo.newVMTime(christos, "VM.1")
71     val disk = pithos.newDiskspace(christos, "DISK.1")
72
73     // Let's create our dates of interest
74     val vmStartDateCalc = StartOfBillingYearDateCalc.copy.goPlusDays(1).goPlusHours(1)
75 //    println("vmStartDateCalc = %s".format(vmStartDateCalc))
76     // 2012-01-16 01:00:00.000
77     val vmStartDate = vmStartDateCalc.toDate
78
79     // Within January, create one VM ON-OFF ...
80     val onOff1_M = vm.newONOFF(vmStartDate, 9)
81
82     val diskConsumptionDateCalc = StartOfBillingYearDateCalc.copy.goPlusHours(3)
83     // 2012-01-16 04:00:00.000
84     val diskConsumptionDate1 = diskConsumptionDateCalc.toDate
85     // 2012-01-17 05:00:00.000
86     val diskConsumptionDate2 = diskConsumptionDateCalc.copy.goPlusDays(1).goPlusHours(1).toDate
87
88     // ... and two diskspace changes
89     val consume1_M = disk.consumeMB(diskConsumptionDate1, 99)
90     val consume2_M = disk.consumeMB(diskConsumptionDate2, 23)
91
92     // ... and one "future" event
93     // 2012-02-07 07:07:07.007
94     disk.consumeMB(
95       StartOfBillingYearDateCalc.copy.
96         goNextMonth.goPlusDays(6).
97         goPlusHours(7).
98         goPlusMinutes(7).
99         goPlusSeconds(7).
100         goPlusMillis(7).toDate,
101       777)
102
103     println("=============================")
104     for {
105       event <- christos.myResourceEventsByReceivedDate
106     } {
107       val when = new DateCalculator(event.receivedMillis)
108       val resource  = event.resource
109       val instanceId = event.instanceId
110       val value = event.beautifyValue(DEFAULT_RESOURCES_MAP)
111       println("%s [%s, %s] %s".format(when, resource, instanceId, value))
112     }
113     println("=============================")
114
115     val userStateM = computer.doFullMonthlyBilling(
116       christos.userId,
117       StartOfBillingYearDateCalc.getYear,
118       StartOfBillingYearDateCalc.getMonthOfYear,
119       userStateStore,
120       resourceEventStore,
121       policyStore,
122       christos.userCreationDate.getTime,
123       computer.createFirstUserState(christos.userId),
124       computer.createFirstUserState(christos.userId),
125       DEFAULT_POLICY,
126       DEFAULT_RESOURCES_MAP,
127       new Accounting{}
128     )
129     
130     println("!! userStateM = %s".format(userStateM))
131     userStateM.forFailed { failed ⇒
132       failed.exception.printStackTrace()
133       NoVal
134     }
135   }
136 }