package org.bench4q.recorder;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.zip.GZIPInputStream;

import org.apache.commons.io.FileUtils;
import org.bench4q.recorder.httpcapture.Bench4qTestScriptAdapter;
import org.bench4q.recorder.httpcapture.HeaderValue;
import org.bench4q.recorder.httpcapture.HttpRequestHeader;
import org.bench4q.recorder.httpcapture.Param;
import org.bench4q.recorder.httpcapture.ResponseModel;
import org.bench4q.recorder.httpcapture.Utils.UserException;
import org.bench4q.recorder.httpcapture.generator.Bench4qCodeGenerator;
import org.bench4q.recorder.httpcapture.generator.ContentDecoder;
import org.bench4q.recorder.httpcapture.generator.IScriptGenerator;
import org.bench4q.recorder.httpcapture.generator.ResponseParser;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.scriptrecord.BehaviorModel;
import org.junit.Test;

public class Test_Bench4QCodeGenerator extends TestRecordBase {
	private Bench4qTestScriptAdapter scriptAdapter = new Bench4qTestScriptAdapter(
			new RunScenarioModel());
	private Bench4qCodeGenerator codeGenerator = new Bench4qCodeGenerator(
			this.scriptAdapter);
	public static String dealWithSerie = "RecordScriptTestCase/gzip/gzipBaiduIR";

//	@Test
//	public void testProcessResponseWithHtmlContentTypeWithNullResponseBody()
//			throws FileNotFoundException, IOException, Exception {
//		IScriptGenerator scriptGenerator = this.codeGenerator;
//		scriptGenerator.processResponse(
//				makeAHeader("www.makeup.com"),
//				new byte[0],
//				new ResponseModel(FileUtils.readFileToByteArray(new File(dealWithSerie
//						+ "NullResponseBody.html"))));
//		assertEquals(0, this.scriptAdapter.getChildrenUrls().size());
//	}
//
//	@Test
//	public void testProcessResponseOnceWithHtmlResponseButHaveNoBody()
//			throws Exception, IOException, FileNotFoundException {
//		IScriptGenerator scriptGenerator = this.codeGenerator;
//		scriptGenerator.processResponse(
//				makeAHeader("www.makeup.com"),
//				new byte[0],
//				new ResponseModel(FileUtils.readFileToByteArray(new File(dealWithSerie
//						+ "BlankBody.html"))));
//		assertEquals(0, this.scriptAdapter.getChildrenUrls().size());
//	}
//
//	@Test
//	public void testProcessResponseWithOneHtmlResponseHaveBody()
//			throws Exception {
//		HttpRequestHeader header = makeAHeader("ir.baidu.com");
//		IScriptGenerator scriptGenerator = this.codeGenerator;
//		scriptGenerator.processResponse(
//				header,
//				new byte[0],
//				new ResponseModel(FileUtils.readFileToByteArray(new File(dealWithSerie
//						+ "Response.txt"))));
//		assertEquals(11, this.scriptAdapter.getChildrenUrls().size());
//	}
//
//	private HttpRequestHeader makeAHeader(String url) throws IOException,
//			FileNotFoundException {
//		HttpRequestHeader header = new HttpRequestHeader(new FileInputStream(
//				new File(dealWithSerie + "Request.txt")));
//		header.url = url;
//		return header;
//	}
//
//	@Test
//	public void testProcessResponseTwiceWithTwoHtmlResponseButTheSecondHaveNoBody()
//			throws Exception {
//		// testProcessResponseWithOneHtmlResponseHaveBody();
//		// testProcessResponseOnceWithHtmlResponseButHaveNoBody();
//		// assertEquals(0, this.scriptAdapter.getChildrenUrls().size());
//	}
//
//	@Test
//	public void testProcessResponseTwiceWithTwoHtmlResponseOneHtmlSecondNot()
//			throws Exception {
//		testProcessResponseWithOneHtmlResponseHaveBody();
//		testProcessResponseWithNotHtmlResponse();
//		assertEquals(11, this.scriptAdapter.getChildrenUrls().size());
//	}
//
//	@Test
//	public void testProcessResponseWithTwoHtmlResponseFirstNoBodySecondHave()
//			throws FileNotFoundException, IOException, Exception {
//		testProcessResponseOnceWithHtmlResponseButHaveNoBody();
//		testProcessResponseWithOneHtmlResponseHaveBody();
//
//	}
//
//	private void testProcessResponseWithNotHtmlResponse() throws Exception,
//			IOException, FileNotFoundException {
//		IScriptGenerator scriptGenerator = this.codeGenerator;
//		scriptGenerator.processResponse(
//				makeAHeader("www.makeup2.com"),
//				new byte[0],
//				new ResponseModel(FileUtils.readFileToByteArray(new File(dealWithSerie
//						+ "NotHtmlResponse.html"))));
//	}
//
//	@Test
//	public void testParseContentLength() throws IOException {
//		int contentLength = new ResponseParser(
//				FileUtils.readFileToByteArray(new File(dealWithSerie
//						+ "Response.txt"))).getResponseHeader()
//				.getContentLength();
//		assertTrue(contentLength == 5023);
//	}
//
//	@Test
//	public void testGetContentBody() throws IOException {
//		String contentFromFile = FileUtils.readFileToString(new File(
//				dealWithSerie + "ResponseBody.txt"), "ISO-8859-1");
//		byte[] contentBody1 = contentFromFile.getBytes("ISO-8859-1");
//		String responseString = new String(contentBody1,
//				Charset.forName("ISO-8859-1"));
//		System.out.println("testGetContentBody total length:"
//				+ responseString.getBytes("ISO-8859-1").length);
//		System.out.println("testGetContentBody :" + contentBody1.length);
//		GZIPInputStream inputStream = new GZIPInputStream(
//				new ByteArrayInputStream(contentBody1));
//		assertEquals(responseString.getBytes("ISO-8859-1").length,
//				contentBody1.length);
//		assertTrue(inputStream != null);
//	}
//
//	@Test
//	public void testUncompressGzipContent() throws IOException {
//		ContentDecoder contentDecoder = ContentDecoder.createDecoder("gzip");
//		FileInputStream inputStream = new FileInputStream(new File(
//				dealWithSerie + "ResponseBody.txt"));
//		String contentString = new String(
//				contentDecoder.decodeContent(inputStream),
//				Charset.forName("utf-8"));
//		System.out.println(contentString);
//		assertTrue(contentString.indexOf("<html") > -1);
//	}
//
//	@Test
//	public void testFileInputStreamAndFileUtils() throws IOException {
//		byte[] responseBodyBytes = FileUtils.readFileToString(
//				new File(dealWithSerie + "ResponseBody.txt"), "UTF-8")
//				.getBytes("UTF-8");
//		int length1 = responseBodyBytes.length;
//		String responseBodyString = new String(responseBodyBytes, "UTF-8");
//		int length2 = responseBodyString.getBytes("UTF-8").length;
//		assertEquals(length1, length2);
//		System.out.println(length1);
//	}
//
//	@Test
//	public void testCreateGetBehavior() throws UserException {
//		this.codeGenerator.doQueryStringParameterList(new Param[] { Param
//				.createParam("a", "b") });
//		this.codeGenerator.doHeaders(new HeaderValue[] { new HeaderValue(
//				"token", "Bearer") });
//		this.codeGenerator.doBodyParameterList(new Param[] { Param.createParam(
//				"a", "b") });
//		BehaviorModel behaviorModel = this.codeGenerator.createHttpBehavior(1,
//				"http://www.baidu.com", "Get");
//		assertEquals("a=b", behaviorModel.getParameters().get(1).getValue());
//		assertEquals("header=token|value=Bearer|;", behaviorModel
//				.getParameters().get(2).getValue());
//		assertEquals(3, behaviorModel.getParameters().size());
//	}
//
//	public void testCreatePostBehavior() throws UserException {
//		this.codeGenerator.doQueryStringParameterList(new Param[] { Param
//				.createParam("a", "b") });
//		this.codeGenerator.doHeaders(new HeaderValue[] { new HeaderValue(
//				"token", "Bearer") });
//		this.codeGenerator.doBodyParameterList(new Param[] { Param.createParam(
//				"a", "b") });
//		this.codeGenerator.doSetData("ava");
//		BehaviorModel behaviorModel = this.codeGenerator.createHttpBehavior(1,
//				"http://www.baidu.com", "Post");
//		assertEquals("a=b", behaviorModel.getParameters().get(1).getValue());
//		assertEquals("header=token|value=Bearer|;", behaviorModel
//				.getParameters().get(2).getValue());
//		assertEquals(5, behaviorModel.getParameters().size());
//		assertEquals("ava", behaviorModel.getParameters().get(3).getValue());
//		assertEquals("a=b", behaviorModel.getParameters().get(4).getValue());
//	}
}
