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 * Authors : Aswin Raveendran
10 package org.opendaylight.ovsdb.lib.jsonrpc;
12 import io.netty.handler.logging.LogLevel;
13 import io.netty.handler.logging.LoggingHandler;
14 import junit.framework.TestCase;
15 import org.junit.Test;
17 import java.io.OutputStream;
18 import java.io.IOException;
19 import java.net.Socket;
21 public class TestClient extends TestCase {
23 String serverurl = "127.0.0.1";
24 int serverport = 8080;
26 NettyBootStrapper bootstrapper = new NettyBootStrapper();
27 JsonRpcDecoder jsonRpcDecoder = new JsonRpcDecoder(100000);
29 public void setupServer() throws Exception {
30 bootstrapper.startServer(serverport,
32 new LoggingHandler(LogLevel.DEBUG));
35 public void shutDownServer() throws InterruptedException {
36 bootstrapper.stopServer();
40 public void testBasicFlow() throws Exception {
42 Socket socket = socket = new Socket(serverurl, serverport);
44 OutputStream outputStream = socket.getOutputStream();
48 for (int i = 0; i < records; i++) {
49 writeJson(outputStream, 1);
50 writePartialFirst(outputStream);
53 writePartialLast(outputStream);
58 assertEquals("mismatch in records processed", records * 2, jsonRpcDecoder.getRecordsRead());
61 static int counter = 0;
64 create and a json of specified size
66 private void writeJson(OutputStream outputStream, int times) throws IOException {
67 outputStream.write("{".getBytes("UTF-8"));
68 for (int i = 0 ; i < times; i ++) {
70 String s = ",\"key1"+ counter +"\":\"planet of apes" + counter +
71 "\", \"key2"+ counter +"\":{\"k1\":\"ovs-db rocks the world\"}";
72 outputStream.write(s.substring(i == 0 ? 1 : 0).getBytes("UTF-8"));
73 System.out.println("data counter = " + counter);
75 outputStream.write("}".getBytes("UTF-8"));
79 writes a partial json and flush to simulate the case where netty gets half the message and
80 has to frame it accordingly.
82 private void writePartialFirst(OutputStream outputStream) throws IOException {
84 String s = " {\"part"+ counter+"\":";
85 outputStream.write(s.getBytes("UTF-8"));
86 System.out.println("partial first half counter = " + counter);
90 finishes the json started by writePartialFirst
92 private void writePartialLast(OutputStream outputStream) throws IOException {
93 String s = "\"val"+ counter+"\"}";
94 outputStream.write(s.getBytes("UTF-8"));
95 System.out.println("partial second half counter = " + counter);