5647a26cb3d2413d8d2c39b9a2d987662483c21e
[ovsdb.git] / ovsdb / src / test / java / org / opendaylight / ovsdb / lib / jsonrpc / TestClient.java
1 /*
2  * Copyright (C) 2013 EBay Software Foundation
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 : Aswin Raveendran
9  */
10 package org.opendaylight.ovsdb.lib.jsonrpc;
11
12 import io.netty.handler.logging.LogLevel;
13 import io.netty.handler.logging.LoggingHandler;
14 import junit.framework.TestCase;
15 import org.junit.Test;
16
17 import java.io.OutputStream;
18 import java.io.IOException;
19 import java.net.Socket;
20
21 public class TestClient extends TestCase {
22
23         String serverurl = "127.0.0.1";
24         int serverport = 8080;
25
26         NettyBootStrapper bootstrapper = new NettyBootStrapper();
27         JsonRpcDecoder jsonRpcDecoder = new JsonRpcDecoder(100000);
28
29         public void setupServer() throws Exception {
30             bootstrapper.startServer(serverport,
31                     jsonRpcDecoder,
32                     new LoggingHandler(LogLevel.DEBUG));
33         }
34
35         public void shutDownServer() throws InterruptedException {
36             bootstrapper.stopServer();
37         }
38
39         @Test
40         public void testBasicFlow() throws Exception {
41             setupServer();
42             Socket socket = socket = new Socket(serverurl, serverport);
43
44             OutputStream outputStream = socket.getOutputStream();
45
46             int records = 20;
47
48             for (int i = 0; i < records; i++) {
49                 writeJson(outputStream, 1);
50                 writePartialFirst(outputStream);
51                 outputStream.flush();
52                 Thread.sleep(10);
53                 writePartialLast(outputStream);
54             }
55             socket.close();
56             shutDownServer();
57
58             assertEquals("mismatch in records processed", records * 2, jsonRpcDecoder.getRecordsRead());
59         }
60
61     static int counter = 0;
62
63     /*
64        create and a json of specified size
65      */
66     private void writeJson(OutputStream outputStream, int times) throws IOException {
67         outputStream.write("{".getBytes("UTF-8"));
68         for (int i = 0 ; i < times; i ++) {
69             counter++;
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);
74         }
75         outputStream.write("}".getBytes("UTF-8"));
76     }
77
78     /*
79       writes a partial json and flush to simulate the case where netty gets half the message and
80       has to frame it accordingly.
81      */
82     private void writePartialFirst(OutputStream outputStream) throws IOException {
83         counter++;
84         String s  = "                       {\"part"+ counter+"\":";
85         outputStream.write(s.getBytes("UTF-8"));
86         System.out.println("partial first half counter = " + counter);
87     }
88
89     /*
90       finishes the json started by writePartialFirst
91      */
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);
96     }
97
98 }
99