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

	TransformEngine engine = null;
    private static boolean RUN_TRANSFORM = true;
    
    public TransformJsonMultiGridTest(String name) {
        super(name);
    }

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

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

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

    public void tearDown() throws Exception {
        //
    }

  public void testFlatToJsonTransform() throws Exception {
	  		long cTime = System.currentTimeMillis();
	  		System.out.println("Flat TO Json double grid ==============================================================");
            //----------------------------------------------------------------------
            // source message 생성
            //----------------------------------------------------------------------
        	String data ="030200000000  1111111111333333333344444444440150555555556066666666035155555555616666666652555555556266666666535555555563666666660002001234";
            Message source = MessageFactory.getFactory().getMessage("FLAT_DGRID_REQ1");
            System.out.println("org : " + source);
            
            System.out.println("\n\n>> SET-DATA");
            
            source.setData(data.getBytes());
            System.out.println("set :" + source);
            
                //----------------------------------------------------------------------
	            // 변환 방식 1
	            //----------------------------------------------------------------------
	            Transformer transformer = engine.getTransformer();
	            Message target = transformer.transform("TRANSFORM_DGRIDTEST_RES", new Message[] { source });
	    
	            
//	            System.out.println(target);
	            System.out.println(target.toString());
	            System.out.println("JSON >> ["+target.getData()+"]");
	            
             //----------------------------------------------------------------------
            // 변환 방식 1
            //----------------------------------------------------------------------
            Transformer transformer2 = engine.getTransformer();
            Message target2 = transformer.transform("TRANSFORM_DGRIDTEST_REQ", new Message[] {target });
    
            
//            System.out.println(target2);
            System.out.println(target2.toString());
            System.out.println("FLAT >> ["+new String( (byte[])(target2.getData() ) )+"]");
            
            System.out.println("Tramsform run time (ms): " + (System.currentTimeMillis() - cTime));
            
        }        
    
  public void atestJsonToJsonTransform() throws Exception {
		System.out.println("Json TO Json Multi Grid ==============================================================");
		 logger.debug("TransformJsonMultiGridTest | testJsonToJsonTransform()");
		 long cTime = System.currentTimeMillis();
		// ----------------------------------------------------------------------
		// source message 생성
		// ----------------------------------------------------------------------
		String data = "";
		
	    	data =  "{"
	    			+ "    \"transactionUuid\": \"bf48a92e-a412-4995-b693-82a2cfd445d1\","
	    			+ "    \"originator\": {"
	    			+ "        \"originatorPersons\": ["
	    			+ "            {"
	    			+ "                \"naturalPerson\": {"
	    			+ "                    \"name\": {"
	    			+ "                        \"nameIdentifier\": ["
	    			+ "                            {"
	    			+ "                                \"primaryIdentifier\": \"Smith\""
	    			+ "                            }"
	    			+ "                        ]"
	    			+ "                    },"
	    			+ "                    \"customerIdentification\": \"1002390\""
	    			+ "                }"
	    			+ "            }"
	    			+ "        ],"
	    			+ "        \"accountNumber\": ["
	    			+ "            \"10023909\""
	    			+ "        ]"
	    			+ "    }"
	    			+ "    ,\"errorGroup\": {"
	    			+ "        \"testField\": "
	    			+ "            \"errorCode\""
	    			+ "        }"
	    			+ "}";


//		data = "{}";
		
		Message source = MessageFactory.getFactory().getMessage("JSON_MGRID_REQ1");
	
		String rootItemName = ((JSONMessage) source).getLayout().getRootItem().getName();
		
		System.out.println(source);
		
		System.out.println("Source text Data : " + data);
		
//		JSONObject value = (JSONObject) JSONValue.parse(data);
//		JSONObject root = new JSONObject();
//		root.put(rootItemName, value);
//		data = root.toJSONString();
		
		System.out.println(data);
		source.setData(data.getBytes());
		
		System.out.println(source);
		System.out.println(source.getData());
		if(RUN_TRANSFORM) {
			Transformer transformer = engine.getTransformer();
			Message target = transformer.transform("TRANSFORM_MGRIDTEST_REQ", new Message[] { source });
		      
			System.out.println(target);
	      
			System.out.println(target.getData());
		}
		System.out.println("Tramsform run time (ms): " + (System.currentTimeMillis() - cTime));
	}
    }
