package org.bench4q.agent.test.plugin;

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.bench4q.agent.plugin.basic.MongoDB.MongoDBPlugin;
import org.bench4q.agent.plugin.basic.MongoDB.MongoDBReturn;
import org.bench4q.agent.scenario.Scenario;
import org.bench4q.agent.scenario.engine.VUser;
import org.bench4q.agent.test.TestBase;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.helper.TestHelper;
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_MongoDBPlugin extends TestBase {
	private UUID testId = UUID.randomUUID();

	
	//localhost", 10000
	@Test
	public void test_Insert() {
		MongoDBPlugin mongoDBPlugin = new MongoDBPlugin("m001.node.com", 27017,
				"temp");
		MongoDBReturn result = mongoDBPlugin
				.insert(this.testId.toString(),
						"value1AAAAAAAAAAAAAAAAAAAAAAASFFFAFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
		result = mongoDBPlugin.query("propertyName=key|propertyValue="
				+ this.testId.toString() + "|;");
		assertTrue(result.getSuccessCount() > 0);
	}

	private RunScenarioModel buildUpMongoDBScenario() throws IOException {
		RunScenarioModel scenarioModel = new RunScenarioModel();
		scenarioModel.setUsePlugins(new LinkedList<UsePluginModel>());
		UsePluginModel mongoDB = new UsePluginModel();
		String pluginId = "mongo1";
		mongoDB.setId(pluginId);
		mongoDB.setName("MongoDBPlugin");
		mongoDB.setParameters(Arrays.asList(
				ParameterModel.createParameter("hostName", "m001.node.com"),
				ParameterModel.createParameter("port", "27017"),
				ParameterModel.createParameter("dbName", "temp")));
		scenarioModel.getUsePlugins().add(mongoDB);

		scenarioModel.setPages(new LinkedList<PageModel>());
		
		int idGenerator = 1;
		PageModel page1 = new PageModel(idGenerator++);
		BatchModel batch = new BatchModel(idGenerator++);
		page1.getBatches().add(batch);
		scenarioModel.getPages().add(page1);

		batch.getBehaviors()
				.add(BehaviorModel.UserBehaviorBuilder(
						0,
						"Insert",
						pluginId,
						Arrays.asList(
								ParameterModel.createParameter("key",
										this.testId.toString()),
								ParameterModel
										.createParameter(
												"value",
												"ZXZXCZXVZXZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"))));
		batch.getBehaviors().add(
				BehaviorModel.UserBehaviorBuilder(1, "Query", pluginId, Arrays
						.asList(ParameterModel.createParameter(
								"properties",
								"propertyName=key|propertyValue="
										+ this.testId.toString() + "|;"))));
		String scriptContent = MarshalHelper.tryMarshal(scenarioModel);
		System.out.println(scriptContent);
		File targetFile = new File("Scripts"
				+ System.getProperty("file.separator") + "mongoScript.xml");
		TestHelper.createFileIfNotExist(targetFile);
		FileUtils.writeStringToFile(targetFile, scriptContent);
		return scenarioModel;
	}

	@Test
	public void test_BuildScenario() throws IOException {
		RunScenarioModel result = buildUpMongoDBScenario();
		VUser vUser = createVUser(Scenario.scenarioBuilderWithCompile(result),
				UUID.randomUUID());
		vUser.run();
	}
}
