2 * Copyright (C) 2014 Red Hat, Inc.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 * Authors : Dave Tucker
11 package org.opendaylight.ovsdb.lib.jsonrpc;
13 import static io.netty.buffer.Unpooled.copiedBuffer;
14 import static org.junit.Assert.assertEquals;
16 import com.google.common.base.Charsets;
17 import com.google.common.io.Resources;
18 import io.netty.channel.embedded.EmbeddedChannel;
19 import io.netty.handler.codec.DecoderException;
20 import io.netty.handler.codec.TooLongFrameException;
21 import io.netty.util.CharsetUtil;
23 import org.junit.Before;
24 import org.junit.Test;
26 public class JsonRpcDecoderTest {
27 static int testJson_BYTES = 179;
29 String prettyTestJson;
30 static final String PREAMBLE = " ";
31 static final String PARTIAL_START = "{\"foo\":";
32 static final String PARTIAL_END = "{\"bar\":\"baz\"}}";
34 JsonRpcDecoder decoder;
38 public void setUp() throws Exception {
39 decoder = new JsonRpcDecoder(1000);
40 ch = new EmbeddedChannel(decoder);
42 URL testJsonUrl = Resources.getResource(JsonRpcDecoderTest.class, "test.json");
43 testJson = Resources.toString(testJsonUrl, Charsets.UTF_8);
44 URL prettyTestJsoUrl = Resources.getResource(JsonRpcDecoderTest.class, "pretty-test.json");
45 prettyTestJson = Resources.toString(prettyTestJsoUrl, Charsets.UTF_8);
49 * Test decoding the Stringified Json text in test.json to
50 * individual Json node objects.
54 public void testDecode() throws Exception {
55 for (int i = 0; i < 10; i++) {
56 ch.writeInbound(copiedBuffer(testJson, CharsetUtil.UTF_8));
59 assertEquals(10, decoder.getRecordsRead());
64 * Test decoding the Stringified Json text in pretty-test.json to
65 * individual Json node objects.
69 public void testDecodePrettyJson() throws Exception {
70 ch.writeInbound(copiedBuffer(prettyTestJson, CharsetUtil.UTF_8));
72 assertEquals(1, decoder.getRecordsRead());
77 * Test decoding the Stringified Json text with large spaces to
78 * individual Json node objects.
82 public void testDecodeSkipSpaces() throws Exception {
83 ch.writeInbound(copiedBuffer(PREAMBLE + testJson + PREAMBLE + testJson, CharsetUtil.UTF_8));
85 assertEquals(2, decoder.getRecordsRead());
90 * Test whether phased decoding is allowed with JsonRpcDecoder by
91 * writing Json string over two separate iterations, and checking if
92 * the decoder collates the record appropriately.
96 public void testDecodePartial() throws Exception {
97 ch.writeInbound(copiedBuffer(PARTIAL_START, CharsetUtil.UTF_8));
100 ch.writeInbound(copiedBuffer(PARTIAL_END, CharsetUtil.UTF_8));
102 assertEquals(1, decoder.getRecordsRead());
107 * Test whether decoder throws appropriate DecoderException when
108 * passing a Json string using an unsupported (i.e., UTF-16)
112 @Test(expected= DecoderException.class)
113 public void testDecodeInvalidEncoding() throws Exception {
114 ch.writeInbound(copiedBuffer(testJson, CharsetUtil.UTF_16));
117 /* Disabling this test as the limit was changed
118 * from exception to a log warning...
120 * Test whether decoder throws appropriate TooLongFrameException
121 * when passing a Json string longer than the decoder's maximum
126 @Test(expected=TooLongFrameException.class)
127 public void testDecodeFrameLengthExceed() {
128 decoder = new JsonRpcDecoder(testJson_BYTES -1);
129 ch = new EmbeddedChannel(decoder);
130 ch.writeInbound(copiedBuffer(testJson, CharsetUtil.UTF_8));