643df7197668c27694329ac05e7f15f39cb3d2c1
[ovsdb.git] / library / impl / 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 package org.opendaylight.ovsdb.lib.jsonrpc;
9
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;
17
18 public class TestClient extends TestCase {
19
20     String serverurl = "127.0.0.1";
21     NettyBootStrapper bootstrapper = new NettyBootStrapper();
22     JsonRpcDecoder jsonRpcDecoder = new JsonRpcDecoder(100000);
23
24     public void setupServer() throws Exception {
25         bootstrapper.startServer(0, jsonRpcDecoder, new LoggingHandler(LogLevel.DEBUG));
26     }
27
28     public void shutDownServer() throws InterruptedException {
29         bootstrapper.stopServer();
30     }
31
32     /**
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.
35      */
36     @Test
37     public void testBasicFlow() throws Exception {
38         setupServer();
39         Socket socket = new Socket(serverurl, bootstrapper.getServerPort());
40         OutputStream outputStream = socket.getOutputStream();
41
42         int records = 20;
43
44         for (int i = 0; i < records; i++) {
45             writeJson(outputStream, 1);
46             writePartialFirst(outputStream);
47             outputStream.flush();
48             Thread.sleep(10);
49             writePartialLast(outputStream);
50         }
51         socket.close();
52         shutDownServer();
53
54         assertEquals("mismatch in records processed", records * 2, jsonRpcDecoder.getRecordsRead());
55     }
56
57     static int counter = 0;
58
59     /**
60      * Create and write a json string for specified number of times.
61      */
62     private void writeJson(OutputStream outputStream, int times) throws IOException {
63         outputStream.write("{".getBytes("UTF-8"));
64         for (int i = 0; i < times; i++) {
65             counter++;
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"));
69         }
70         outputStream.write("}".getBytes("UTF-8"));
71     }
72
73     /**
74      * Writes a partial JSON and flush to simulate the case where netty gets half the message and has to frame it
75      * accordingly.
76      */
77     private void writePartialFirst(OutputStream outputStream) throws IOException {
78         counter++;
79         String string = "                       {\"part" + counter + "\":";
80         outputStream.write(string.getBytes("UTF-8"));
81     }
82
83     /**
84      * Finishes the JSON started by writePartialFirst.
85      */
86     private void writePartialLast(OutputStream outputStream) throws IOException {
87         String string = "\"val" + counter + "\"}";
88         outputStream.write(string.getBytes("UTF-8"));
89     }
90
91 }