/*
 * Copyright (C) 2013 EBay Software Foundation
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.ovsdb.lib.jsonrpc;

import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import junit.framework.TestCase;
import org.junit.Test;

public class TestClient extends TestCase {

    String serverurl = "127.0.0.1";
    NettyBootStrapper bootstrapper = new NettyBootStrapper();
    JsonRpcDecoder jsonRpcDecoder = new JsonRpcDecoder(100000);

    public void setupServer() throws Exception {
        bootstrapper.startServer(0, jsonRpcDecoder, new LoggingHandler(LogLevel.DEBUG));
    }

    public void shutDownServer() throws InterruptedException {
        bootstrapper.stopServer();
    }

    /**
     * Testing appropriate ChannelHandler integration for JsonRpcDecoder, so that JSON strings written using an
     * OutputStream connected to a ServerSocket of a Netty ServerBootstrap can be decoded properly.
     */
    @Test
    public void testBasicFlow() throws Exception {
        setupServer();
        Socket socket = new Socket(serverurl, bootstrapper.getServerPort());
        OutputStream outputStream = socket.getOutputStream();

        int records = 20;

        for (int i = 0; i < records; i++) {
            writeJson(outputStream, 1);
            writePartialFirst(outputStream);
            outputStream.flush();
            Thread.sleep(10);
            writePartialLast(outputStream);
        }
        socket.close();
        shutDownServer();

        assertEquals("mismatch in records processed", records * 2, jsonRpcDecoder.getRecordsRead());
    }

    static int counter = 0;

    /**
     * Create and write a json string for specified number of times.
     */
    private void writeJson(OutputStream outputStream, int times) throws IOException {
        outputStream.write("{".getBytes("UTF-8"));
        for (int i = 0; i < times; i++) {
            counter++;
            String string = ",\"key1" + counter + "\":\"planet of apes" + counter + "\", \"key2" + counter
                    + "\":{\"k1\":\"ovs-db rocks the world\"}";
            outputStream.write(string.substring(i == 0 ? 1 : 0).getBytes("UTF-8"));
        }
        outputStream.write("}".getBytes("UTF-8"));
    }

    /**
     * Writes a partial JSON and flush to simulate the case where netty gets half the message and has to frame it
     * accordingly.
     */
    private void writePartialFirst(OutputStream outputStream) throws IOException {
        counter++;
        String string = "                       {\"part" + counter + "\":";
        outputStream.write(string.getBytes("UTF-8"));
    }

    /**
     * Finishes the JSON started by writePartialFirst.
     */
    private void writePartialLast(OutputStream outputStream) throws IOException {
        String string = "\"val" + counter + "\"}";
        outputStream.write(string.getBytes("UTF-8"));
    }

}

@@ -488,16 +488,58 @@
 Socket;%0A
+import java.nio.charset.StandardCharsets;%0A
 import j
@@ -2047,32 +2047,39 @@
  */%0A    private 
+static 
 void writeJson(O
@@ -2077,16 +2077,22 @@
 iteJson(
+final 
 OutputSt
@@ -2109,16 +2109,22 @@
 tStream,
+ final
  int tim
@@ -2180,39 +2180,54 @@
 te(%22%7B%22.getBytes(
-%22UTF-8%22
+StandardCharsets.UTF_8
 ));%0A        for 
@@ -2515,39 +2515,54 @@
 1 : 0).getBytes(
-%22UTF-8%22
+StandardCharsets.UTF_8
 ));%0A        %7D%0A  
@@ -2591,39 +2591,54 @@
 te(%22%7D%22.getBytes(
-%22UTF-8%22
+StandardCharsets.UTF_8
 ));%0A    %7D%0A%0A    /
@@ -2786,32 +2786,39 @@
  */%0A    private 
+static 
 void writePartia
@@ -2824,16 +2824,22 @@
 alFirst(
+final 
 OutputSt
@@ -3008,39 +3008,54 @@
 string.getBytes(
-%22UTF-8%22
+StandardCharsets.UTF_8
 ));%0A    %7D%0A%0A    /
@@ -3132,16 +3132,23 @@
 private 
+static 
 void wri
@@ -3161,16 +3161,22 @@
 ialLast(
+final 
 OutputSt
@@ -3313,15 +3313,30 @@
 tes(
-%22UTF-8%22
+StandardCharsets.UTF_8
 ));%0A

Use String.getBytes(CharSet)