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