package org.bench4q.web.test.masterMessager;

import static com.github.tomakehurst.wiremock.client.WireMock.*;

import static org.junit.Assert.*;

import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.TestPlanResponseModel;
import org.bench4q.share.models.master.TestPlanResultModel;
import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel;
import org.bench4q.web.masterMessager.TestPlanMessager;

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("file:src/test/resources/bench4qweb-servlet.xml")
public class TestPlanMessageTest extends MessagerTestBase {
	private TestPlanMessager testPlanMessager;
	private String baseUrl = "/testPlan";
	private String testPlanId = "testPlanId";
	private String scriptId = "scriptId";

	public TestPlanMessager getTestPlanMessager() {
		return testPlanMessager;
	}

	@Autowired
	public void setTestPlanMessager(TestPlanMessager testPlanMessager) {
		this.testPlanMessager = testPlanMessager;
	}

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

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

	@Test
	public void test_runTestPlan() {
		String url = baseUrl + "/run";
		this.getWireMock().register(
				post(urlEqualTo(url)).willReturn(
						aResponse().withStatus(200)
								.withHeader("Content-Type", "text/xml")
								.withBody(this.createResponse())));
		assertNotNull(this.testPlanMessager.runTestPlan(null, ""));
	}

	@Test
	public void test_getRunningTestInfo() {
		String url = baseUrl + "/getRunningInfo";
		this.getWireMock().register(
				post(urlEqualTo(url)).withRequestBody(containing("testPlanId"))
						.willReturn(
								aResponse().withStatus(200)
										.withHeader("Content-Type", "text/xml")
										.withBody(this.createResponse())));
		assertNotNull(this.testPlanMessager.getRunningTestInfo(null, ""));
	}

	// @Test
	// public void test_getScriptBriefResult() {
	// TestPlanScriptBriefResultModel testPlanScriptBriefResultModel = new
	// TestPlanScriptBriefResultModel();
	// String response = MarshalHelper
	// .tryMarshal(testPlanScriptBriefResultModel);
	// String url = baseUrl + '/' + testPlanId + '/' + scriptId + '/'
	// + duationBegin;
	// System.out.println(url);
	// this.getWireMock().register(
	// get(urlEqualTo(url)).willReturn(
	// aResponse().withStatus(200)
	// .withHeader("Content-Type", "text/xml")
	// .withBody(response)));
	// assertNotNull(this.testPlanMessager.getScriptBriefResult(null,
	// testPlanId, scriptId, duationBegin));
	// }

	@Test
	public void test_getScriptBehaviorsBriefResult() {
		ScriptBehaviorsBriefModel scriptBehaviorsBriefModel = new ScriptBehaviorsBriefModel();
		scriptBehaviorsBriefModel.setFinished(true);
		String response = MarshalHelper.tryMarshal(scriptBehaviorsBriefModel);
		String url = baseUrl +"/getBehaviorsBrief"+ '/' + testPlanId + '/' + scriptId;
		this.getWireMock().register(
				get(urlEqualTo(url)).willReturn(
						aResponse().withStatus(200)
								.withHeader("Content-Type", "text/xml")
								.withBody(response)));
		assertNotNull(this.testPlanMessager.getScriptBehaviorsBriefResult(null,
				testPlanId, scriptId));
		assertTrue(this.testPlanMessager.getScriptBehaviorsBriefResult(null,
				testPlanId, scriptId).isFinished());
	}

//	@Test
//	public void test_getPageBriefResult() {
//		ScriptPagesBriefModel scriptPagesBriefModel = new ScriptPagesBriefModel();
//		String response = MarshalHelper.tryMarshal(scriptPagesBriefModel);
//		String url = baseUrl + '/' + testPlanId + '/' + scriptId;
//		this.getWireMock().register(
//				get(urlEqualTo(url)).willReturn(
//						aResponse().withStatus(200)
//								.withHeader("Content-Type", "text/xml")
//								.withBody(response)));
//		assertNotNull(this.testPlanMessager.getPageBriefResult(null,
//				testPlanId, scriptId));
//	}

//	@Test
//	public void test_queryTestPlanById() {
//		String testPlanRunId = "testPlanRunId";
//		String url = baseUrl + "/queryTestPlan/" + testPlanRunId;
//		this.getWireMock().register(
//				get(urlEqualTo(url)).willReturn(
//						aResponse().withStatus(200)
//								.withHeader("Content-Type", "text/xml")
//								.withBody(this.createResponse())));
//		assertNotNull(this.testPlanMessager.queryTestPlanById(null,
//				testPlanRunId));
//	}

	@Test
	public void test_deleteTestPlan() {
		TestPlanResponseModel testPlanResponseModel = new TestPlanResponseModel();
		testPlanResponseModel.setSuccess(true);
		String response = MarshalHelper.tryMarshal(testPlanResponseModel);
		String url = baseUrl + "/removeTestPlanFromPool";
		this.getWireMock().register(
				post(urlEqualTo(url)).withRequestBody(containing("testPlanId"))
						.willReturn(
								aResponse().withStatus(200)
										.withHeader("Content-Type", "text/xml")
										.withBody(response)));
		assertNotNull(this.testPlanMessager.deleteTestPlan(null, testPlanId));
		assertTrue(this.testPlanMessager.deleteTestPlan(null, testPlanId)
				.isSuccess());
	}

	@Test
	public void test_loadTestPlans() {
		String url = baseUrl + "/loadTestPlans";
		TestPlanResponseModel testPlanResponseModel = new TestPlanResponseModel();
		testPlanResponseModel.setSuccess(true);
		String response = MarshalHelper.tryMarshal(testPlanResponseModel);
		this.getWireMock().register(
				post(urlEqualTo(url)).willReturn(
						aResponse().withStatus(200)
								.withHeader("Content-Type", "text/xml")
								.withBody(response)));
		assertNotNull(this.testPlanMessager.loadTestPlans(null));
		assertTrue(this.testPlanMessager.loadTestPlans(null).isSuccess());

	}

	@Test
	public void test_getTestPlanReport() {

	}

	public String createResponse() {
		TestPlanResultModel testPlanResultModel = new TestPlanResultModel();
		return MarshalHelper.tryMarshal(testPlanResultModel);
	}
}
