package com.eactive.eai.transformer;

import org.apache.commons.lang3.StringUtils;

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 LayoutVFieldTest extends TestCase {
	protected static Logger logger = Logger.getLogger("transformer");

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

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

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

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

    public void tearDown() throws Exception {
        //
    }
	public void atestVfield() throws Exception {
		System.out.println("testVfield ==============================================================");
		Message target = null;
		byte[] data = null;
		byte[] result = null;
		target = MessageFactory.getFactory().getMessage("EAI_TST003_REQ1");
		System.out.println("set data before="+target);
		data = "KBO0002AAAA1AAAA2BBBB1BBBB20010ABCDEFGHIJ0005543210003AAAA1AAAA2BBBB1BBBB2CCCC1CCCC2".getBytes();
		System.out.println(new String(data));
		target.setData(data);
		System.out.println("set data after="+target);
//		result =(byte[])target.getData();
//		System.out.println("result=["+result.length+"][" + new String(result)+"]");



	} 
	public void atestXml() throws Exception {
		System.out.println("testXml ==============================================================");
		Message target = null;
		
        String reqXML = "<EAI_TST003_REQ2>"
        		+ "<InData>"  // 
        		+ "<reqGroupCoCd>KBO</reqGroupCoCd>"
        		+ "<grid1>"
        		+ "<field1>AAAA1</field1>"
        		+ "<field2>BBBB1</field2>"
        		+ "</grid1>"
        		+ "<grid1>"
        		+ "<field1>AAAA2</field1>"
        		+ "<field2>BBBB2</field2>"
        		+ "</grid1>"
        		+ "<reqCount1>0010</reqCount1>"
        		+ "<reqMsgP1>ABCDEFGHIJ</reqMsgP1>"
        		+ "<reqCount2>0005</reqCount2>"
        		+ "<reqMsgP2>54321</reqMsgP2>"
        		+ "<grid2>"
        		+ "<field1>AAAA1</field1>"
        		+ "<field2>BBBB1</field2>"
        		+ "</grid2>"
        		+ "<grid2>"
        		+ "<field1>AAAA2</field1>"
        		+ "<field2>BBBB2</field2>"
        		+ "</grid2>"        		
        		+ "<grid2>"
        		+ "<field1>AAAA3</field1>"
        		+ "<field2>BBBB3</field2>"
        		+ "</grid2>"        		
        		+ "</InData>"
        		+ "</EAI_TST003_REQ2>";
        
		target = MessageFactory.getFactory().getMessage("EAI_TST003_REQ2");
		System.out.println("before="+target);
		target.setData(reqXML);
		//System.out.println("reqMsgP1="+target.getString("EAI_TST003_REQ2.InData.reqMsgP1"));
		System.out.println("after="+target);
		System.out.println("result=" + (String)target.getData());
		
	} 
	public void atestTransformXmlToFlat() throws Exception {
		System.out.println("testTransformXmlToFlat ==============================================================");
	    Message source = MessageFactory.getFactory().getMessage("EAI_TST003_REQ2");
	    Message target = MessageFactory.getFactory().getMessage("EAI_TST003_REQ1");
	        
	    String reqXML = "<EAI_TST003_REQ2>"
	    		+ "<InData>"  // 
	    		+ "<reqGroupCoCd>KBO</reqGroupCoCd>"
	    		+ "<grid1>"
	    		+ "<field1>AAAA1</field1>"
	    		+ "<field2>BBBB1</field2>"
	    		+ "</grid1>"
	    		+ "<grid1>"
	    		+ "<field1>AAAA2</field1>"
	    		+ "<field2>BBBB2</field2>"
	    		+ "</grid1>"
	    		+ "<reqCount1>0010</reqCount1>"
	    		+ "<reqMsgP1>ABCDEFGHIJ</reqMsgP1>"
	    		+ "<reqCount2>0005</reqCount2>"
	    		+ "<reqMsgP2>54321</reqMsgP2>"
	    		+ "<grid2>"
	    		+ "<field1>AAAA1</field1>"
	    		+ "<field2>BBBB1</field2>"
	    		+ "</grid2>"
	    		+ "<grid2>"
	    		+ "<field1>AAAA2</field1>"
	    		+ "<field2>BBBB2</field2>"
	    		+ "</grid2>"        		
	    		+ "<grid2>"
	    		+ "<field1>AAAA3</field1>"
	    		+ "<field2>BBBB3</field2>"
	    		+ "</grid2>"        		
	    		+ "</InData>"
	    		+ "</EAI_TST003_REQ2>";
	    System.out.println(reqXML);    
	    source.setData(reqXML);
	    System.out.println(source);
        Transformer transformer = engine.getTransformer();
        transformer.transform("TRANSFORM_EAI_TST003_REQ", target, new Message[] { source });
        System.out.println(target);
        System.out.println(new String((byte[])target.getData()));
		
	} 
	public void atestTransformFlatToXml() throws Exception {
		System.out.println("testTransformFlatToXml ==============================================================");
		Message source = MessageFactory.getFactory().getMessage("EAI_TST003_REQ1");
	    Message target = MessageFactory.getFactory().getMessage("EAI_TST003_REQ2");
	        
        byte[] data = "KBO0002AAAA1AAAA2BBBB1BBBB20010ABCDEFGHIJ0005543210003AAAA1AAAA2BBBB1BBBB2CCCC1CCCC2".getBytes();
	        
	    source.setData(data);
        Transformer transformer = engine.getTransformer();
        transformer.transform("TRANSFORM_EAI_TST003_RES", target, new Message[] { source });
        System.out.println(target);
        System.out.println("result=" + (String)target.getData());
	} 
	public void atestTransformFlatToJson() throws Exception {
		System.out.println("testTransformFlatToJson ==============================================================");
		Message source = MessageFactory.getFactory().getMessage("SAD_BFF71K44RR_REQ1");
		Message target = MessageFactory.getFactory().getMessage("BFF_BFF71K44RR_REQ2");
		
		byte[] data = " ".getBytes();
		
		source.setData(data);
		Transformer transformer = engine.getTransformer();
		transformer.transform("TRANSFORM_SAD_BFF71K44RRSAD_REQ", target, new Message[] { source });
		System.out.println(target);
		System.out.println("result=" + (String)target.getData());
	} 
	public void atestTransformFlatToXml2() throws Exception {
		System.out.println("testTransformFlatToXml ==============================================================");
		Message source = MessageFactory.getFactory().getMessage("KMB_EMC0210501100R01_RES1");
        byte[] data = "0243HDRIDV0040210501110C000011202004091625400006957                004IDV000695702018204271559512�̼���                                                                        201604091801081298510000000000049840T002075   08700                     ".getBytes();
	    source.setData(data);
	    System.out.println(source);
	    System.out.println("-----------------------------------------------------------------");
        Transformer transformer = engine.getTransformer();
        Message target = transformer.transform("TRANSFORM_KMB_EMC0210501100KMB_RES", new Message[] { source });
        System.out.println(target);
        System.out.println("result=" + (String)target.getData());
	} 
	public void testTransformVFlatToJson() throws Exception {
		System.out.println("testTransformFlatToJson ==============================================================");
		Message source = MessageFactory.getFactory().getMessage("TST_VFIELD_REQ");
		Message target = MessageFactory.getFactory().getMessage("TST_UJSONVFIELD_REQ");
		
		byte[] data = "0010ABCDEFGHIJ0011A0123456789".getBytes();
		
		source.setData(data);
		System.out.println(source);
		Transformer transformer = engine.getTransformer();
		transformer.transform("TRANSFORM_TST_VFIELD2UJSON_REQ", target, new Message[] { source });
		System.out.println(target);
		System.out.println("result=" + (String)target.getData());
	} 
	public void testTransformUJsonToVField() throws Exception {
		System.out.println("testTransformFlatToJson ==============================================================");
		Message source = MessageFactory.getFactory().getMessage("TST_UJSONVFIELD_REQ");
		Message target = MessageFactory.getFactory().getMessage("TST_VFIELD_REQ");
		
		String data = "{\"TST_UJSONVFIELD_REQ\" : { \"reqCount1\":\"0010\",\"reqMsgP1\":\"ABCDEFGHIJ\",\"reqCount2\":\"0011\",\"reqMsgP2\":\"A1234567890\"}}";
		
		source.setData(data);
		System.out.println(source);
		Transformer transformer = engine.getTransformer();
		transformer.transform("TRANSFORM_TST_UJSON2VFIELD_REQ", target, new Message[] { source });
		System.out.println(target);
		System.out.println("result=" + new String((byte[])target.getData()));
	} 
	public void testTransformVFlatToJson2() throws Exception {
		System.out.println("testTransformFlatToJson ==============================================================");
		Message source = MessageFactory.getFactory().getMessage("XYZ_OTST0000000X_REQ");
		String d = StringUtils.leftPad("", 159);
		d = d + "00010ABCDEFGHIJ";
		byte[] data = d.getBytes();
		
		source.setData(data);
		System.out.println(source);
	} 
	

    
}
