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.groupbasedpolicy.renderer.opflex.jsonrpc;
13 import static io.netty.buffer.Unpooled.copiedBuffer;
15 import static org.junit.Assert.assertEquals;
17 import com.google.common.base.Charsets;
18 import com.google.common.io.Resources;
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;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.opendaylight.groupbasedpolicy.renderer.opflex.jsonrpc.JsonRpcDecoder;
31 public class JsonRpcDecoderTest {
32 static int testJson_BYTES = 179;
34 String prettyTestJson;
35 static final String PREAMBLE = " ";
36 static final String PARTIAL_START = "{\"foo\":";
37 static final String PARTIAL_END = "{\"bar\":\"baz\"}}";
39 JsonRpcDecoder decoder;
43 public void setUp() throws Exception {
44 decoder = new JsonRpcDecoder(1000);
45 ch = new EmbeddedChannel(decoder);
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);
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 public void testDecodePrettyJson() throws Exception {
65 ch.writeInbound(copiedBuffer(prettyTestJson, CharsetUtil.UTF_8));
67 assertEquals(1, decoder.getRecordsRead());
72 public void testDecodeSkipSpaces() throws Exception {
73 ch.writeInbound(copiedBuffer(PREAMBLE + testJson + PREAMBLE + testJson, CharsetUtil.UTF_8));
75 assertEquals(2, decoder.getRecordsRead());
80 public void testDecodePartial() throws Exception {
81 ch.writeInbound(copiedBuffer(PARTIAL_START, CharsetUtil.UTF_8));
84 ch.writeInbound(copiedBuffer(PARTIAL_END, CharsetUtil.UTF_8));
86 assertEquals(1, decoder.getRecordsRead());
90 @Test(expected= DecoderException.class)
91 public void testDecodeInvalidEncoding() throws Exception {
92 ch.writeInbound(copiedBuffer(testJson, CharsetUtil.UTF_16));
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));