package com.eactive.eai.transformer;

import org.apache.commons.lang3.time.StopWatch;

import com.eactive.eai.common.util.Logger;
import com.eactive.eai.transformer.engine.TransformEngine;
import com.eactive.eai.transformer.engine.Transformer;
import com.eactive.eai.transformer.message.Message;
import com.eactive.eai.transformer.message.MessageFactory;
import com.eactive.eai.transformer.util.IConv;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;


public class TransformCommonErrorTest extends TestCase {
	protected static Logger logger = Logger.getLogger("transformer");

	TransformEngine engine = null;
    
    
    public TransformCommonErrorTest(String name) {
        super(name);
    }

    public static Test suite() {
        /*
        * the dynamic way
        */
        return new TestSuite(TransformCommonErrorTest.class);
    }

    public void setUp() throws Exception {
        // 변환 초기화
        engine = TransformEngine.getInstance();

        if (!engine.isStarted()) {
            engine.start();
        }
    }

    public void tearDown() throws Exception {
        //
    }

    
    public void atestXmlErr() throws Exception {
		System.out.println("XML ==============================================================");

		// ----------------------------------------------------------------------
		// source message 생성
		// ----------------------------------------------------------------------
		String data = "<EAI_XML_ERR><OutForms/><OutData ErrmsgErrNoitm=\"\" ErrmsgStpErrTranCd=\"\" ErrmsgStpErrNoitm=\"\" ErrmsgHighlightErrNoitm=\"\"><RpsntErrInfo ErrmsgErrcd=\"B4200099\" ErrmsgTreatCd=\"UKEE0896\" ErrmsgErrPgmName=\"com.kbstar.kee.fc.commonfc.ExceptionThrower.asDataAccessException()\" ErrmsgErrSituNo=\"170\"/></OutData></EAI_XML_ERR>";
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		
		Message source = MessageFactory.getFactory().getMessage("EAI_XML_ERR");
		source.setData(data);
		System.out.println(source);
		
		Transformer transformer = engine.getTransformer();
		Message target = transformer.transform("TRANSFORM_EAI_XMLTOUJN_ERR", new Message[] { source }); // TRANSFORM_EAI_XMLTOUJN_ERR
		System.out.println(target.toString());
		System.out.println("[" + target.getData() + "]");
		
//		transformer = engine.getTransformer();
//		target = transformer.transform("TRANSFORM_EAI_XMLTOEBC_ERR", new Message[] { source });
//		System.out.println(target.toString());
//		System.out.println("[" + target.getData() + "]");
//		
//		transformer = engine.getTransformer();
//		target = transformer.transform("TRANSFORM_EAI_XMLTOASC_ERR", new Message[] { source });
//		System.out.println(target.toString());
//		System.out.println("[" + target.getData() + "]");
		
		stopWatch.stop();
		System.out.println("    elapsed       : " + stopWatch.toString());
	}
    
	public void atestEbcdicErr() throws Exception {
		System.out.println("EBCDIC ==============================================================");

		// ----------------------------------------------------------------------
		// source message 생성
		// ----------------------------------------------------------------------
//		String data = "000000001111111133333333444444AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA010000000011111111333333334444447777777777010000000011111111333333334444445555555566666601BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
		
		// 반복참조회수에러 -> 고정그리드로 변경 후 정상
		String data = "B0900038UKFK0032IFK0024 000509                                                                                                    00                        000000                        000000                        000000                        000000                        000000                        000000                        000000                        000000                        000000                        000000KFK124F61002B0900038UKFK0032PFKA4F6 000422        +00000B0900038UKFK0032IFK0024 000509        +00000                        000000        +00000                        000000        +0000000                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ";
		
//		String data = "B0900038UKFK0032IFK0024 000509                                                                                                    10                        000000                        000000                        000000                        000000                        000000                        000000                        000000                        000000                        000000                        000000KFK124F61004B0900038UKFK0032PFKA4F6 000422        +00000B0900038UKFK0032IFK0024 000509        +00000                        000000        +00000                        000000        +0000010                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ";
//		StopWatch stopWatch = new StopWatch();
//		stopWatch.start();
		Message source = MessageFactory.getFactory().getMessage("EAI_EBCDIC_ERR");
//		source.setBytes("KAE_KAE0403140_RES1.OutData.outBook.grid[0].groupCoCd", "111".getBytes());
		source.setData(IConv.asciiToEBCDIC(data));
		System.out.println(source);

		Transformer transformer = engine.getTransformer();
		Message target = transformer.transform("TRANSFORM_EAI_EBCTOUJN_ERR", new Message[] { source });
		System.out.println(target.toString());
		System.out.println("[" + target.getData() + "]");
		
		transformer = engine.getTransformer();
		target = transformer.transform("TRANSFORM_EAI_EBCTOXML_ERR", new Message[] { source });
		System.out.println(target.toString());
		System.out.println("[" + target.getData() + "]");
		
		transformer = engine.getTransformer();
		target = transformer.transform("TRANSFORM_EAI_EBCTOASC_ERR", new Message[] { source });
		System.out.println(target.toString());
		System.out.println("[" + target.getData() + "]");
		
//		stopWatch.stop();
//		System.out.println("    elapsed       : " + stopWatch.toString());
	}
    
