package org.bench4q.master.unitTest.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.JAXBException;

import static org.junit.Assert.*;

import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.master.OperateScriptServerResponseModel;
import org.bench4q.share.models.master.ScriptModel;
import org.junit.Before;
import org.junit.Test;

public class RecordScriptControllerTest extends TestBase {
	private static final int RECORD_TIME = 60000;
	private final String SCRIPT_URL = TestBase.BASE_URL + "/RecordScript";

	@Before
	public void prepare() {
		this.setAccessTocken(this.login());
	}

	public OperateScriptServerResponseModel startRecord() throws IOException,
			JAXBException {
		HttpResponse httpResponse = this.httpRequester.sendPost(this.SCRIPT_URL
				+ "/startScriptRecordServer", null,
				this.makeAccessTockenMap(this.getAccessTocken()));
		System.out.println(httpResponse.getContent());
		OperateScriptServerResponseModel ret = (OperateScriptServerResponseModel) MarshalHelper
				.unmarshal(OperateScriptServerResponseModel.class,
						httpResponse.getContent());
		assertTrue(ret.isSuccess());
		return ret;
	}

	public void stopRecord(OperateScriptServerResponseModel model)
			throws IOException, JAXBException {
		Map<String, String> paramsMap = buildParams("port",
				String.valueOf(model.getPort()));
		paramsMap.put("fileNameUUID", model.getFileName());
		HttpResponse httpResponse = this.httpRequester.sendPost(this.SCRIPT_URL
				+ "/stopScriptRecordServer", paramsMap,
				this.makeAccessTockenMap(this.accessTocken));
		OperateScriptServerResponseModel ret = (OperateScriptServerResponseModel) MarshalHelper
				.unmarshal(OperateScriptServerResponseModel.class,
						httpResponse.getContent());
		assertTrue(ret.isSuccess());
	}

	private Map<String, String> buildParams(String key, String value) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(key, value);
		return params;
	}

	public ScriptModel saveScriptToDB(OperateScriptServerResponseModel model,
			String scriptName) throws IOException, JAXBException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("scriptName", scriptName);
		params.put("port", String.valueOf(model.getPort()));
		params.put("fileNameUUID", model.getFileName());
		HttpResponse httpResponse = this.httpRequester.sendPost(this.SCRIPT_URL
				+ "/saveScriptToDB", params,
				this.makeAccessTockenMap(this.getAccessTocken()));
		OperateScriptServerResponseModel ret = (OperateScriptServerResponseModel) MarshalHelper
				.unmarshal(OperateScriptServerResponseModel.class,
						httpResponse.getContent());
		assertTrue(ret.isSuccess());
		assertNotNull(ret.getScriptModels());
		assertNotNull(ret.getScriptModels().get(0));
		return ret.getScriptModels().get(0);
	}

	@Test
	public void testRecordScript() throws IOException, JAXBException,
			InterruptedException {
		RecordScriptControllerTest test = new RecordScriptControllerTest();
		test.accessTocken = test.login();
		Thread.sleep(1000);
		OperateScriptServerResponseModel model = test.startRecord();
		Thread.sleep(RECORD_TIME);
		System.out.println("Thread has waken up");
		test.stopRecord(model);
		ScriptModel scriptModel = test.saveScriptToDB(model, "chen2");
		assertNotNull(scriptModel.getScriptContent());
		RunScenarioModel runScenarioModel = (RunScenarioModel) MarshalHelper
				.unmarshal(RunScenarioModel.class,
						scriptModel.getScriptContent());
		assertNotNull(runScenarioModel);
	}
}
