package org.bench4q.master.unitTest.controller;

import static org.junit.Assert.*;

import java.io.IOException;
import java.util.UUID;

import javax.xml.bind.JAXBException;

import org.bench4q.master.domain.entity.User;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.ResultLoadModel;
import org.bench4q.share.models.master.TestPlanModel;
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 TestPlanScriptResultControllerTest extends TestBase_MakeUpTestPlan {
	private String urlString = BASE_URL + "/testPlanResult";

	@Before
	public void prepare() {
		this.setAccessTocken(this.login());
	}

	@Test
	public void testGetScriptResults() throws IOException, JAXBException {
		HttpResponse httpResponse = this.httpRequester.sendGet(urlString + "/"
				+ this.getTestPlanRunIdUuid().toString()
				+ "/loadScriptBirefResults/" + this.getScriptId()
				+ "/averageResponseTime", null,
				this.makeAccessTockenMap(this.getAccessTocken()));
		ResultLoadModel loadModel = (ResultLoadModel) MarshalHelper.unmarshal(
				ResultLoadModel.class, httpResponse.getContent());
		assertEquals(3, loadModel.getValueTimeModels().size());
	}

	@Before
	public void makeUpTestPlanAndTestPlanScriptAndTestPlanScriptResult() {
		UUID testPlanRunId = UUID.randomUUID();
		User user = this.getUserRepository().getUser("admin");
		this.setScriptId(this.getUserFirstScript(user));
		TestPlanModel model = createATestPlanWithOneScript(this.getScriptId());
		// TestPlanInBusiness testPlan =
		// BusinessModelMapFactory.toBusiness(model);
		this.getTestPlanEngine().submitTestPlan(model, user, testPlanRunId);
		this.setTestPlanRunIdUuid(testPlanRunId);
		for (int i = 0; i < 3; i++) {
			/*
			 * this.getTestScriptResultSave().doSaveResult(testPlanRunId,
			 * this.getScriptId(), buildScriptBriefResultModel(i), new Date());
			 * this.getTestScriptResultSave().doSaveResult(testPlanRunId,
			 * this.getScriptId(), new ScriptBehaviorsBriefModel(), new Date());
			 * 
			 * this.getTestScriptResultSave().doSaveResult(testPlanRunId,
			 * this.getScriptId(), new ScriptPageBriefModel(), new Date());
			 */
		}

	}

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