root / logic / src / test / scala / gr / grnet / aquarium / rest / actor / RESTActorTest.scala @ e4721b31
History | View | Annotate | Download (3.8 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.rest.actor |
37 |
|
38 |
import org.junit.Test |
39 |
import org.junit.Assert._ |
40 |
import org.junit.Assume.assumeTrue |
41 |
|
42 |
import gr.grnet.aquarium.MasterConf |
43 |
import gr.grnet.aquarium.actor.RESTRole |
44 |
import cc.spray.can.HttpMethods.{GET, POST} |
45 |
import cc.spray.can.HttpClient._ |
46 |
import cc.spray.can.HttpClient.{HttpDialog ⇒ SprayHttpDialog} |
47 |
import cc.spray.can.{HttpResponse, HttpHeader, HttpRequest, HttpServer ⇒ SprayHttpServer, HttpClient ⇒ SprayHttpClient} |
48 |
import akka.actor.{PoisonPill, Actor} |
49 |
import gr.grnet.aquarium.logic.events.ResourceEvent |
50 |
import net.liftweb.json.JsonAST.JInt |
51 |
|
52 |
/** |
53 |
* |
54 |
* @author Christos KK Loverdos <loverdos@gmail.com>. |
55 |
*/ |
56 |
class RESTActorTest { |
57 |
@Test |
58 |
def testOneCall: Unit = { |
59 |
// Initialize configuration subsystem |
60 |
val mc = MasterConf.MasterConf |
61 |
val port = mc.props.getInt(MasterConf.Keys.rest_port).getOr(8080) |
62 |
// Initialize REST actor |
63 |
val rest = mc.actorProvider.actorForRole(RESTRole) |
64 |
// Initialize spray underlying server |
65 |
val server = Actor.actorOf(new SprayHttpServer()).start() |
66 |
val client = Actor.actorOf(new SprayHttpClient()).start() |
67 |
val dialog = SprayHttpDialog("localhost", port) |
68 |
|
69 |
val pingReq = HttpRequest(method = GET, uri = "/ping", headers = HttpHeader("Content-Type", "text/plain; charset=UTF-8")::Nil) |
70 |
dialog.send(pingReq).end onComplete { futureResp ⇒ |
71 |
futureResp.value match { |
72 |
case Some(Right(HttpResponse(status, _, bytesBody, _))) ⇒ |
73 |
assertTrue("Status 200 OK", status == 200) |
74 |
val stringBody = new String(bytesBody, "UTF-8") |
75 |
println("!! Got stringBody = %s".format(stringBody)) |
76 |
// Note that the response is in JSON format, so must parse it |
77 |
implicit val formats = ResourceEvent.DefaultJsonFormats |
78 |
val jValue = net.liftweb.json.parse(stringBody) |
79 |
println("!! ==> jValue = %s".format(jValue)) |
80 |
val pongValue = jValue \ "pong" |
81 |
println("!! ==> pongValue = %s".format(pongValue)) |
82 |
assertTrue("pong Int in response", pongValue.isInstanceOf[JInt]) |
83 |
case Some(Left(error)) ⇒ |
84 |
fail("Got error: %s".format(error.getMessage)) |
85 |
case None ⇒ |
86 |
fail("Got nothing") |
87 |
} |
88 |
} |
89 |
|
90 |
Thread.sleep(3000) |
91 |
server ! PoisonPill |
92 |
client ! PoisonPill |
93 |
Actor.registry.shutdownAll() |
94 |
} |
95 |
} |