	public void testUjsonErr() throws Exception {
		System.out.println("UJSON ==============================================================");

		// ----------------------------------------------------------------------
		// source message 생성
		// ----------------------------------------------------------------------
		String data = "{\"EAI_UJSON_ERR\":{\"ErrData\":{\"ErrmsgHighlightErrNoitm\":\"00\",\"HighlightErrInfo\":[{\"ErrmsgTelgmItemName\":\"1                                                \"},{\"ErrmsgTelgmItemName\":\"2                                                \"},{\"ErrmsgTelgmItemName\":\"                                                  \"},{\"ErrmsgTelgmItemName\":\"                                                  \"},{\"ErrmsgTelgmItemName\":\"                                                  \"},{\"ErrmsgTelgmItemName\":\"                                                  \"},{\"ErrmsgTelgmItemName\":\"                                                  \"},{\"ErrmsgTelgmItemName\":\"                                                  \"},{\"ErrmsgTelgmItemName\":\"                                                  \"},{\"ErrmsgTelgmItemName\":\"                                                  \"}],\"MultiErrInfo\":[{\"ErrmsgErrcd\":\"1       \",\"ErrmsgErrSituNo\":\"000000\",\"ErrmsgErrPgmName\":\"        \",\"ErrmsgTreatCd\":\"        \"},{\"ErrmsgErrcd\":\"        \",\"ErrmsgErrSituNo\":\"000000\",\"ErrmsgErrPgmName\":\"        \",\"ErrmsgTreatCd\":\"        \"},{\"ErrmsgErrcd\":\"        \",\"ErrmsgErrSituNo\":\"000000\",\"ErrmsgErrPgmName\":\"        \",\"ErrmsgTreatCd\":\"        \"},{\"ErrmsgErrcd\":\"        \",\"ErrmsgErrSituNo\":\"000000\",\"ErrmsgErrPgmName\":\"        \",\"ErrmsgTreatCd\":\"        \"},{\"ErrmsgErrcd\":\"        \",\"ErrmsgErrSituNo\":\"000000\",\"ErrmsgErrPgmName\":\"        \",\"ErrmsgTreatCd\":\"        \"},{\"ErrmsgErrcd\":\"        \",\"ErrmsgErrSituNo\":\"000000\",\"ErrmsgErrPgmName\":\"        \",\"ErrmsgTreatCd\":\"        \"},{\"ErrmsgErrcd\":\"        \",\"ErrmsgErrSituNo\":\"000000\",\"ErrmsgErrPgmName\":\"        \",\"ErrmsgTreatCd\":\"        \"},{\"ErrmsgErrcd\":\"        \",\"ErrmsgErrSituNo\":\"000000\",\"ErrmsgErrPgmName\":\"        \",\"ErrmsgTreatCd\":\"        \"},{\"ErrmsgErrcd\":\"        \",\"ErrmsgErrSituNo\":\"000000\",\"ErrmsgErrPgmName\":\"        \",\"ErrmsgTreatCd\":\"        \"},{\"ErrmsgErrcd\":\"        \",\"ErrmsgErrSituNo\":\"000000\",\"ErrmsgErrPgmName\":\"        \",\"ErrmsgTreatCd\":\"        \"}],\"ErrmsgErrcd\":\"B0900038\",\"ErrmsgOutptCstzMsgCtnt\":\"                                                                                                    \",\"ErrmsgErrSituNo\":\"000509\",\"ErrmsgStpErrInfo\":[{\"ErrmsgStpErrSituNo\":\"000422\",\"ErrmsgStpTreatCd\":\"UKFK0032\",\"ErrmsgStpErrcd\":\"B0900038\",\"ErrmsgStpErrPgmName\":\"PFKA4F6 \",\"ErrmsgStpErrSqlCd\":\"+00000\",\"ErrmsgStpErrTblName\":\"        \"},{\"ErrmsgStpErrSituNo\":\"000509\",\"ErrmsgStpTreatCd\":\"UKFK0032\",\"ErrmsgStpErrcd\":\"B0900038\",\"ErrmsgStpErrPgmName\":\"IFK0024 \",\"ErrmsgStpErrSqlCd\":\"+00000\",\"ErrmsgStpErrTblName\":\"        \"},{\"ErrmsgStpErrSituNo\":\"000000\",\"ErrmsgStpTreatCd\":\"        \",\"ErrmsgStpErrcd\":\"        \",\"ErrmsgStpErrPgmName\":\"        \",\"ErrmsgStpErrSqlCd\":\"+00000\",\"ErrmsgStpErrTblName\":\"        \"},{\"ErrmsgStpErrSituNo\":\"000000\",\"ErrmsgStpTreatCd\":\"        \",\"ErrmsgStpErrcd\":\"        \",\"ErrmsgStpErrPgmName\":\"        \",\"ErrmsgStpErrSqlCd\":\"+00000\",\"ErrmsgStpErrTblName\":\"        \"}],\"ErrmsgMulErrNoitm\":\"00\",\"ErrmsgStpErrTranCd\":\"KFK124F610\",\"ErrmsgStpErrNoitm\":\"02\",\"ErrmsgErrPgmName\":\"IFK0024 \",\"ErrmsgTreatCd\":\"UKFK0032\"}}}";
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		Message source = MessageFactory.getFactory().getMessage("EAI_UJSON_ERR");
		source.setData(data);
		System.out.println(source);

		Transformer transformer = engine.getTransformer();
		Message target = transformer.transform("TRANSFORM_EAI_UJNTOXML_ERR", new Message[] { source });
		System.out.println(target.toString());
		System.out.println("[" + target.getData() + "]");
		
//		transformer = engine.getTransformer();
//		target = transformer.transform("TRANSFORM_EAI_UJNTOASC_ERR", new Message[] { source });
//		System.out.println(target.toString());
//		System.out.println("[" + target.getData() + "]");
		
//		transformer = engine.getTransformer();
//		target = transformer.transform("TRANSFORM_EAI_UJNTOEBC_ERR", new Message[] { source });
//		System.out.println(target.toString());
//		System.out.println("[" + target.getData() + "]");
		
		stopWatch.stop();
		System.out.println("    elapsed       : " + stopWatch.toString());
	}
	
