package org.bench4q.master.integrated;

import static org.junit.Assert.*;

import javax.xml.bind.JAXBException;

import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.domain.valueobject.datastatistics.RunningScriptSampler;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
import org.bench4q.share.models.master.statistics.ScriptResultModel;
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:service-test-context.xml" })
public class Test_RunningScriptSampler extends TestBase_MakeUpTestPlan {
	private RunningScriptSampler runningScriptSampler;

	private RunningScriptSampler getRunningScriptSampler() {
		return runningScriptSampler;
	}

	private void setRunningScriptSampler(
			RunningScriptSampler runningScriptSampler) {
		this.runningScriptSampler = runningScriptSampler;
	}

	@Before
	public void prepare() {
		prepareForTestPlanRunning();
		this.submitATestPlanWithOneScript();
		TestPlan testPlan = fetchTestPlan();
		testPlan.run();
		this.getTestPlanRepository().updateEntity(testPlan);
		TestPlan testPlanInRunning = fetchTestPlan();
		TestPlanScript testPlanScript = testPlanInRunning
				.extracSpecifiedScript(getScriptId());
		this.setRunningScriptSampler(new RunningScriptSampler(testPlanScript
				.getRunningAgentsDB()));
	}

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

	@Test
	public void testGetScriptResult() throws InterruptedException,
			JAXBException {
		ScriptResultModel scriptResultModel = this.getRunningScriptSampler()
				.getResultModelFromAgent();
		Thread.sleep(1000);
		assertNotNull(scriptResultModel);
		assertNotNull(scriptResultModel.getScriptBriefResultModel());
		assertTrue(scriptResultModel.getScriptBehaviorsBriefModel()
				.getBehaviorBriefModels().size() > 0);
		assertTrue(scriptResultModel.getScriptPagesBriefModel()
				.getScriptPageBriefModels().size() > 0);
	}
}
