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.User;
import org.bench4q.master.domain.factory.TestPlanFactory;
import org.bench4q.master.domain.repository.TestPlanRepository;
import org.bench4q.master.domain.service.TestResultSave;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
import org.bench4q.share.models.master.TestPlanModel;
import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel;
import org.bench4q.share.models.master.statistics.ScriptBriefResultModel;
import org.bench4q.share.models.master.statistics.ScriptPagesBriefModel;
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_TestPlanScriptResultService extends TestBase_MakeUpTestPlan {
	@Autowired
	private TestResultSave testResultSave;

	@Autowired
	private TestPlanFactory testPlanFactory;

	@Autowired
	private TestPlanRepository testPlanRepository;

	@Test
	public void testLoadScriptBriefWithWrongTestPlanId() {
		List<ScriptBriefResultModel> results = this
				.getTestPlanScriptResultService()
				.loadScriptBriefWithDuation(
						UUID.fromString("fdfe0e55-c65f-4829-ae7c-b1d3a90f62d5"),
						this.getScriptId(), 0);
		ScriptPagesBriefModel scriptPagesBriefModel = this
				.getTestPlanScriptResultService()
				.getLatestScriptPagesBrief(
						UUID.fromString("fdfe0e55-c65f-4829-ae7c-b1d3a90f62d5"),
						this.getScriptId());
		ScriptBehaviorsBriefModel behavirosResults = this
				.getTestPlanScriptResultService()
				.getLatestScriptBehaviorsBrief(
						UUID.fromString("fdfe0e55-c65f-4829-ae7c-b1d3a90f62d5"),
						this.getScriptId());
		assertEquals(0, results.size());
		assertNotNull(scriptPagesBriefModel);
		assertEquals(0, scriptPagesBriefModel.getScriptPageBriefModels().size());
		assertNotNull(behavirosResults);
		assertNotNull(behavirosResults.isFinished());
	}

	@Before
	public void makeUpTestPlanAndTestPlanScriptAndTestPlanScriptResult() {
		UUID testPlanRunId = UUID.randomUUID();
		User user = this.getUserRepository().getUser("admin");
		this.setScriptId(this.getUserFirstScript(user));
		TestPlanModel model = createATestPlanWithOneScript(this.getScriptId());
		this.getTestPlanEngine().submitTestPlan(model, user, testPlanRunId);
		this.setTestPlanRunIdUuid(testPlanRunId);
		this.testPlanRepository.getTestPlanInDomainBy(getTestPlanRunIdUuid());

	}

	@After
	public void cleanUp() {
		this.getTestPlanService().removeTestPlanInDB(
				this.testPlanRepository.getTestPlanInDomainBy(
						this.getTestPlanRunIdUuid()).getId());
	}
}
