Rename all messages
[aquarium] / src / test / scala / gr / grnet / aquarium / message / avro / gen / ResourceEventMsgTest.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.message.avro.gen
37
38 import java.io.ByteArrayOutputStream
39 import org.apache.avro.io.{EncoderFactory, Encoder, JsonEncoder}
40 import org.apache.avro.specific.SpecificDatumWriter
41 import org.junit.{Assert, Test}
42 import java.util
43 import org.codehaus.jackson.{JsonEncoding, JsonFactory}
44
45 /**
46  *
47  * @author Christos KK Loverdos <loverdos@gmail.com>
48  */
49 class ResourceEventMsgTest {
50   @Test
51   def testJSONSchema() {
52     val rcEvent = new ResourceEventMsg()
53     val schema = rcEvent.getSchema
54     val generatedPrettySchema = schema.toString(true)
55     val goodJSONSchema =
56       """{
57         |  "type" : "record",
58         |  "name" : "ResourceEventMsg",
59         |  "namespace" : "gr.grnet.aquarium.message.avro.gen",
60         |  "fields" : [ {
61         |    "name" : "id",
62         |    "type" : "string",
63         |    "aliases" : [ "originalID", "ID" ]
64         |  }, {
65         |    "name" : "idInStore",
66         |    "type" : "string"
67         |  }, {
68         |    "name" : "occurredMillis",
69         |    "type" : "long"
70         |  }, {
71         |    "name" : "receivedMillis",
72         |    "type" : "long",
73         |    "default" : 0
74         |  }, {
75         |    "name" : "userID",
76         |    "type" : "string"
77         |  }, {
78         |    "name" : "clientID",
79         |    "type" : "string"
80         |  }, {
81         |    "name" : "eventVersion",
82         |    "type" : "string",
83         |    "default" : "1.0"
84         |  }, {
85         |    "name" : "resource",
86         |    "type" : "string",
87         |    "aliases" : [ "resourceType" ]
88         |  }, {
89         |    "name" : "instanceID",
90         |    "type" : "string"
91         |  }, {
92         |    "name" : "value",
93         |    "type" : "string"
94         |  }, {
95         |    "name" : "details",
96         |    "type" : {
97         |      "type" : "map",
98         |      "values" : {
99         |        "type" : "record",
100         |        "name" : "AnyValueMsg",
101         |        "fields" : [ {
102         |          "name" : "anyValue",
103         |          "type" : [ "null", "int", "long", "boolean", "double", "bytes", "string" ]
104         |        } ]
105         |      }
106         |    }
107         |  } ]
108         |}""".stripMargin
109
110     Assert.assertEquals(goodJSONSchema, generatedPrettySchema)
111   }
112
113   @Test
114   def testJSONObjectOutput() {
115     val rcEvent = ResourceEventMsg.newBuilder().
116       setId("id-1").
117       setClientID("client-1").
118       setResource("diskspace").
119       setInstanceID("1").
120       setIdInStore("").
121       setOccurredMillis(1000L).
122       setUserID("foouser").
123       setValue("123.32").
124       setDetails(new util.HashMap[CharSequence, AnyValueMsg]()).
125       build()
126
127     val schema = rcEvent.getSchema
128     val out = new ByteArrayOutputStream()
129     val encoder = EncoderFactory.get().jsonEncoder(schema, out)
130     val jsonGenerator = new JsonFactory().
131       createJsonGenerator(out, JsonEncoding.UTF8).
132       useDefaultPrettyPrinter()
133     encoder.configure(jsonGenerator)
134     val writer = new SpecificDatumWriter[ResourceEventMsg](schema)
135
136     writer.write(rcEvent, encoder)
137
138     encoder.flush()
139
140     val generatedJSON = out.toString
141
142     val goodJSONString =
143       """{
144         |  "id" : "id-1",
145         |  "idInStore" : "",
146         |  "occurredMillis" : 1000,
147         |  "receivedMillis" : 0,
148         |  "userID" : "foouser",
149         |  "clientID" : "client-1",
150         |  "eventVersion" : "1.0",
151         |  "resource" : "diskspace",
152         |  "instanceID" : "1",
153         |  "value" : "123.32",
154         |  "details" : {
155         |  }
156         |}""".stripMargin
157
158     Assert.assertEquals(goodJSONString, generatedJSON)
159   }
160 }