package org.bench4q.master.unitTest.service;

import static org.junit.Assert.*;

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

import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.domain.entity.TestPlanScriptResult;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
import org.bench4q.share.models.master.statistics.ScriptBriefResultModel;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:mockHttpMesseger-context.xml" })
public class Test_TestPlanScriptService extends TestBase_MakeUpTestPlan {
	private static final int WRONG_SCRIPT_ID = 99999;
	private static final String WRONG_TEST_PLAN_RUN_ID = "0cdca8da-5b61-4bff-be48-8075e5d2fa64";

	@Before
	public void prepare() {
		submitATestPlanWithOneScript();
		System.out.println(getScriptId());
	}

	@After
	public void cleanUp() {
		deleteTestPlan();
	}

	@Test
	public void testLoadScriptResultsWithWrongScriptIdOrRunId() {
		TestPlanScript testPlanScript = this.getTestPlanScriptService()
				.getTestPlanScript(WRONG_SCRIPT_ID,
						UUID.fromString(WRONG_TEST_PLAN_RUN_ID));
		List<TestPlanScriptResult> scriptBriefResultModels = this
				.getTestPlanScriptService().queryScriptBriefResults(
						testPlanScript);
		assertTrue(scriptBriefResultModels.size() == 0);
	}

	@Test
	public void testSaveScriptBriefResultWithWrongScriptIdOrRunId() {
		ScriptBriefResultModel briefResultModel = new ScriptBriefResultModel();
		briefResultModel.setAverageElapsedTime(100);
		briefResultModel.setAverageResponseTime(100);
		briefResultModel.setFailRateThisTime(0);
		briefResultModel.setFailThroughputThisTime(0);
		briefResultModel.setFinished(true);
		/*
		 * this.getTestScriptResultSave().doSaveResult(
		 * UUID.fromString(WRONG_TEST_PLAN_RUN_ID), WRONG_SCRIPT_ID,
		 * briefResultModel, new Date());
		 */
		TestPlanScript testPlanScript = this.getTestPlanScriptService()
				.getTestPlanScript(WRONG_SCRIPT_ID,
						UUID.fromString(WRONG_TEST_PLAN_RUN_ID));
		assertTrue(this.getTestPlanScriptService()
				.queryScriptBriefResults(testPlanScript).size() == 0);
	}

	@Test
	public void testGetTestPlanScript() {
		System.out.println(getScriptId());
		TestPlanScript testPlanScript = this.getTestPlanScriptService()
				.getTestPlanScript(getScriptId(), getTestPlanRunIdUuid());
		assertNotNull(testPlanScript);
		assertEquals(this.getScriptId(), testPlanScript.getScript().getId());
	}

	@Test
	public void testQueryTestPlanScripts() {
		assertEquals(
				1,
				this.getTestPlanScriptService()
						.queryTestPlanScripts(getTestPlanRunIdUuid()).size());
	}
}
