package org.bench4q.agent.test.plugin;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.UUID;

import org.bench4q.agent.plugin.basic.context.Context;
import org.bench4q.agent.scenario.Scenario;
import org.bench4q.agent.scenario.engine.VUser;
import org.bench4q.agent.test.TestBase;
import org.bench4q.share.models.agent.ParameterModel;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.scriptrecord.BatchModel;
import org.bench4q.share.models.agent.scriptrecord.BehaviorModel;
import org.bench4q.share.models.agent.scriptrecord.PageModel;
import org.bench4q.share.models.agent.scriptrecord.UsePluginModel;
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:application-context.xml" })
public class Test_ContextPlugin extends TestBase {
	@Test
	public void test_SetAndGet() {
		Context context = new Context(UUID.randomUUID().toString());
		context.set("abcd");
		System.out.println(context.getValue(""));
	}

	@Test
	public void testWithScenario() {
		RunScenarioModel scenarioModel = new RunScenarioModel();
		String pluginId = "context1";
		UsePluginModel context = UsePluginModel.createUsePlugin(pluginId,
				"Context","Context_"+pluginId, new ArrayList<ParameterModel>());
		UsePluginModel log = UsePluginModel.createUsePlugin("log1", "Log","log1",
				new ArrayList<ParameterModel>());
		scenarioModel.getUsePlugins().add(context);
		scenarioModel.getUsePlugins().add(log);
		BehaviorModel set = BehaviorModel.ControlBehaviorBuilder(0, "set",
				pluginId, Arrays.asList(ParameterModel.createParameter(
						"toSave", "chentienan")));
		BehaviorModel logIt = BehaviorModel.UserBehaviorBuilder(1, "Log",
				"log1", Arrays.asList(ParameterModel.createParameter("message",
						"${context1:test}")));
		int idGenerator = 1;
		PageModel pageModel = new PageModel(idGenerator++);
		scenarioModel.getPages().add(pageModel);
		BatchModel batchModel = new BatchModel(idGenerator++);
		pageModel.getBatches().add(batchModel);
		batchModel.getBehaviors().add(set);
		batchModel.getBehaviors().add(logIt);
		// System.out.println(MarshalHelper.tryMarshal(scenarioModel));
		VUser vuser = createVUser(
				Scenario.scenarioBuilderWithCompile(scenarioModel),
				UUID.randomUUID());
		vuser.run();
	}
}
