package org.bench4q.agent.test.scenario;

import static org.junit.Assert.*;

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

import org.apache.commons.io.FileUtils;
import org.bench4q.agent.plugin.PluginManager;
import org.bench4q.agent.scenario.Scenario;
import org.bench4q.agent.scenario.engine.ScenarioContext;
import org.bench4q.agent.scenario.engine.ScenarioEngine;
import org.bench4q.agent.test.TestBase;
import org.bench4q.share.models.agent.ParameterModel;
import org.bench4q.share.models.agent.scriptrecord.BehaviorModel;
import org.bench4q.share.models.agent.scriptrecord.UsePluginModel;
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:application-context.xml" })
public class Test_ScenarioEngine extends TestBase {
	private ScenarioEngine scenarioEngine;
	private UUID testId = UUID.randomUUID();
	@Autowired
	private PluginManager pluginManager;

	private ScenarioEngine getScenarioEngine() {
		return scenarioEngine;
	}

	@Autowired
	private void setScenarioEngine(ScenarioEngine scenarioEngine) {
		this.scenarioEngine = scenarioEngine;
	}

	@Test
	public void test_UpdatePopulation() {
		ScenarioContext scenarioContext = buildScenarioContext(
				testId,
				Scenario.scenarioBuilderWithCompile(buildRunScenarioModelWith(
						new ArrayList<UsePluginModel>(), BehaviorModel
								.UserBehaviorBuilder(1, "ok", "http",
										new ArrayList<ParameterModel>()))),
				100, this.pluginManager);
		this.getScenarioEngine().getRunningTests().put(testId, scenarioContext);
		scenarioContext.updatePopulation(20);
		assertEquals(20, scenarioContext.getExecutor().getMaximumPoolSize());
		System.out.println(scenarioContext.getExecutor().getActiveCount());
	}

	@Test
	public void test_RunWithContext() throws IOException {
		ScenarioContext scenarioContext = buildScenarioContext(
				testId, Scenario.scenarioBuilderWithCompile(FileUtils
						.readFileToString(new File("Scripts"
								+ System.getProperty("file.separator")
								+ "homepage.xml"))), 100, this.pluginManager);
		this.getScenarioEngine().getRunningTests().put(testId, scenarioContext);
		this.getScenarioEngine().runWith(testId);
	}
}
