package debugging.jsonclientcaller;

import static debugging.jsonclientcaller.JsonClientCaller.streamToString;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import us.kbase.abstracthandle.AbstractHandleClient;
import us.kbase.auth.AuthService;
import us.kbase.auth.AuthToken;


public class JsonClientCallerGutsTest {
	private static final String URL = "http://dev03.berkeley.kbase.us:7109";
	private static final int SLEEP = 1000; //ms between requests
	

	public static void main(String [] args) throws Exception{
		System.out.println(URL);
		String user = args[0];
		String pwd = args[1];
		int count = Integer.parseInt(args[2]);
		int excepts = 0;
		for (int i = 0; i < count; i++) {
			System.out.print(i + "\n");
			try {
				HttpURLConnection conn =
						(HttpURLConnection) new URL(URL).openConnection();
				conn.setDoOutput(true);
				conn.setRequestMethod("POST");
				AuthToken accessToken = AuthService.login(
						user, pwd).getToken();
				conn.setRequestProperty("Authorization",
						accessToken.toString());
				final long[] sizeWrapper = new long[] {0};
				OutputStream os = new OutputStream() {
					@Override
					public void write(int b) {sizeWrapper[0]++;}
					@Override
					public void write(byte[] b) {sizeWrapper[0] += b.length;}
					@Override
					public void write(byte[] b, int o, int l) {sizeWrapper[0] += l;}
				};
				String method = "AbstractHandle.are_readable";
				Object arg = Arrays.asList(Arrays.asList("KBH_3"));
				String id = "12345";
				
				writeRequestData(method, arg, os, id);
				// Set content-length
				conn.setFixedLengthStreamingMode(sizeWrapper[0]);
				
				writeRequestData(method, arg, conn.getOutputStream(), id);
				System.out.print(conn.getResponseCode() + " ");
				System.out.println(conn.getResponseMessage());
				conn.getResponseMessage();
				InputStream istream;
				if (conn.getResponseCode() == 500) {
					istream = conn.getErrorStream();
				} else {
					istream = conn.getInputStream();
				}
				// Parse response into json
				System.out.println(streamToString(istream));
			} catch (Exception e) {
				excepts++;
				System.out.println(e.getClass().getName() + ": " +
						e.getLocalizedMessage());
			}
			Thread.sleep(SLEEP);
		}
		System.out.println(String.format("Sleep: %s, failures %s/%s",
				SLEEP, excepts, count));
	}

	public static void writeRequestData(String method, Object arg,
			OutputStream os, String id) 
			throws IOException {
		JsonGenerator g = new ObjectMapper().getFactory()
				.createGenerator(os, JsonEncoding.UTF8);
		g.writeStartObject();
		g.writeObjectField("params", arg);
		g.writeStringField("method", method);
		g.writeStringField("version", "1.1");
		g.writeStringField("id", id);
		g.writeEndObject();
		g.close();
		os.flush();
	}
}

@@ -34,83 +34,8 @@
 r;%0A%0A
-import static debugging.jsonclientcaller.JsonClientCaller.streamToString;%0A%0A
 impo
@@ -203,56 +203,8 @@
 st;%0A
-import java.util.Arrays;%0Aimport java.util.List;%0A
 impo
@@ -356,203 +356,29 @@
 son.
-core.type.TypeReference;%0Aimport com.fasterxml.jackson.databind.ObjectMapper;%0A%0Aimport us.kbase.abstracthandle.AbstractHandleClient;%0Aimport us.kbase.auth.AuthService;%0Aimport us.kbase.auth.AuthToken
+databind.ObjectMapper
 ;%0A%0A%0A
@@ -553,16 +553,54 @@
 equests%0A
+%09private static final int COUNT = 10;%0A
 %09%0A%0A%09publ
@@ -684,98 +684,8 @@
 L);%0A
-%09%09String user = args%5B0%5D;%0A%09%09String pwd = args%5B1%5D;%0A%09%09int count = Integer.parseInt(args%5B2%5D);%0A
 %09%09in
@@ -721,21 +721,21 @@
  0; i %3C 
-count
+COUNT
 ; i++) %7B
@@ -928,160 +928,8 @@
 %22);%0A
-%09%09%09%09AuthToken accessToken = AuthService.login(%0A%09%09%09%09%09%09user, pwd).getToken();%0A%09%09%09%09conn.setRequestProperty(%22Authorization%22,%0A%09%09%09%09%09%09accessToken.toString());%0A
 %09%09%09%09
@@ -1289,20 +1289,20 @@
 dle.
-are_readab
+list_hand
 le
+s
 %22;%0A%09
@@ -1320,45 +1320,31 @@
 rg =
+ new
  Array
-s.asList(Arrays.asList(%22KBH_3%22)
+List%3CObject%3E(
 );%0A%09
@@ -1674,157 +1674,20 @@
 %09%09%09%09
-conn.getResponseMessage();%0A%09%09%09%09InputStream istream;%0A%09%09%09%09if (conn.getResponseCode() == 500) %7B%0A%09%09%09%09%09istream = conn.getErrorStream();%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09
+InputStream 
 istr
@@ -1719,14 +1719,8 @@
 ();%0A
-%09%09%09%09%7D%0A
 %09%09%09%09
@@ -2046,13 +2046,13 @@
 ts, 
-count
+COUNT
 ));%0A
@@ -2497,10 +2497,178 @@
 h();%0A%09%7D%0A
+%09%0A%09public static String streamToString(InputStream os) %7B%0A%09%09Scanner s = new Scanner(os);%0A%09%09s.useDelimiter(%22%5C%5CA%22);%0A%09%09String ret = s.next();%0A%09%09s.close();%0A%09%09return ret;%0A%09%7D%0A
 %7D%0A

Minimize the tests