package org.bench4q.recorder;

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.bench4q.recorder.httpcapture.generator.ChildrenUrl;
import org.junit.Test;

public class TestDomGenerator extends TestRecordBase {
	// private URL url = this.getClass().getResource("testcase.html");
	private String url = "RecordScriptTestCase"
			+ System.getProperty("file.separator") + "testcase.html";

	public TestDomGenerator() {
		init();
	}

	@Test
	public void testDomParser() throws IOException {
		this.getCodeGenerator()
				.getScriptAdapter()
				.insertUserBehaviorsToScenario(
						createUserBehaviorModel(parentUrl));
		this.getCodeGenerator().doParseHtmlContent(
				FileUtils.readFileToString(new File(url)), parentUrl);
		assertTrue(this.getCodeGenerator().getScriptAdapter().getChildrenUrls()
				.size() == 8);
		for (ChildrenUrl childrenUrl : this.getCodeGenerator()
				.getScriptAdapter().getChildrenUrls()) {
			assertTrue(childrenUrl.getParentBatchId() == 3);
		}
	}

	@Test
	public void testDomParserWithinAdapter() throws IOException {
		this.getAdpater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(independentUrl));
		this.getAdpater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(parentUrl));
		this.getCodeGenerator().doParseHtmlContent(
				FileUtils.readFileToString(new File(url)), parentUrl);
		System.out.println(this.getCodeGenerator().getScriptAdapter()
				.getChildrenUrls().size());
		assertTrue(this.getCodeGenerator().getScriptAdapter().getChildrenUrls()
				.size() == 8);
		for (ChildrenUrl childrenUrl : this.getAdpater().getChildrenUrls()) {
			
			assertTrue(childrenUrl.getParentBatchId() == 5);
		}
	}

	@Test
	public void testDomParserWithIncorrectParentUrl() throws IOException {
		this.getAdpater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(parentUrl));
		this.getAdpater().insertUserBehaviorsToScenario(
				createUserBehaviorModel(independentUrl));
		this.getCodeGenerator().doParseHtmlContent(
				FileUtils.readFileToString(new File(url)), parentUrl + "error");
		assertTrue(this.getCodeGenerator().getScriptAdapter().getChildrenUrls()
				.size() == 0);
	}

}
