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.Collections;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.bench4q.agent.plugin.basic.HBasePlugin.HBasePlugin;
import org.bench4q.agent.plugin.basic.HBasePlugin.HBaseReturn;
import org.bench4q.agent.scenario.Scenario;
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.BehaviorModel;
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_HBasePlugin extends TestBase {
	HBasePlugin hBase = new HBasePlugin("133.133.134.188", "133.133.13.154",
			"/home/Xinsheng/hbase/tmp/zookeeper_data", "133.133.134.188",
			"users");

	@Test
	public void test() {
		HBaseReturn result = this.hBase.insert("123", "chentienan");
		assertTrue(result.getSuccessCount() > 0);
	}

	@Test
	public void testQuery() {
		HBaseReturn result = this.hBase.query("123");
		assertTrue(result.getSuccessCount() > 0);
	}

	@Test
	public void testGenerateHBaseScenario() throws IOException {
		BehaviorModel[] behaviors = new BehaviorModel[19];
		for (int i = 0; i < 9; i++) {
			behaviors[2 * i] = BehaviorModel.ControlBehaviorBuilder(2 * i,
					"Next", "UUID_0", Collections.<ParameterModel> emptyList());
			behaviors[2 * i + 1] = BehaviorModel
					.UserBehaviorBuilder(
							2 * i + 1,
							"Insert",
							"HBasePlugin_0",
							Arrays.asList(
									ParameterModel.createParameter("key",
											"${UUID_0:var}"),
									ParameterModel
											.createParameter(
													"value",
													"828731929090399688663513844419156517791913487136162075054396888608979390802698967398774976538053071265086303536432973027547398485897859650637839199967916419278859657562410371550970990533755543895317958490879810955359859803793616914752497927102101107393602107374624265703167902037562601756290624400217677905796841586506540830996528039667809341170671731917392828543425909887469350859806312812589702773082295018742634842159778009485901428286815715514440370336211621120027294867866046842579395402354849290925687231979896816952130161564546780855710443002842992917011602246580128072986737704758180050405202976240171827695507627799312781883033823819663102843159141022325482803101497005554269652897365593399081219143594918151989844361363454642099858085072470136971359171934346214072016627940215525657780835264287095014748952849011967941296078931973149033644269161629346531549579584063826798825958153903463232155174301997795504543310963155667642944030315802981522872371957377604512253881055347955240594154853114826089693318722619283530276507503")));
		}
		behaviors[18] = BehaviorModel.TimerBehaviorBuilder(28, "Sleep",
				"Timer_0",
				Arrays.asList(ParameterModel.createParameter("time", "10")));
		RunScenarioModel runScenarioModel = buildRunScenarioModelWith(
				Arrays.asList(
						new UsePluginModel("Timer_0", "ConstantTimer", 
								Collections.<ParameterModel> emptyList()),
						new UsePluginModel("UUID_0", "UUID", null),
						new UsePluginModel("Context_0", "Context", null),
						new UsePluginModel(
								"HBasePlugin_0",
								"HBase",
								Arrays.asList(
										ParameterModel.createParameter(
												"dnsServer", "133.133.134.188"),
										ParameterModel.createParameter(
												"zookeeperQuorum",
												"133.133.13.154"),
										ParameterModel
												.createParameter(
														"zookeeperDataDir",
														"/home/Xinsheng/hbase/tmp/zookeeper_data"),
										ParameterModel.createParameter(
												"masterAdrress",
												"133.133.134.188"),
										ParameterModel.createParameter(
												"tableName", "users")))),
				behaviors);
		File file = new File("Scripts" + System.getProperty("file.separator")
				+ "hbaseScenario.xml");
		TestHelper.createFileIfNotExist(file);
		FileUtils.writeStringToFile(file,
				MarshalHelper.tryMarshal(runScenarioModel));
		createVUser(Scenario.scenarioBuilderWithCompile(runScenarioModel),
				UUID.randomUUID()).run();
	}
}
