package com.eactive.eai.transformer;

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 junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;


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

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

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

    public void setUp() throws Exception {
        // 
        engine = TransformEngine.getInstance();

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

    public void tearDown() throws Exception {
        //
    }
	public void atestLayoutTest() throws Exception {
		byte[] b = {0x09};
		String Delimiter = new String(b);
		System.out.println("layout Test ==============================================================");
		
		Message source = MessageFactory.getFactory().getMessage("TST_T311TR0107_CBSS");
		StringBuilder sb = new StringBuilder();
		sb.append(Delimiter);
		sb.append("12345678901234567890").append(Delimiter);
		sb.append("012").append(Delimiter);
		sb.append("1234567890123456").append(Delimiter);
		sb.append("19720407").append(Delimiter);
		sb.append("james").append(Delimiter);
		sb.append("userci-adfaadddccccc1234567890userci-adfaadddccccc1234567890userci-adfaadddccccc1234567890sbdjuuuuuu").append(Delimiter);
		sb.append("test@eactive.co.kr").append(Delimiter);
		sb.append("scope123").append(Delimiter);
		sb.append("Y").append(Delimiter);
		sb.append("Y").append(Delimiter);
		sb.append("1").append(Delimiter);
		
		String data =sb.toString();
		source.setData(data);
		System.out.println(source);
		
		System.out.println("["+source.getData()+"]");

	} 
	public void atestTransformerTest() throws Exception {
		byte[] b = {0x09};
		String Delimiter = new String(b);
		System.out.println("transformer Test Delimiter=> JSON ==============================================================");
		
		Message source = MessageFactory.getFactory().getMessage("TST_T311TR0107_CBSS");
		
		StringBuilder sb = new StringBuilder();
		sb.append(Delimiter);
		sb.append("12345678901234567890").append(Delimiter);
		sb.append("012").append(Delimiter);
		sb.append("1234567890123456").append(Delimiter);
		sb.append("19720407").append(Delimiter);
		sb.append("james").append(Delimiter);
		sb.append("userci-adfaadddccccc1234567890userci-adfaadddccccc1234567890userci-adfaadddccccc1234567890sbdjuuuuuu").append(Delimiter);
		sb.append("test@eactive.co.kr").append(Delimiter);
		sb.append("scope123").append(Delimiter);
		sb.append("Y").append(Delimiter);
		sb.append("Y").append(Delimiter);
		sb.append("1").append(Delimiter);
		 
		String data = sb.toString();
		source.setData(data);
		System.out.println(source);
		System.out.println("["+new String((byte[])source.getData())+"]");

		Transformer transformer = engine.getTransformer();
		Message target = transformer.transform("TRANSFORM_TST_T311TR0107S2_REQ", new Message[] { source });
		
		
		System.out.println(target);
		System.out.println("["+target.getData()+"]");
		
		
	} 
	public void atestTransformer2Test() throws Exception {
		System.out.println("transformer Test JSON=> Delimiter=============================================================");
		
		Message source = MessageFactory.getFactory().getMessage("COP_T311TR0107_ORGS");
		
		String data ="{\"COP_T311TR0107_ORGS\":{\"EAI_BODY\":{\"user_ci\":\"userci-adfaadddccccc1234567890userci-adfaadddccccc1234567890userci-adfaadddccccc1234567890sbdjuuuuuu\",\"agmt_data_type\":\"1\",\"wd_agmt_yn\":\"Y\",\"user_email\":\"test@eactive.co.kr\",\"info_prvd_agmt_yn\":\"Y\",\"user_info\":\"19720407\",\"user_name\":\"james\",\"scope\":\"scope123\",\"bank_code_std\":\"012\",\"bank_tran_id\":\"12345678901234567890\",\"register_account_num\":\"1234567890123456\"}}}";
		
		source.setData(data);
		System.out.println(source);
		
		System.out.println("["+source.getData()+"]");

		Transformer transformer = engine.getTransformer();
		Message target = transformer.transform("TRANSFORM_TST_T311TR0107S2_RES", new Message[] { source });
		
		
		System.out.println(target);
		System.out.println("["+target.getData()+"]");
		
		
	} 	
	public void testTransformerUJSONToDelimiterTest() throws Exception {
		System.out.println("transformer Test UJSON=> DELIMITER ==============================================================");
		
		Message source = MessageFactory.getFactory().getMessage("EAI_TST0200100U_REQ1");
		
		String str ="";
		str = str + " {\"EAI_TST0200100U_REQ1\":                                ";
		str = str + " 		{\"trancode1\":\"0200\"                         ";
		str = str + " 		, \"trancode2\":\"100\"                        ";
		str = str + " 		, \"tracekey1\":\"trac1\"                        ";
		str = str + " 		, \"tracekey2\":\"trac2\"                        ";
		str = str + " 		, \"bizkey1\":\"bizk1\"                            ";
		str = str + " 		, \"bizkey2\":\"bizk2\"                            ";
		str = str + " 		, \"grid1Count\":\"2\"                               ";
		str = str + " 		, \"grid1\":[                                      ";
		str = str + " 			{\"data1\" : \"a001\",\"data2\":\"a002\"}        ";
		str = str + " 			,{\"data1\" : \"b001\",\"data2\":\"b002\"}       ";
		str = str + " 		]                                                ";
		str = str + " 		, \"add1\":\"add\"                               ";
		str = str + " 		}                                                ";
		str = str + " }                                                      ";
		 
		
		source.setData(str);
		System.out.println(source);
		System.out.println("["+source.getData()+"]");

		Transformer transformer = engine.getTransformer();
		Message target = transformer.transform("TRANSFORM_EAI_TST0200100DTST_REQ", new Message[] { source });
		
		
		System.out.println(target);
		System.out.println("["+new String((byte[])target.getData())+"]");
		
		
	} 
	public void testTransformerDelimiterToUJSONTest() throws Exception {
		byte[] b = {0x09};
		String Delimiter = new String(b);
		System.out.println("transformer Test UJSON=> DELIMITER  ==============================================================");
		
		Message source = MessageFactory.getFactory().getMessage("EAI_TST0200100D_REQ1");
		
		StringBuilder sb = new StringBuilder();
		sb.append(Delimiter);
		sb.append("0200").append(Delimiter);
		sb.append("100").append(Delimiter);
		sb.append("trac1").append(Delimiter);
		sb.append("trac2").append(Delimiter);
		sb.append("bizk1").append(Delimiter);
		sb.append("bizk2").append(Delimiter);
		sb.append("2").append(Delimiter);
		sb.append("a001").append(Delimiter);
		sb.append("a002").append(Delimiter).append("\n");
		sb.append("b001").append(Delimiter);
		sb.append("b002").append(Delimiter).append("\n");
		sb.append("add11").append(Delimiter);
		
		
		source.setData(sb.toString());
		//source.setData(sb.toString().getBytes());
		System.out.println(source);
		System.out.println("["+new String((byte[])source.getData())+"]");
		
		Transformer transformer = engine.getTransformer();
		Message target = transformer.transform("TRANSFORM_EAI_TST0200100UTST_REQ", new Message[] { source });
		
		
		System.out.println(target);
		System.out.println("["+target.getData()+"]");
		
		
	} 
    
}
