Use String.getBytes(CharSet)
[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 java.nio.charset.StandardCharsets;
16 import junit.framework.TestCase;
17 import org.junit.Test;
18
19 public class TestClient extends TestCase {
20
21     String serverurl = "127.0.0.1";
22     NettyBootStrapper bootstrapper = new NettyBootStrapper();
23     JsonRpcDecoder jsonRpcDecoder = new JsonRpcDecoder(100000);
24
25     public void setupServer() throws Exception {
26         bootstrapper.startServer(0, jsonRpcDecoder, new LoggingHandler(LogLevel.DEBUG));
27     }
28
29     public void shutDownServer() throws InterruptedException {
30         bootstrapper.stopServer();
31     }
32
33     /**
34      * Testing appropriate ChannelHandler integration for JsonRpcDecoder, so that JSON strings written using an
35      * OutputStream connected to a ServerSocket of a Netty ServerBootstrap can be decoded properly.
36      */
37     @Test
38     public void testBasicFlow() throws Exception {
39         setupServer();
40         Socket socket = new Socket(serverurl, bootstrapper.getServerPort());
41         OutputStream outputStream = socket.getOutputStream();
42
43         int records = 20;
44
45         for (int i = 0; i < records; i++) {
46             writeJson(outputStream, 1);
47             writePartialFirst(outputStream);
48             outputStream.flush();
49             Thread.sleep(10);
50             writePartialLast(outputStream);
51         }
52         socket.close();
53         shutDownServer();
54
55         assertEquals("mismatch in records processed", records * 2, jsonRpcDecoder.getRecordsRead());
56     }
57
58     static int counter = 0;
59
60     /**
61      * Create and write a json string for specified number of times.
62      */
63     private static void writeJson(final OutputStream outputStream, final int times) throws IOException {
64         outputStream.write("{".getBytes(StandardCharsets.UTF_8));
65         for (int i = 0; i < times; i++) {
66             counter++;
67             String string = ",\"key1" + counter + "\":\"planet of apes" + counter + "\", \"key2" + counter
68                     + "\":{\"k1\":\"ovs-db rocks the world\"}";
69             outputStream.write(string.substring(i == 0 ? 1 : 0).getBytes(StandardCharsets.UTF_8));
70         }
71         outputStream.write("}".getBytes(StandardCharsets.UTF_8));
72     }
73
74     /**
75      * Writes a partial JSON and flush to simulate the case where netty gets half the message and has to frame it
76      * accordingly.
77      */
78     private static void writePartialFirst(final OutputStream outputStream) throws IOException {
79         counter++;
80         String string = "                       {\"part" + counter + "\":";
81         outputStream.write(string.getBytes(StandardCharsets.UTF_8));
82     }
83
84     /**
85      * Finishes the JSON started by writePartialFirst.
86      */
87     private static void writePartialLast(final OutputStream outputStream) throws IOException {
88         String string = "\"val" + counter + "\"}";
89         outputStream.write(string.getBytes(StandardCharsets.UTF_8));
90     }
91
92 }