package com.eactive.eai.transformer;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

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

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

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

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

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

    public void tearDown() throws Exception {
        //
    }
    
    public void testJsonToFlatTransform2() throws Exception {
  		System.out.println("Json TO Flat ==============================================================");
        //logger.debug("Transform08900001Test | testTransform()");

        //----------------------------------------------------------------------
        // source message 생성
        //----------------------------------------------------------------------
    	String data ="";
    	data = "{\"USER_ID\":\"12345678\",\"USER_PSWD\":\"PASSWORD\"}";

    	
        Message source = MessageFactory.getFactory().getMessage("TST_EUIMCI00000_REQ1");
        
		String rootItemName = ((JSONMessage)source).getLayout().getRootItem().getName();

        JSONObject value = (JSONObject)JSONValue.parse(data);
        JSONObject root = new JSONObject();
        root.put(rootItemName, value);
        data = root.toJSONString();  

        System.out.println("before="+source);
        source.setData(data.getBytes());
        System.out.println("source="+source);

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

        System.out.println(target.toString());
        
    }
//  public void testFlatToJsonTransform2() throws Exception {
//	  		System.out.println("Flat TO Json ==============================================================");
//            //----------------------------------------------------------------------
//            // source message 생성
//            //----------------------------------------------------------------------
//        	String data ="00                                                                                                                    0000100023";
//            Message source = MessageFactory.getFactory().getMessage("TST_CCMMCI0023RS1JBB_REQ2");
//            source.setData(data.getBytes());
////            System.out.println(source);
//    
//            //----------------------------------------------------------------------
//            // 변환 방식 1
//            //----------------------------------------------------------------------
//            Transformer transformer = engine.getTransformer();
//            Message target = transformer.transform("TRANSFORM_TST_CCMMCI9999S1_RES", new Message[] { source });
//    
//            
////            System.out.println(target);
//            System.out.println(target.toString());
//            System.out.println("testFlatToJsonTransform >> [["+target.getData()+"]]");
//            
//        }        
    
    
}