	public void atestAscErr() throws Exception {
		System.out.println("ASCII ==============================================================");

		// ----------------------------------------------------------------------
		// source message 생성
		// ----------------------------------------------------------------------
		String data = "B0900038UKFK0032IFK0024 000509                                                                                                    00                        000000                        000000                        000000                        000000                        000000                        000000                        000000                        000000                        000000                        000000KFK124F61002B0900038UKFK0032PFKA4F6 000422        +00000B0900038UKFK0032IFK0024 000509        +00000                        000000        +00000                        000000        +0000000                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ";
//		StopWatch stopWatch = new StopWatch();
//		stopWatch.start();
		Message source = MessageFactory.getFactory().getMessage("EAI_ASCII_ERR");
		source.setData(data.getBytes());
		System.out.println(source);

		Transformer transformer = engine.getTransformer();
		Message target = transformer.transform("TRANSFORM_EAI_ASCTOEBC_ERR", new Message[] { source });
		System.out.println(target.toString());
		System.out.println("[" + target.getData() + "]");
		
		transformer = engine.getTransformer();
		target = transformer.transform("TRANSFORM_EAI_ASCTOXML_ERR", new Message[] { source });
		System.out.println(target.toString());
		System.out.println("[" + target.getData() + "]");
		
		transformer = engine.getTransformer();
		target = transformer.transform("TRANSFORM_EAI_ASCTOUJN_ERR", new Message[] { source });
		System.out.println(target.toString());
		System.out.println("[" + target.getData() + "]");
		
//		stopWatch.stop();
//		System.out.println("    elapsed       : " + stopWatch.toString());
	}
	
