package org.bench4q.master.integrated;

import static org.junit.Assert.*;

import java.util.Set;

import org.bench4q.master.domain.entity.RunningAgentDB;
import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.domain.factory.TestPlanFactory;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
import org.bench4q.share.enums.master.TestPlanStatus;
import org.junit.After;
import org.junit.Before;
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 = { "classpath:service-test-context.xml" })
public class Test_TestPlan extends TestBase_MakeUpTestPlan {
	private TestPlanFactory testPlanFactory;

	private TestPlanFactory getTestPlanFactory() {
		return testPlanFactory;
	}

	@Autowired
	private void setTestPlanFactory(TestPlanFactory testPlanFactory) {
		this.testPlanFactory = testPlanFactory;
	}

	@Before
	public void prepare() {
		prepareForTestPlanRunning();
		submitATestPlanWithOneScript();
	}

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

	@Test
	public void testRun() throws InterruptedException {
		assertTrue(this.getHaPool().getCurrentAvailableLoad() >= 500);
		TestPlan testPlanFromRepo = this.getTestPlanRepository()
				.getTestPlanInDomainBy(getTestPlanRunIdUuid());
		TestPlan testPlanInDomain = this.getTestPlanFactory().convertToDomain(
				testPlanFromRepo);
		testPlanInDomain.run();
		this.getTestPlanRepository().attachRunningTestPlan(testPlanInDomain);
		assertEquals(TestPlanStatus.InRunning.name(),
				testPlanInDomain.getCurrentStatus());

		assertTrue(this.getTestPlanRepository().updateEntity(testPlanInDomain));
		TestPlan testPlanAfterRun = this.getTestPlanRepository()
				.getTestPlanInDomainBy(getTestPlanRunIdUuid());
		testPlanAfterRun = this.getTestPlanFactory().convertToDomain(
				testPlanAfterRun);
		Set<TestPlanScript> testPlanScripts = testPlanAfterRun
				.getTestPlanScripts();
		assertNotNull(testPlanScripts);
		assertEquals(1, testPlanScripts.size());
		TestPlanScript testPlanScript = testPlanAfterRun
				.extracSpecifiedScript(getScriptId());
		assertNotNull(testPlanScript);
		assertEquals(EACH_SCRIPT_LOAD_SMALLSCALE,
				testPlanScript.getRequireLoad());
		assertNotNull(testPlanScript.getRunningAgentsDB());
		assertEquals(1, testPlanScript.getRunningAgentsDB().size());
		RunningAgentDB runningAgentDB = extractTheFirstRunningAgent(testPlanScript);
		assertNotNull(runningAgentDB);
		assertEquals(EACH_SCRIPT_LOAD_SMALLSCALE, runningAgentDB.getLoadInUse());
		Thread.sleep(10000);
		while (!TestPlanStatus.valueOf(
				this.getTestPlanRepository()
						.getTestPlanInDomainBy(getTestPlanRunIdUuid())
						.getCurrentStatus()).equals(TestPlanStatus.Complete)) {
			System.out.println("sleep for not complete");
			Thread.sleep(10000);
		}
	}
}
