2 * Copyright 2011-2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
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.
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.
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.
36 package gr.grnet.aquarium.message.avro.gen
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}
43 import org.codehaus.jackson.{JsonEncoding, JsonFactory}
47 * @author Christos KK Loverdos <loverdos@gmail.com>
49 class ResourceEventTest {
51 def testJSONSchema() {
52 val rcEvent = new _ResourceEvent()
53 val schema = rcEvent.getSchema
54 val generatedPrettySchema = schema.toString(true)
58 | "name" : "_ResourceEvent",
59 | "namespace" : "gr.grnet.aquarium.message.avro",
63 | "aliases" : [ "originalID", "ID" ]
65 | "name" : "idInStore",
68 | "name" : "occurredMillis",
71 | "name" : "receivedMillis",
78 | "name" : "clientID",
81 | "name" : "eventVersion",
85 | "name" : "resource",
87 | "aliases" : [ "resourceType" ]
89 | "name" : "instanceID",
100 | "name" : "_AnyValue",
102 | "name" : "anyValue",
103 | "type" : [ "null", "int", "long", "boolean", "double", "bytes", "string" ]
110 Assert.assertEquals(goodJSONSchema, generatedPrettySchema)
114 def testJSONObjectOutput() {
115 val rcEvent = _ResourceEvent.newBuilder().
117 setClientID("client-1").
118 setResource("diskspace").
121 setOccurredMillis(1000L).
122 setUserID("foouser").
124 setDetails(new util.HashMap[CharSequence, _AnyValue]()).
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[_ResourceEvent](schema)
136 writer.write(rcEvent, encoder)
140 val generatedJSON = out.toString
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",
158 Assert.assertEquals(goodJSONString, generatedJSON)