Feature uses features-parent as parent
[groupbasedpolicy.git] / renderers / opflex / src / test / java / org / opendaylight / groupbasedpolicy / renderer / opflex / 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.groupbasedpolicy.renderer.opflex.jsonrpc;
12
13 import static io.netty.buffer.Unpooled.copiedBuffer;
14
15 import static org.junit.Assert.assertEquals;
16
17 import com.google.common.base.Charsets;
18 import com.google.common.io.Resources;
19
20 import io.netty.channel.embedded.EmbeddedChannel;
21 import io.netty.handler.codec.DecoderException;
22 import io.netty.handler.codec.TooLongFrameException;
23 import io.netty.util.CharsetUtil;
24
25 import java.net.URL;
26
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.opendaylight.groupbasedpolicy.renderer.opflex.jsonrpc.JsonRpcDecoder;
30
31 public class JsonRpcDecoderTest {
32     static int testJson_BYTES = 179;
33     String testJson;
34     String prettyTestJson;
35     static final String PREAMBLE = "                    ";
36     static final String PARTIAL_START = "{\"foo\":";
37     static final String PARTIAL_END = "{\"bar\":\"baz\"}}";
38
39     JsonRpcDecoder decoder;
40     EmbeddedChannel ch;
41
42     @Before
43     public void setUp() throws Exception {
44         decoder = new JsonRpcDecoder(1000);
45         ch = new EmbeddedChannel(decoder);
46
47         URL testJsonUrl = Resources.getResource(JsonRpcDecoderTest.class, "test.json");
48         testJson = Resources.toString(testJsonUrl, Charsets.UTF_8);
49         URL prettyTestJsoUrl = Resources.getResource(JsonRpcDecoderTest.class, "pretty-test.json");
50         prettyTestJson = Resources.toString(prettyTestJsoUrl, Charsets.UTF_8);
51     }
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     @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     @Test
72     public void testDecodeSkipSpaces() throws Exception {
73         ch.writeInbound(copiedBuffer(PREAMBLE + testJson + PREAMBLE + testJson, CharsetUtil.UTF_8));
74         ch.readInbound();
75         assertEquals(2, decoder.getRecordsRead());
76         ch.finish();
77     }
78
79     @Test
80     public void testDecodePartial() throws Exception {
81         ch.writeInbound(copiedBuffer(PARTIAL_START, CharsetUtil.UTF_8));
82         ch.readInbound();
83         Thread.sleep(10);
84         ch.writeInbound(copiedBuffer(PARTIAL_END, CharsetUtil.UTF_8));
85         ch.readInbound();
86         assertEquals(1, decoder.getRecordsRead());
87         ch.finish();
88     }
89
90     @Test(expected= DecoderException.class)
91     public void testDecodeInvalidEncoding() throws Exception {
92         ch.writeInbound(copiedBuffer(testJson, CharsetUtil.UTF_16));
93         ch.finish();
94     }
95
96     @Test(expected=TooLongFrameException.class)
97     public void testDecodeFrameLengthExceed() {
98         decoder = new JsonRpcDecoder(testJson_BYTES -1);
99         ch = new EmbeddedChannel(decoder);
100         ch.writeInbound(copiedBuffer(testJson, CharsetUtil.UTF_8));
101         ch.finish();
102     }
103