package org.bench4q.recorder;

import static org.junit.Assert.*;

import java.util.List;

import org.bench4q.recorder.httpcapture.Bench4qTestScriptAdapter;
import org.bench4q.recorder.httpcapture.IScriptAdapter;
import org.bench4q.recorder.httpcapture.generator.ChildrenUrl;
import org.bench4q.share.helper.RunScenarioModelHelper;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.scriptrecord.BatchModel;
import org.junit.Test;

public class TestParentRequest extends TestRecordBase {
	private IScriptAdapter adapater;
	private static String parentUrl = "www.apache.com";
	private static String childUrl = "www.yahoo.com";
	private static String independentUrl = "www.baidu.com";
	private static String independentUrl2 = "www.eclipse.org";
	private static String childUrl2 = "www.tomcat.com";

	private IScriptAdapter getAdapater() {
		return adapater;
	}

	private void setAdapater(IScriptAdapter adapater) {
		this.adapater = adapater;
	}

	public TestParentRequest() {
		this.setAdapater(new Bench4qTestScriptAdapter(new RunScenarioModel()));
	}

	@Test
	public void testAddOneBehaviorWihoutChildrenUrl() {
		this.getAdapater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(parentUrl));
		assertNotNull(this.getAdapater().getRunScenarioModel());
		List<BatchModel> batches = RunScenarioModelHelper.getBatches(this
				.getAdapater().getRunScenarioModel());
		assertNotNull(batches);
		assertNotNull(batches.get(0));
		assertTrue(batches.get(0).getId() == 3);

	}

	@Test
	public void testAddTwoBehaviorWithOutChildrenUrl() {
		this.getAdapater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(parentUrl));
		this.getAdapater().getChildrenUrls()
				.add(createChildrenUrl(childUrl, 0));

		this.getAdapater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(independentUrl));
		this.getAdapater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(independentUrl2));
		RunScenarioModel runScenarioModel = this.getAdapater()
				.getRunScenarioModel();
		assertNotNull(runScenarioModel);
		List<BatchModel> batches = RunScenarioModelHelper
				.getBatches(runScenarioModel);
		assertNotNull(batches);
		assertTrue(batches.size() == 3);
		assertTrue(batches.get(1).getBehaviors().size() == 1);
		assertTrue(batches.get(2).getBehaviors().size() == 1);
		assertTrue(batches.get(1).getBehaviors().get(0).getParameters().get(0)
				.getValue().equals(independentUrl));
		assertTrue(batches.get(2).getBehaviors().get(0).getParameters().get(0)
				.getValue().equals(independentUrl2));
	}

	// private UserBehaviorModel createUserBehavior(String url) {
	// UserBehaviorModel model = new UserBehaviorModel();
	// model.setId(1);
	// model.setName("Get");
	// model.setUse("http");
	// List<ParameterModel> params = new ArrayList<ParameterModel>();
	// ParameterModel param = new ParameterModel();
	// param.setKey("url");
	// param.setValue(url);
	// params.add(param);
	// model.setParameters(params);
	// return model;
	// }

	@Test
	public void testAddBehaviorWithInChildrenUrl() {
		this.getAdapater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(parentUrl));
		this.getAdapater().getChildrenUrls()
				.add(createChildrenUrl(childUrl, 0));
		this.getAdapater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(childUrl));
		RunScenarioModel runScenarioModel = this.getAdapater()
				.getRunScenarioModel();
		assertNotNull(runScenarioModel);
		List<BatchModel> batches = RunScenarioModelHelper
				.getBatches(runScenarioModel);
		assertNotNull(batches);
		assertNotNull(batches.get(0));
		assertNotNull(batches.get(1));
		assertTrue(batches.get(1).getId() == 5);
//		assertEquals(batches.get(0).getChildId(), batches.get(1).getId());
//		assertEquals(batches.get(1).getParentId(), batches.get(0).getId());
	}

	private ChildrenUrl createChildrenUrl(String url, int parentBatchId) {
		ChildrenUrl childrenUrl = new ChildrenUrl();
		childrenUrl.setParentBatchId(parentBatchId);
		childrenUrl.setUrl(url);
		return childrenUrl;
	}

	@Test
	public void testAddBehaviorsIncludeInAndOut() {
		this.getAdapater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(parentUrl));

		this.getAdapater().getChildrenUrls()
				.add(createChildrenUrl(childUrl, 0));

		this.getAdapater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(childUrl));
		this.getAdapater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(independentUrl));
		List<BatchModel> batches = RunScenarioModelHelper.getBatches(this
				.getAdapater().getRunScenarioModel());
		assertTrue(batches.get(1).getBehaviors().size() == 1);
		assertTrue(batches.get(2).getBehaviors().size() == 1);
		assertTrue(batches.get(1).getBehaviors().get(0).getParameters().get(0)
				.getValue().equals(childUrl));
		assertTrue(batches.get(2).getBehaviors().get(0).getParameters().get(0)
				.getValue().equals(independentUrl));
//		System.out.println("hemeimei:"+batches.get(0).getChildId()+"|"+ batches.get(1).getId());
//		assertEquals(batches.get(0).getChildId(), batches.get(1).getId());
//		assertEquals(batches.get(1).getParentId(), batches.get(0).getId());
	}

	@Test
	public void addTwoBehaviorsInChildrenUrl() {
		this.getAdapater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(parentUrl));
		this.getAdapater().getChildrenUrls()
				.add(createChildrenUrl(childUrl, 0));
		this.getAdapater().getChildrenUrls()
				.add(createChildrenUrl(childUrl2, 0));
		this.getAdapater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(childUrl));
		this.getAdapater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(childUrl2));
		List<BatchModel> batches = RunScenarioModelHelper.getBatches(this
				.getAdapater().getRunScenarioModel());
		System.out.println(batches.size());
		assertTrue(batches.size() == 2);
		assertTrue(batches.get(0).getBehaviors().get(0).getParameters().get(0)
				.getValue().equals(parentUrl));
		assertTrue(batches.get(1).getBehaviors().get(0).getParameters().get(0)
				.getValue().equals(childUrl));
		assertTrue(batches.get(1).getBehaviors().get(1).getParameters().get(0)
				.getValue().equals(childUrl2));
	}

	@Test
	public void addTwoInTwoOut() {
		this.getAdapater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(parentUrl));
		this.getAdapater().getChildrenUrls()
				.add(createChildrenUrl(childUrl, 0));
		this.getAdapater().getChildrenUrls()
				.add(createChildrenUrl(childUrl2, 0));
		this.getAdapater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(childUrl));
		this.getAdapater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(childUrl2));
		this.getAdapater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(independentUrl));
		this.getAdapater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(independentUrl2));
		assertTrue(RunScenarioModelHelper.getBatches(
				this.getAdapater().getRunScenarioModel()).size() == 4);
	}

	@Test
	public void testGetParentUrl() {
		this.getAdapater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(parentUrl));
		this.getAdapater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(independentUrl));
		this.getAdapater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(independentUrl2));
		assertTrue(this.getAdapater().getParentBatchIdWithParentUrl(parentUrl) == 3);
		assertTrue(this.getAdapater().getParentBatchIdWithParentUrl(
				independentUrl) == 5);
		assertTrue(this.getAdapater().getParentBatchIdWithParentUrl(
				independentUrl2) == 7);
	}
}
