package org.bench4q.master.unitTest.entity;

import static org.junit.Assert.*;

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

import org.bench4q.master.domain.entity.Agent;
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.unitTest.TestBase_MakeUpTestPlan;
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_RunningScript extends TestBase_MakeUpTestPlan {
	private List<Agent> testcase2 = new ArrayList<Agent>();
	{
		testcase2.add(Agent.createAgentWithoutId("127.0.0.1", 6565, 500));
		testcase2.add(Agent.createAgentWithoutId("127.0.0.2", 6565, 500));
	}

	@Before
	public void before() {
		this.getHaPool().checkAllHeartBeat();
		this.submitATestPlanWithOneScript();
		for (Agent agent : this.testcase2) {
			this.getAgentService().addAgentToPool(agent);
		}
	}

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

	@Test
	public void test_distributeScriptAndLoad() {
		TestPlan testPlanInDomain = this.getTestPlanRepository()
				.getTestPlanInDomainBy(getTestPlanRunIdUuid());
		TestPlanScript testPlanScript = testPlanInDomain
				.extracSpecifiedScript(getUserFirstScript(this
						.getUserRepository().getUser("admin")));
		testPlanScript.applyForLoad();
		for (RunningAgentDB runningAgent : testPlanScript.getRunningAgentsDB()) {
			assertNotNull(runningAgent.getAgentMessenger());
			assertNotNull(runningAgent.getAgentRunId());
		}
		assertTrue(testPlanScript.distributeScriptAndParams());
	}

	@Test
	public void test_run() {
		TestPlan testPlanInDomain = this.getTestPlanRepository()
				.getTestPlanInDomainBy(getTestPlanRunIdUuid());
		TestPlanScript testPlanScript = testPlanInDomain
				.extracSpecifiedScript(getUserFirstScript(this
						.getUserRepository().getUser("admin")));
		assertTrue(testPlanScript.applyForLoad());
		assertTrue(testPlanScript.distributeScriptAndParams());
		assertTrue(testPlanScript.run());
		System.out.println("ok?");
	}

	@Test
	public void test_doAfterApplyLoad() {
		
	}
}