	public void atestbcdicToAsc() throws Exception {
		System.out.println("==============================================================");

		// ----------------------------------------------------------------------
		// source message 생성
		// ----------------------------------------------------------------------
		String data = "B0900038UKFK0032IFK0024 000509                                                                                                    00                        000000                        000000                        000000                        000000                        000000                        000000                        000000                        000000                        000000                        000000KFK124F61002B0900038UKFK0032PFKA4F6 000422        +00000B0900038UKFK0032IFK0024 000509        +00000                        000000        +00000                        000000        +0000000                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ";

		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		Message source = MessageFactory.getFactory().getMessage("EAI_EBCDIC_ERR");
//		source.setBytes("KAE_KAE0403140_RES1.OutData.outBook.grid[0].groupCoCd", "111".getBytes());
		source.setData(IConv.asciiToEBCDIC(data));
		System.out.println(source);

		Transformer transformer = engine.getTransformer();
		Message target = transformer.transform("TRANSFORM_EAI_EBCTOASC_ERR", new Message[] { source });
		
		System.out.println(target.toString());
		System.out.println("[" + new String((byte[]) target.getData()) + "]");
		stopWatch.stop();
		System.out.println("    elapsed       : " + stopWatch.toString());
	}
	
	public void atestEbcdicToXml() throws Exception {
		System.out.println("==============================================================");

		// ----------------------------------------------------------------------
		// source message 생성
		// ----------------------------------------------------------------------
		String data = "B0900038UKFK0032IFK0024 000509                                                                                                    00                        000000                        000000                        000000                        000000                        000000                        000000                        000000                        000000                        000000                        000000KFK124F61002B0900038UKFK0032PFKA4F6 000422        +00000B0900038UKFK0032IFK0024 000509        +00000                        000000        +00000                        000000        +0000000                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ";
		
		System.out.println(data.getBytes().length);
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		
		Message source = MessageFactory.getFactory().getMessage("EAI_EBCDIC_ERR");
//		source.setBytes("KAE_KAE0403140_RES1.OutData.outBook.grid[0].groupCoCd", "111".getBytes());
		source.setData(IConv.asciiToEBCDIC(data));
		//System.out.println(source);

		Transformer transformer = engine.getTransformer();
		Message target = transformer.transform("TRANSFORM_EAI_EBCTOXML_ERR", new Message[] { source });
		
		System.out.println(target.toString());
		System.out.println("[" + target.getData() + "]");
		stopWatch.stop();
		System.out.println("    elapsed       : " + stopWatch.toString());
	}
	
	public void atestEbcdicToXmlErr() throws Exception {
		System.out.println("==============================================================");

		// ----------------------------------------------------------------------
		// source message 생성
		// ----------------------------------------------------------------------
//		String data = "000000001111111133333333444444AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA010000000011111111333333334444447777777777010000000011111111333333334444445555555566666601BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
		String data = "B0900038UKFK0032IFK0024 000509                                                                                                    00                        000000                        000000                        000000                        000000                        000000                        000000                        000000                        000000                        000000                        000000KFK124F61002B0900038UKFK0032PFKA4F6 000422        +00000B0900038UKFK0032IFK0024 000509        +00000                        000000        +00000                        000000        +0000000                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ";
		data = data.substring(0,620);
		System.out.println(data.getBytes().length);
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		
		Message source = MessageFactory.getFactory().getMessage("EAI_TSTEBCDIC_ERR");
//		source.setBytes("KAE_KAE0403140_RES1.OutData.outBook.grid[0].groupCoCd", "111".getBytes());
		source.setData(IConv.asciiToEBCDIC(data));
		//System.out.println(source);

		Transformer transformer = engine.getTransformer();
		Message target = transformer.transform("TRANSFORM_EAI_TST0100543KGN_ERR", new Message[] { source });
		
		System.out.println(target.toString());
		System.out.println("[" + target.getData() + "]");
		stopWatch.stop();
		System.out.println("    elapsed       : " + stopWatch.toString());
	}
}
