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 com.eactive.eai.transformer.util.IConv;

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


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

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

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

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

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

    public void tearDown() throws Exception {
        //
    }
    
    public void atestXmlToJson() throws Exception {
  		System.out.println("Xml TO Json ==============================================================");

        //----------------------------------------------------------------------
        // source message 생성
        //----------------------------------------------------------------------
    	String data ="";
    	data = "<EAI_TSTXML_REQ1><GROUP><HANGLE>한글</HANGLE><TEXT>ABCD1234</TEXT><BASE>한글</BASE></GROUP></EAI_TSTXML_REQ1>";
    	System.out.println(data);
    	Message source = MessageFactory.getFactory().getMessage("EAI_TSTXML_REQ1");
        source.setData(data);

        System.out.println("source="+source);

        //----------------------------------------------------------------------
        // 변환 방식 1
        //----------------------------------------------------------------------
        Transformer transformer = engine.getTransformer();
        Message target = transformer.transform("TRANSFORM_EAI_TSTXML2JSON_REQ", new Message[] { source });

        System.out.println(target.toString());
        System.out.println(target.getData());
        
    }
    public void atestAscToJson() throws Exception {
  		System.out.println("Asc TO Json ==============================================================");
        //logger.debug("Transform08900001Test | testTransform()");

        //----------------------------------------------------------------------
        // source message 생성
        //----------------------------------------------------------------------
    	String data ="";
    	data = "한글      ABCD1234  한글      ";
    	System.out.println(data);
    	Message source = MessageFactory.getFactory().getMessage("EAI_TSTASC_REQ1");
        source.setData(data.getBytes());

        System.out.println("source="+source);

        //----------------------------------------------------------------------
        // 변환 방식 1
        //----------------------------------------------------------------------
        Transformer transformer = engine.getTransformer();
        Message target = transformer.transform("TRANSFORM_EAI_TSTASC2JSON_REQ", new Message[] { source });

        System.out.println(target.toString());
        System.out.println(target.getData());
        
    }
    public void testEbcdicToJson() throws Exception {
    	System.out.println("Ebcdic TO Json ==============================================================");
    	//logger.debug("Transform08900001Test | testTransform()");
    	
    	//----------------------------------------------------------------------
    	// source message 생성
    	//----------------------------------------------------------------------
    	String data ="";
    	data = "HANGLE    ABCD1234  HANGLE    ";
    	byte[] d = IConv.asciiToEBCDIC(data);
    	System.out.println(new String(d));
    	Message source = MessageFactory.getFactory().getMessage("EAI_TSTEBCDIC_REQ1");
    	source.setData(d);
    	
    	System.out.println("source="+source);
    	
    	//----------------------------------------------------------------------
    	// 변환 방식 1
    	//----------------------------------------------------------------------
    	Transformer transformer = engine.getTransformer();
    	Message target = transformer.transform("TRANSFORM_EAI_TSTEBCDIC2JSON_REQ", new Message[] { source });
    	
    	System.out.println(target.toString());
    	System.out.println(target.getData());
    	
    }
    
    
    public void atestJsonToXml() throws Exception {
    	System.out.println("Json TO Xml ==============================================================");
    	
    	//----------------------------------------------------------------------
    	// source message 생성
    	//----------------------------------------------------------------------
    	String data ="";
    	data = "{\"EAI_TSTJSON_REQ1\": {\"GROUP\": {\"HANGLE\":\"한글\",\"TEXT\":\"ABCD1234\" ,\"BASE\":\"한글\" }}}";
    	
    	System.out.println(data);
    	Message source = MessageFactory.getFactory().getMessage("EAI_TSTJSON_REQ1");
    	
    	source.setData(data);
    	
    	System.out.println(source.toString());
    	
    	//----------------------------------------------------------------------
    	// 변환 방식 1
    	//----------------------------------------------------------------------
    	Transformer transformer = engine.getTransformer();
    	Message target = transformer.transform("TRANSFORM_EAI_TSTJSON2XML_REQ", new Message[] { source });
    	System.out.println(target.toString());
    	System.out.println(target.getData());
    	
    }
    public void testJsonToAsc() throws Exception {
    	System.out.println("Json TO Asc ==============================================================");
    	
    	//----------------------------------------------------------------------
    	// source message 생성
    	//----------------------------------------------------------------------
    	String data ="";
       	data = "{\"EAI_TSTJSON_REQ1\": {\"GROUP\": [{\"HANGLE\":\"한글\",\"TEXT\":\"ABCD1234\" ,\"BASE\":\"한글\" },{\"HANGLE\":\"한글\",\"TEXT\":\"ABCD1234\" ,\"BASE\":\"한글\" }]}}";
   	
       	System.out.println(data);
    	Message source = MessageFactory.getFactory().getMessage("EAI_TSTJSON_REQ1");
    	
    	source.setData(data);
    	
    	System.out.println(source.toString());
    	
    	//----------------------------------------------------------------------
    	// 변환 방식 1
    	//----------------------------------------------------------------------
    	Transformer transformer = engine.getTransformer();
    	Message target = transformer.transform("TRANSFORM_EAI_TSTJSON2ASC_REQ", new Message[] { source });
    	System.out.println(target.toString());
    	System.out.println(new String((byte[])target.getData()));
    	
    }    
    public void atestJsonToEbcdic() throws Exception {
    	System.out.println("Json TO Ebcdic ==============================================================");
    	
    	//----------------------------------------------------------------------
    	// source message 생성
    	//----------------------------------------------------------------------
    	String data ="";
       	data = "{\"EAI_TSTJSON_REQ1\": {\"GROUP\": {\"HANGLE\":\"한글\",\"TEXT\":\"ABCD1234\" ,\"BASE\":\"한글\" }}}";
    	
    	System.out.println(data);
    	Message source = MessageFactory.getFactory().getMessage("EAI_TSTJSON_REQ1");
    	
    	source.setData(data);
    	
    	System.out.println(source.toString());
    	
    	//----------------------------------------------------------------------
    	// 변환 방식 1
    	//----------------------------------------------------------------------
    	Transformer transformer = engine.getTransformer();
    	Message target = transformer.transform("TRANSFORM_EAI_TSTJSON2EBCDIC_REQ", new Message[] { source });
    	System.out.println(target.toString());
    	System.out.println(new String((byte[])target.getData()));
    	
    }   
    
    
}
