2 * Copyright (C) 2013 EBay Software Foundation
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 io.netty.handler.logging.LogLevel;
11 import io.netty.handler.logging.LoggingHandler;
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import java.net.Socket;
15 import junit.framework.TestCase;
16 import org.junit.Test;
18 public class TestClient extends TestCase {
20 String serverurl = "127.0.0.1";
21 NettyBootStrapper bootstrapper = new NettyBootStrapper();
22 JsonRpcDecoder jsonRpcDecoder = new JsonRpcDecoder(100000);
24 public void setupServer() throws Exception {
25 bootstrapper.startServer(0, jsonRpcDecoder, new LoggingHandler(LogLevel.DEBUG));
28 public void shutDownServer() throws InterruptedException {
29 bootstrapper.stopServer();
33 * Testing appropriate ChannelHandler integration for JsonRpcDecoder, so that JSON strings written using an
34 * OutputStream connected to a ServerSocket of a Netty ServerBootstrap can be decoded properly.
37 public void testBasicFlow() throws Exception {
39 Socket socket = new Socket(serverurl, bootstrapper.getServerPort());
40 OutputStream outputStream = socket.getOutputStream();
44 for (int i = 0; i < records; i++) {
45 writeJson(outputStream, 1);
46 writePartialFirst(outputStream);
49 writePartialLast(outputStream);
54 assertEquals("mismatch in records processed", records * 2, jsonRpcDecoder.getRecordsRead());
57 static int counter = 0;
60 * Create and write a json string for specified number of times.
62 private void writeJson(OutputStream outputStream, int times) throws IOException {
63 outputStream.write("{".getBytes("UTF-8"));
64 for (int i = 0; i < times; i++) {
66 String string = ",\"key1" + counter + "\":\"planet of apes" + counter + "\", \"key2" + counter
67 + "\":{\"k1\":\"ovs-db rocks the world\"}";
68 outputStream.write(string.substring(i == 0 ? 1 : 0).getBytes("UTF-8"));
70 outputStream.write("}".getBytes("UTF-8"));
74 * Writes a partial JSON and flush to simulate the case where netty gets half the message and has to frame it
77 private void writePartialFirst(OutputStream outputStream) throws IOException {
79 String string = " {\"part" + counter + "\":";
80 outputStream.write(string.getBytes("UTF-8"));
84 * Finishes the JSON started by writePartialFirst.
86 private void writePartialLast(OutputStream outputStream) throws IOException {
87 String string = "\"val" + counter + "\"}";
88 outputStream.write(string.getBytes("UTF-8"));