2 * Copyright (C) 2014 Red Hat, Inc. and others. All rights reserved.
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 package org.opendaylight.ovsdb.lib.jsonrpc;
10 import static io.netty.buffer.Unpooled.copiedBuffer;
11 import static org.junit.Assert.assertEquals;
13 import com.google.common.base.Charsets;
14 import com.google.common.io.Resources;
15 import io.netty.channel.embedded.EmbeddedChannel;
16 import io.netty.handler.codec.DecoderException;
17 import io.netty.util.CharsetUtil;
19 import org.junit.Before;
20 import org.junit.Test;
22 public class JsonRpcDecoderTest {
24 static int testJson_BYTES = 179;
26 String prettyTestJson;
27 static final String PREAMBLE = " ";
28 static final String PARTIAL_START = "{\"foo\":";
29 static final String PARTIAL_END = "{\"bar\":\"baz\"}}";
31 JsonRpcDecoder decoder;
35 public void setUp() throws Exception {
36 decoder = new JsonRpcDecoder(1000);
37 ch = new EmbeddedChannel(decoder);
39 URL testJsonUrl = Resources.getResource(JsonRpcDecoderTest.class, "test.json");
40 testJson = Resources.toString(testJsonUrl, Charsets.UTF_8);
41 URL prettyTestJsoUrl = Resources.getResource(JsonRpcDecoderTest.class, "pretty-test.json");
42 prettyTestJson = Resources.toString(prettyTestJsoUrl, Charsets.UTF_8);
46 * Test decoding the Stringified Json text in test.json to
47 * individual Json node objects.
50 public void testDecode() throws Exception {
51 for (int i = 0; i < 10; i++) {
52 ch.writeInbound(copiedBuffer(testJson, CharsetUtil.UTF_8));
55 assertEquals(10, decoder.getRecordsRead());
60 * Test decoding the Stringified Json text in pretty-test.json to
61 * individual Json node objects.
64 public void testDecodePrettyJson() throws Exception {
65 ch.writeInbound(copiedBuffer(prettyTestJson, CharsetUtil.UTF_8));
67 assertEquals(1, decoder.getRecordsRead());
72 * Test decoding the Stringified Json text with large spaces to
73 * individual Json node objects.
76 public void testDecodeSkipSpaces() throws Exception {
77 ch.writeInbound(copiedBuffer(PREAMBLE + testJson + PREAMBLE + testJson, CharsetUtil.UTF_8));
79 assertEquals(2, decoder.getRecordsRead());
84 * Test whether phased decoding is allowed with JsonRpcDecoder by
85 * writing Json string over two separate iterations, and checking if
86 * the decoder collates the record appropriately.
89 public void testDecodePartial() throws Exception {
90 ch.writeInbound(copiedBuffer(PARTIAL_START, CharsetUtil.UTF_8));
93 ch.writeInbound(copiedBuffer(PARTIAL_END, CharsetUtil.UTF_8));
95 assertEquals(1, decoder.getRecordsRead());
100 * Test whether decoder throws appropriate DecoderException when
101 * passing a Json string using an unsupported (i.e., UTF-16)
104 @Test(expected = DecoderException.class)
105 public void testDecodeInvalidEncoding() throws Exception {
106 ch.writeInbound(copiedBuffer(testJson, CharsetUtil.UTF_16));
110 /* Disabling this test as the limit was changed
111 * from exception to a log warning...
113 * Test whether decoder throws appropriate TooLongFrameException
114 * when passing a Json string longer than the decoder's maximum
119 @Test(expected=TooLongFrameException.class)
120 public void testDecodeFrameLengthExceed() {
121 decoder = new JsonRpcDecoder(testJson_BYTES -1);
122 ch = new EmbeddedChannel(decoder);
123 ch.writeInbound(copiedBuffer(testJson, CharsetUtil.UTF_8));