Updating fix for bug 3989 based on discussion in OVSDB meeting
[ovsdb.git] / library / src / test / java / org / opendaylight / ovsdb / lib / jsonrpc / JsonRpcDecoderTest.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
3  *
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
7  *
8  *  Authors : Dave Tucker
9  */
10
11 package org.opendaylight.ovsdb.lib.jsonrpc;
12
13 import static io.netty.buffer.Unpooled.copiedBuffer;
14 import static org.junit.Assert.assertEquals;
15
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;
22 import java.net.URL;
23 import org.junit.Before;
24 import org.junit.Test;
25
26 public class JsonRpcDecoderTest {
27     static int testJson_BYTES = 179;
28     String testJson;
29     String prettyTestJson;
30     static final String PREAMBLE = "                    ";
31     static final String PARTIAL_START = "{\"foo\":";
32     static final String PARTIAL_END = "{\"bar\":\"baz\"}}";
33
34     JsonRpcDecoder decoder;
35     EmbeddedChannel ch;
36
37     @Before
38     public void setUp() throws Exception {
39         decoder = new JsonRpcDecoder(1000);
40         ch = new EmbeddedChannel(decoder);
41
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);
46     }
47
48     /**
49      * Test decoding the Stringified Json text in test.json to
50      * individual Json node objects.
51      * @throws Exception
52      */
53     @Test
54     public void testDecode() throws Exception {
55         for (int i = 0; i < 10; i++) {
56             ch.writeInbound(copiedBuffer(testJson, CharsetUtil.UTF_8));
57         }
58         ch.readInbound();
59         assertEquals(10, decoder.getRecordsRead());
60         ch.finish();
61     }
62
63     /**
64      * Test decoding the Stringified Json text in pretty-test.json to
65      * individual Json node objects.
66      * @throws Exception
67      */
68     @Test
69     public void testDecodePrettyJson() throws Exception {
70         ch.writeInbound(copiedBuffer(prettyTestJson, CharsetUtil.UTF_8));
71         ch.readInbound();
72         assertEquals(1, decoder.getRecordsRead());
73         ch.finish();
74     }
75
76     /**
77      * Test decoding the Stringified Json text with large spaces to
78      * individual Json node objects.
79      * @throws Exception
80      */
81     @Test
82     public void testDecodeSkipSpaces() throws Exception {
83         ch.writeInbound(copiedBuffer(PREAMBLE + testJson + PREAMBLE + testJson, CharsetUtil.UTF_8));
84         ch.readInbound();
85         assertEquals(2, decoder.getRecordsRead());
86         ch.finish();
87     }
88
89     /**
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.
93      * @throws Exception
94      */
95     @Test
96     public void testDecodePartial() throws Exception {
97         ch.writeInbound(copiedBuffer(PARTIAL_START, CharsetUtil.UTF_8));
98         ch.readInbound();
99         Thread.sleep(10);
100         ch.writeInbound(copiedBuffer(PARTIAL_END, CharsetUtil.UTF_8));
101         ch.readInbound();
102         assertEquals(1, decoder.getRecordsRead());
103         ch.finish();
104     }
105
106     /**
107      * Test whether decoder throws appropriate DecoderException when
108      * passing a Json string using an unsupported (i.e., UTF-16)
109      * character set.
110      * @throws Exception
111      */
112     @Test(expected= DecoderException.class)
113     public void testDecodeInvalidEncoding() throws Exception {
114         ch.writeInbound(copiedBuffer(testJson, CharsetUtil.UTF_16));
115         ch.finish();
116     }
117     /* Disabling this test as the limit was changed 
118      * from exception to a log warning...
119     /**
120      * Test whether decoder throws appropriate TooLongFrameException
121      * when passing a Json string longer than the decoder's maximum
122      * frame length.
123      * @throws Exception
124      */
125     /*
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));
131         ch.finish();
132     }*/
133 }