package org.bench4q.web.test.masterMessager;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.containing;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.OperateScriptServerResponseModel;
import org.bench4q.share.models.master.ScriptModel;
import org.bench4q.web.masterMessager.ScriptMessager;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/test/resources/bench4qweb-servlet.xml" })
public class ScriptMessagerTest extends MessagerTestBase {
	private ScriptMessager scriptMessager;
	private String port = "8090";
	private String baseUrl = "/script";

	public ScriptMessager getScriptMessager() {
		return scriptMessager;
	}

	@Autowired
	public void setScriptMessager(ScriptMessager scriptMessager) {
		this.scriptMessager = scriptMessager;
	}

	@BeforeClass
	public static void setUp() {
		startServer();
	}

	@AfterClass
	public static void clear() {
		stopServer();
	}

	@Test
	public void test_StartScriptRecordPort() {

		String url = this.baseUrl + "/startScriptRecordServer";
		this.getWireMock().register(
				post(urlEqualTo(url)).willReturn(
						aResponse().withStatus(200)
								.withHeader("Content-Type", "text/xml")
								.withBody(this.createResponse())));
		assertNotNull(this.getScriptMessager().startScriptRecordServer(null));
		assertTrue(this.getScriptMessager().startScriptRecordServer(null)
				.isSuccess());

	}

	@Test
	public void test_stopScriptRecordServer() {
		String url = this.baseUrl + "/stopScriptRecordServer";
		this.getWireMock().register(
				post(urlEqualTo(url))
						.withRequestBody(containing("port"))
						.withRequestBody(containing("fileNameUUID"))
						.willReturn(
								aResponse().withStatus(200)
										.withHeader("Content-Type", "text/xml")
										.withBody(this.createResponse())));

		assertNotNull(this.getScriptMessager().stopScriptRecordServer(null,
				port, ""));
		assertTrue(this.getScriptMessager()
				.stopScriptRecordServer(null, port, "").isSuccess());
	}

	@Test
	public void test_saveScriptRecorded() {
		String url = this.baseUrl + "/saveScriptToDB";
		this.getWireMock().register(
				post(urlEqualTo(url))
						.withRequestBody(containing("scriptName"))
						.withRequestBody(containing("port"))
						.withRequestBody(containing("fileNameUUID"))
						.willReturn(
								aResponse().withStatus(200)
										.withHeader("Content-Type", "text/xml")
										.withBody(this.createResponse())));
		assertNotNull(this.getScriptMessager().saveScriptRecorded(null, "",
				port, ""));
		assertTrue(this.getScriptMessager()
				.saveScriptRecorded(null, "", port, "").isSuccess());
	}

	@Test
	public void test_uploadScript() {
		String scriptName = "scriptName";
		String url = this.baseUrl + "/uploadScript/" + scriptName;
		this.getWireMock().register(
				post(urlEqualTo(url)).willReturn(
						aResponse().withStatus(200)
								.withHeader("Content-Type", "text/xml")
								.withBody(this.createResponse())));
		assertNotNull(this.getScriptMessager().uploadScript(null, scriptName,
				"", null));
		assertTrue(this.getScriptMessager()
				.uploadScript(null, scriptName, "", null).isSuccess());
	}

	@Test
	public void test_deleteScript() {
		String url = this.baseUrl + "/deleteScript";
		this.getWireMock().register(
				post(urlEqualTo(url)).withRequestBody(containing("scriptId"))
						.willReturn(
								aResponse().withStatus(200)
										.withHeader("Content-Type", "text/xml")
										.withBody(this.createResponse())));
		assertNotNull(this.getScriptMessager().deleteScript(null, ""));
		assertTrue(this.getScriptMessager().deleteScript(null, "").isSuccess());
	}

	@Test
	public void test_updateScript() {
		String scriptId = UUID.randomUUID().toString();
		String url = this.baseUrl + "/updateScript/" + scriptId;
		this.getWireMock().register(
				post(urlEqualTo(url)).withRequestBody(containing("content"))
						.willReturn(
								aResponse().withStatus(200)
										.withHeader("Content-Type", "text/xml")
										.withBody(this.createResponse())));
		assertNotNull(this.getScriptMessager().updateScript(null, scriptId, "",null));
		assertTrue(this.getScriptMessager().updateScript(null, scriptId, "",null)
				.isSuccess());
	}

	@Test
	public void test_loadScript() {
		String url = this.baseUrl + "/loadScriptList";
		this.getWireMock().register(
				post(urlEqualTo(url)).willReturn(
						aResponse().withStatus(200)
								.withHeader("Content-Type", "text/xml")
								.withBody(this.createResponse())));
		assertNotNull(this.getScriptMessager().loadScripts(null));
		assertTrue(this.getScriptMessager().loadScripts(null).isSuccess());
	}

	@Test
	public void test_queryScriptById() {
		String url = this.baseUrl + "/queryScriptById";
		this.getWireMock().register(
				post(urlEqualTo(url)).withRequestBody(containing("scriptId"))
						.willReturn(
								aResponse().withStatus(200)
										.withHeader("Content-Type", "text/xml")
										.withBody(this.createResponse())));
		assertNotNull(this.getScriptMessager().queryScriptById(null, ""));
		assertTrue(this.getScriptMessager().queryScriptById(null, "")
				.isSuccess());
	}

	@Test
	public void test_queryScriptByName() {
		String url = this.baseUrl + "/queryScriptByName";
		this.getWireMock().register(
				post(urlEqualTo(url)).withRequestBody(containing("name"))
						.willReturn(
								aResponse().withStatus(200)
										.withHeader("Content-Type", "text/xml")
										.withBody(this.createResponse())));
		assertNotNull(this.getScriptMessager().queryScriptByName(null, ""));
		assertTrue(this.getScriptMessager().queryScriptByName(null, "")
				.isSuccess());
	}

	private String createResponse() {
		ScriptModel scriptModel=new ScriptModel();
		List<ScriptModel> list=new ArrayList<ScriptModel>();
		list.add(scriptModel);
		OperateScriptServerResponseModel operateScriptServerResponseModel = new OperateScriptServerResponseModel();
		operateScriptServerResponseModel.setSuccess(true);
		operateScriptServerResponseModel.setScriptModels(list);
		return MarshalHelper.tryMarshal(operateScriptServerResponseModel);

	}

}
