package org.bench4q.agent.test.scenario;

import static org.junit.Assert.*;

import java.util.List;

import org.bench4q.agent.scenario.dfa.DFA;
import org.bench4q.agent.scenario.dfa.ParamPart;
import org.bench4q.agent.scenario.dfa.ParamPart.ParamPartType;
import org.junit.Test;

public class Test_DFA {
	private static final String TEST_CASE = "${csvProvider0:userName}";
	private static final String TEST_CASE2 = "${csvProvider0:userName}$#";
	private static final String TEST_CASE3 = "${csvProvider0:userName}$#okOrNot";
	private static final String TEST_CASE4 = "${csvProvider0:userName}$#okOrNot${file.separator}";

	private static final String TEST_CASE_NOTSUPPORT1 = "${{abc";
	private static final String TEST_CASE_NOTSUPPORT2 = "${csvProvider0:adc{";
	private static final String TEST_CASE_NOTSUPPORT3 = "$abc";

	@Test
	public void test_resolveMaltipleParamPart_WithOnePluginIdVariable() {
		List<ParamPart> result = DFA.resolveMaltipleParamPart(TEST_CASE);
		assertEquals(1, result.size());
		assertEquals("csvProvider0", result.get(0)
				.getPluginIdForContextCallType());
		assertEquals("userName", result.get(0)
				.getVariableForContextCallAndProperty());
		assertNull(result.get(0).getContentForStringType());
	}

	@Test
	public void test_resolveMaltipleParamPart_WithOnePluginIdVariable_AND_OneSessionID() {
		List<ParamPart> result = DFA.resolveMaltipleParamPart(TEST_CASE2);
		assertEquals(2, result.size());
		assertEquals("csvProvider0", result.get(0)
				.getPluginIdForContextCallType());
		assertEquals(ParamPartType.CONTEXT_CALL, result.get(0).getType());
		assertEquals("userName", result.get(0)
				.getVariableForContextCallAndProperty());
		assertNull(result.get(0).getContentForStringType());
		assertEquals(ParamPartType.SESSION_ID, result.get(1).getType());
		assertNull(result.get(1).getContentForStringType());
		assertNull(result.get(1).getPluginIdForContextCallType());
		assertNull(result.get(1).getVariableForContextCallAndProperty());
	}

	@Test
	public void test_resolveMaltipleParamPart_WithOnePluginIdVariable_AND_OneSessionID_And_PlainString() {
		List<ParamPart> result = DFA.resolveMaltipleParamPart(TEST_CASE3);
		assertEquals(3, result.size());
		assertEquals("csvProvider0", result.get(0)
				.getPluginIdForContextCallType());
		assertEquals(ParamPartType.CONTEXT_CALL, result.get(0).getType());
		assertEquals("userName", result.get(0)
				.getVariableForContextCallAndProperty());
		assertNull(result.get(0).getContentForStringType());

		assertEquals(ParamPartType.SESSION_ID, result.get(1).getType());
		assertNull(result.get(1).getContentForStringType());
		assertNull(result.get(1).getPluginIdForContextCallType());
		assertNull(result.get(1).getVariableForContextCallAndProperty());

		assertEquals(ParamPartType.STRING, result.get(2).getType());
		assertNull(result.get(2).getPluginIdForContextCallType());
		assertNull(result.get(2).getVariableForContextCallAndProperty());
		assertEquals("okOrNot", result.get(2).getContentForStringType());
	}

	@Test
	public void test_resolveMaltipleParamPart_WithOnePluginIdVariable_AND_OneSessionID_And_PlainString_AND_Property() {
		List<ParamPart> result = DFA.resolveMaltipleParamPart(TEST_CASE4);
		assertEquals(4, result.size());
		assertEquals("csvProvider0", result.get(0)
				.getPluginIdForContextCallType());
		assertEquals(ParamPartType.CONTEXT_CALL, result.get(0).getType());
		assertEquals("userName", result.get(0)
				.getVariableForContextCallAndProperty());
		assertNull(result.get(0).getContentForStringType());

		assertEquals(ParamPartType.SESSION_ID, result.get(1).getType());
		assertNull(result.get(1).getContentForStringType());
		assertNull(result.get(1).getPluginIdForContextCallType());
		assertNull(result.get(1).getVariableForContextCallAndProperty());

		assertEquals(ParamPartType.STRING, result.get(2).getType());
		assertNull(result.get(2).getPluginIdForContextCallType());
		assertNull(result.get(2).getVariableForContextCallAndProperty());
		assertEquals("okOrNot", result.get(2).getContentForStringType());

		assertEquals(ParamPartType.PROPERTY, result.get(3).getType());
		assertNull(result.get(3).getPluginIdForContextCallType());
		assertNull(result.get(3).getContentForStringType());
		assertEquals("file.separator", result.get(3)
				.getVariableForContextCallAndProperty());
	}

	@Test
	public void test_resolveMaltipleParamPart_WithTheNotSupportOnes() {
		test_ForUnsupportOnes(TEST_CASE_NOTSUPPORT1);
		test_ForUnsupportOnes(TEST_CASE_NOTSUPPORT2);
		test_ForUnsupportOnes(TEST_CASE_NOTSUPPORT3);
	}

	private void test_ForUnsupportOnes(String testcaseUnsupport) {
		List<ParamPart> result = DFA
				.resolveMaltipleParamPart(testcaseUnsupport);
		assertEquals(1, result.size());
		assertEquals(ParamPartType.STRING, result.get(0).getType());
		assertNull(result.get(0).getPluginIdForContextCallType());
		assertNull(result.get(0).getVariableForContextCallAndProperty());
		assertEquals(testcaseUnsupport, result.get(0).getContentForStringType());
	}
}
