Merge "explicitly parsing JsonNode for updates" into topic/schema
authorMadhu Venugopal <mavenugo@gmail.com>
Tue, 10 Jun 2014 21:15:13 +0000 (21:15 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Tue, 10 Jun 2014 21:15:13 +0000 (21:15 +0000)
library/src/test/java/org/opendaylight/ovsdb/lib/jsonrpc/JsonRpcDecoderTest.java [new file with mode: 0644]
library/src/test/resources/org/opendaylight/ovsdb/lib/jsonrpc/pretty-test.json [new file with mode: 0644]
library/src/test/resources/org/opendaylight/ovsdb/lib/jsonrpc/test.json [new file with mode: 0644]

diff --git a/library/src/test/java/org/opendaylight/ovsdb/lib/jsonrpc/JsonRpcDecoderTest.java b/library/src/test/java/org/opendaylight/ovsdb/lib/jsonrpc/JsonRpcDecoderTest.java
new file mode 100644 (file)
index 0000000..a6cf060
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2014 Red Hat, Inc.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ *  Authors : Dave Tucker
+ */
+
+package org.opendaylight.ovsdb.lib.jsonrpc;
+
+import static io.netty.buffer.Unpooled.copiedBuffer;
+import static org.junit.Assert.assertEquals;
+
+import com.google.common.base.Charsets;
+import com.google.common.io.Resources;
+import io.netty.channel.embedded.EmbeddedChannel;
+import io.netty.handler.codec.DecoderException;
+import io.netty.handler.codec.TooLongFrameException;
+import io.netty.util.CharsetUtil;
+import java.net.URL;
+import org.junit.Before;
+import org.junit.Test;
+
+public class JsonRpcDecoderTest {
+    static int testJson_BYTES = 179;
+    String testJson;
+    String prettyTestJson;
+    static final String PREAMBLE = "                    ";
+    static final String PARTIAL_START = "{\"foo\":";
+    static final String PARTIAL_END = "{\"bar\":\"baz\"}}";
+
+    JsonRpcDecoder decoder;
+    EmbeddedChannel ch;
+
+    @Before
+    public void setUp() throws Exception {
+        decoder = new JsonRpcDecoder(1000);
+        ch = new EmbeddedChannel(decoder);
+
+        URL testJsonUrl = Resources.getResource(JsonRpcDecoderTest.class, "test.json");
+        testJson = Resources.toString(testJsonUrl, Charsets.UTF_8);
+        URL prettyTestJsoUrl = Resources.getResource(JsonRpcDecoderTest.class, "pretty-test.json");
+        prettyTestJson = Resources.toString(prettyTestJsoUrl, Charsets.UTF_8);
+    }
+
+    @Test
+    public void testDecode() throws Exception {
+        for (int i = 0; i < 10; i++) {
+            ch.writeInbound(copiedBuffer(testJson, CharsetUtil.UTF_8));
+        }
+        ch.readInbound();
+        assertEquals(10, decoder.getRecordsRead());
+        ch.finish();
+    }
+
+    @Test
+    public void testDecodePrettyJson() throws Exception {
+        ch.writeInbound(copiedBuffer(prettyTestJson, CharsetUtil.UTF_8));
+        ch.readInbound();
+        assertEquals(1, decoder.getRecordsRead());
+        ch.finish();
+    }
+
+    @Test
+    public void testDecodeSkipSpaces() throws Exception {
+        ch.writeInbound(copiedBuffer(PREAMBLE + testJson + PREAMBLE + testJson, CharsetUtil.UTF_8));
+        ch.readInbound();
+        assertEquals(2, decoder.getRecordsRead());
+        ch.finish();
+    }
+
+    @Test
+    public void testDecodePartial() throws Exception {
+        ch.writeInbound(copiedBuffer(PARTIAL_START, CharsetUtil.UTF_8));
+        ch.readInbound();
+        Thread.sleep(10);
+        ch.writeInbound(copiedBuffer(PARTIAL_END, CharsetUtil.UTF_8));
+        ch.readInbound();
+        assertEquals(1, decoder.getRecordsRead());
+        ch.finish();
+    }
+
+    @Test(expected= DecoderException.class)
+    public void testDecodeInvalidEncoding() throws Exception {
+        ch.writeInbound(copiedBuffer(testJson, CharsetUtil.UTF_16));
+        ch.finish();
+    }
+
+    @Test(expected=TooLongFrameException.class)
+    public void testDecodeFrameLengthExceed() {
+        decoder = new JsonRpcDecoder(testJson_BYTES -1);
+        ch = new EmbeddedChannel(decoder);
+        ch.writeInbound(copiedBuffer(testJson, CharsetUtil.UTF_8));
+        ch.finish();
+    }
+}
\ No newline at end of file
diff --git a/library/src/test/resources/org/opendaylight/ovsdb/lib/jsonrpc/pretty-test.json b/library/src/test/resources/org/opendaylight/ovsdb/lib/jsonrpc/pretty-test.json
new file mode 100644 (file)
index 0000000..43fe107
--- /dev/null
@@ -0,0 +1,18 @@
+{
+  "Image": {
+    "Width": 800,
+    "Height": 600,
+    "Title": "View from 15th Floor",
+    "Thumbnail": {
+      "Url": "http://www.example.com/image/481989943",
+      "Height": 125,
+      "Width": "100"
+    },
+    "IDs": [
+      116,
+      943,
+      234,
+      38793
+    ]
+  }
+}
\ No newline at end of file
diff --git a/library/src/test/resources/org/opendaylight/ovsdb/lib/jsonrpc/test.json b/library/src/test/resources/org/opendaylight/ovsdb/lib/jsonrpc/test.json
new file mode 100644 (file)
index 0000000..9861b42
--- /dev/null
@@ -0,0 +1 @@
+{"Image":{"Width":800,"Height":600,"Title":"View from 15th Floor","Thumbnail":{"Url":"http://www.example.com/image/481989943","Height":125,"Width":"100"},"IDs":[116,943,234,38793]}}
\ No newline at end of file