package org.bench4q.master.integrated.communication;

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.bind.JAXBException;

import org.apache.commons.io.FileUtils;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.infrastructure.communication.impl.AgentMessengerImpl;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.RunScenarioResultModel;
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_AgentMessenger extends TestBase_MakeUpTestPlan {
	private static final String FILE_SEPARATOR = System
			.getProperty("file.separator");
	private static final String Test_HOSTNAME = "http://127.0.0.1";
	private static final int Test_PORT = 6565;
	private AgentMessengerImpl agentMessenger;

	public AgentMessengerImpl getAgentMessenger() {
		return agentMessenger;
	}

	@Autowired
	public void setAgentMessenger(AgentMessengerImpl agentMessenger) {
		this.agentMessenger = agentMessenger;
	}

	@Test
	public void testSubmitScenarioWithParamsAndRun() throws IOException,
			JAXBException {

		List<File> paramFiles = new ArrayList<File>();
		int scriptId = getUserFirstScript(this.getUserRepository().getUser(
				"admin"));
		File file1 = createParameterFile(scriptId, "param1");
		assertNotNull(file1);
		assertTrue(file1.exists());
		FileUtils.writeStringToFile(file1, "key=set;value=nv");
		File file2 = createParameterFile(scriptId, "param2");
		assertTrue(file2.exists());
		FileUtils.writeStringToFile(file2, "everyThing ok = ok");
		paramFiles.add(file1);
		paramFiles.add(file2);
		File scenarioFile = new File("Scripts" + FILE_SEPARATOR
				+ "forGoodRecord.xml");
		assertTrue(scenarioFile.exists());
		RunScenarioModel inputModel = (RunScenarioModel) MarshalHelper
				.unmarshal(RunScenarioModel.class,
						FileUtils.readFileToString(scenarioFile));
		inputModel.setPoolSize(20);

		Agent agent = Agent
				.createAgentWithoutId(Test_HOSTNAME, Test_PORT, 1000);
		RunScenarioResultModel model = this.getAgentMessenger().bookTest(agent,
				20);
		assertNotNull(model);
		assertNotNull(model.getRunId());
		System.out.println(model.getRunId());
		RunScenarioResultModel modelAfter = this.getAgentMessenger()
				.submitScenrioWithParams(agent, model.getRunId(), paramFiles,
						inputModel, new Date());
		assertEquals(model.getRunId(), modelAfter.getRunId());
		model = this.getAgentMessenger().runWithParams(agent, model.getRunId());
		assertNotNull(model);
		assertNotNull(model.getRunId());
		System.out.println(model.getRunId());
	}

	private File createParameterFile(int scriptId, String paramName)
			throws IOException {
		String dirPath = "ScriptParameterization" + FILE_SEPARATOR + scriptId
				+ FILE_SEPARATOR;
		File dirfile = new File(dirPath);
		File paramFile = new File(dirPath + paramName + ".txt");
		if (!dirfile.exists()) {
			dirfile.mkdirs();
		}
		if (!paramFile.exists()) {
			paramFile.createNewFile();
		}
		return paramFile;
	}

	@Test
	public void testGetScriptResult() throws InterruptedException,
			JAXBException {
		// Just test for if it can run properly;
	}

	@Test
	public void test_getStatus() {
		// this.agentMessenger.getStatus(Agent.createAgentWithoutId(hostName,
		// port))
	}

}
