1 package gr.grnet.aquarium
3 import com.ckkloverdos.resource.FileStreamResource
4 import converter.StdConverters
5 import event.model.im.StdIMEvent
6 import event.model.resource.StdResourceEvent
7 import java.io.{InputStreamReader, BufferedReader, File}
8 import com.ckkloverdos.props.Props
9 import store.memory.MemStoreProvider
10 import java.util.concurrent.atomic.AtomicLong
11 import java.text.SimpleDateFormat
12 import java.net.{URLConnection, URL}
16 * Copyright 2011-2012 GRNET S.A. All rights reserved.
18 * Redistribution and use in source and binary forms, with or
19 * without modification, are permitted provided that the following
22 * 1. Redistributions of source code must retain the above
23 * copyright notice, this list of conditions and the following
26 * 2. Redistributions in binary form must reproduce the above
27 * copyright notice, this list of conditions and the following
28 * disclaimer in the documentation and/or other materials
29 * provided with the distribution.
31 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
32 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
35 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
38 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
39 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
41 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGE.
44 * The views and conclusions contained in the software and
45 * documentation are those of the authors and should not be
46 * interpreted as representing official policies, either expressed
47 * or implied, of GRNET S.A.
52 * @author Prodromos Gerakios <pgerakios@grnet.gr>
54 object BillTest extends Loggable {
60 private[this] val counter = new AtomicLong(0L)
61 private[this] def nextID() = counter.getAndIncrement
63 private [this] val format = new SimpleDateFormat("HH/mm/s/dd/MM/yyyy");
65 val propsfile = new FileStreamResource(new File("aquarium.properties"))
67 var props: Props = Props(propsfile)(StdConverters.AllConverters).getOr(Props()(StdConverters.AllConverters))
69 val (astakosExchangeName,astakosRoutingKey) = ("astakos","astakos.user")
71 val (pithosExchangeName,pithosRoutingKey) = ("pithos","pithos.resource.diskspace")
74 exec("mongo aquarium --eval db.resevents.remove();db.imevents.remove();db.policies.remove();db.userstates.remove()",
75 Console.err.println(_))
76 new AquariumBuilder(props, ResourceLocator.DefaultPolicyModel).
77 //update(Aquarium.EnvKeys.storeProvider, new MemStoreProvider).
78 update(Aquarium.EnvKeys.eventsStoreFolder,Some(new File(".."))).
83 private[this] def exec(cmd : String,func : String=>Unit) : Unit = {
84 val commands = cmd.split(" ")
85 val proc = new ProcessBuilder(commands: _*).redirectErrorStream(true).start();
86 val ins = new java.io.BufferedReader(new java.io.InputStreamReader(proc.getInputStream))
87 val sb = new StringBuilder
89 //spin off a thread to read process output.
90 val outputReaderThread = new Thread(new Runnable(){
92 var ln : String = null
93 while({ln = ins.readLine; ln != null})
97 outputReaderThread.start()
99 //suspense this main thread until sub process is done.
102 //wait until output is fully read/completed.
103 outputReaderThread.join()
109 private [this] def createUser(date:DATE) : (JSON,UID) = {
111 val id = "im.%d.create.user".format(mid)
112 val millis = format.parse(date).getTime
113 val occurredMillis = millis
114 val receivedMillis = millis
115 val userID = "user%d@grnet.gr".format(mid)
116 val clientID = "astakos"
119 val eventVersion = "1.0"
120 val eventType = "create"
121 (new StdIMEvent(id,occurredMillis,receivedMillis,userID,
122 clientID,isActive,role,eventVersion,eventType,
123 Map()).toJsonString,mid)
126 private [this] def addCredits(date:DATE,uid:UID,amount:Long) : JSON = {
127 val id = "im.%d.add.credits".format(nextID)
128 val millis = format.parse(date).getTime
129 val occurredMillis = millis
130 val receivedMillis = millis
131 val userID = "user%d@grnet.gr".format(uid)
132 val clientID = "astakos"
135 val eventVersion = "1.0"
136 val eventType = "addcredits"
137 new StdIMEvent(id,occurredMillis,receivedMillis,userID,
138 clientID,isActive,role,eventVersion,eventType,
139 Map("credits" -> amount.toString)).toJsonString
142 private [this] def makePithos(date:DATE,uid:UID,path:String,
143 value:Double,action:String) : JSON = {
144 val id = "rc.%d.object.%s".format(nextID,action)
145 val millis = format.parse(date).getTime
146 val occurredMillis = millis
147 val receivedMillis = millis
148 val userID = "user%d@grnet.gr".format(uid)
149 val clientID = "pithos"
150 val resource ="diskspace"
152 val eventVersion = "1.0"
153 val details = Map("action" -> "object %s".format(action),
157 new StdResourceEvent(id,occurredMillis,receivedMillis,userID,clientID,
158 resource,instanceID,value,eventVersion,details).toJsonString
161 private[this] def sendCreate(date:DATE) : UID = {
162 val (json,uid) = createUser(date)
163 aquarium(Aquarium.EnvKeys.rabbitMQProducer).
164 sendMessage(astakosExchangeName,astakosRoutingKey,json)
165 Console.err.println("Sent message:\n%s\n".format(json))
169 private[this] def sendAddCredits(date:DATE,uid:UID,amount:Long) = {
170 val json = addCredits(date,uid,amount)
171 aquarium(Aquarium.EnvKeys.rabbitMQProducer).
172 sendMessage(astakosExchangeName,astakosRoutingKey,
174 Console.err.println("Sent message:\n%s\n".format(json))
177 private[this] def sendPithos(date:DATE,uid:UID,path:String,
178 value:Double,action:String) = {
179 val json = makePithos(date,uid,path,value,action)
180 aquarium(Aquarium.EnvKeys.rabbitMQProducer).
181 sendMessage(pithosExchangeName,pithosRoutingKey,
183 Console.err.println("Sent message:\n%s\n".format(json))
186 private[this] def jsonOf(url:String) : JSON = {
187 val in = new BufferedReader(
188 new InputStreamReader(
189 new URL(url).openConnection().
193 while ({inputLine = in.readLine();inputLine} != null)
194 ret += (if(ret.isEmpty) "" else "\n")+ inputLine
199 private[this] def getBill(uid:Long,from:String,to:String) : JSON = {
200 val fromMillis = format.parse(from).getTime
201 val toMillis = format.parse(to).getTime
202 val billURL = " http://localhost:8888/user/user%d@grnet.gr/bill/%d/%d".format(uid,fromMillis,toMillis)
211 private[this] def sleep(l:Long) = {
215 case ex:InterruptedException =>
216 Thread.currentThread().interrupt()
220 private[this] def testCase1() : JSON = {
221 /* GET BILL FROM TO*/
222 val billFromDate = "00/00/00/01/08/2012"
223 val billToDate= "23/59/59/31/08/2012"
225 val creationDate = "15/00/00/03/08/2012"
227 val addCreditsDate = "18/15/00/05/08/2012"
228 val creditsToAdd = 6000
230 val pithosPath = "/Papers/GOTO_HARMFUL.PDF"
232 val pithosDate1 = "20/30/00/05/08/2012"
233 val pithosAction1 = "update"
234 val pithosValue1 = 2000
237 val pithosDate2 = "21/05/00/15/08/2012"
238 val pithosAction2 = "update"
239 val pithosValue2 = 4000
242 val pithosDate3 = "08/05/00/20/08/2012"
243 val pithosAction3 = "update"
244 val pithosValue3 = 100
247 sendCreate(creationDate)
249 sendAddCredits(addCreditsDate,id,creditsToAdd)
251 sendPithos(pithosDate1,id,pithosPath,pithosValue1,pithosAction1)
253 sendPithos(pithosDate2,id,pithosPath,pithosValue2,pithosAction2)
255 sendPithos(pithosDate3,id,pithosPath,pithosValue3,pithosAction3)
258 Console.err.println("Waiting for stuff to be processed")
263 while(resp.isEmpty && count < 5){
264 if(count > 0) Console.err.println("Retrying for bill request.")
265 resp = getBill(id,billFromDate,billToDate)
266 if(resp.isEmpty) Thread.sleep(1000)
270 Console.err.println("Sending URL done")
274 def runTestCase(f: => JSON) = {
286 Console.err.println("Response : " + json )
289 def main(args: Array[String]) = {
290 //Console.err.println("JSON: " + (new BillEntry).toJsonString)
291 runTestCase(testCase1)