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

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

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

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

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

    public void tearDown() throws Exception {
        //
    }

    public void testJsonGridTransform() throws Exception {
		System.out.println("testJsonGridTransform ==============================================================");
		 logger.debug("TransformJsonDoubleGridTest | testJsonGrid3Transform()");
	
		// ----------------------------------------------------------------------
		// source message 생성
		// ----------------------------------------------------------------------
		String data = "";
		data = "{ " 
						+ "    \"head\": { " 
						+ "        \"contentType\": [ " 
						+ "            \"application/json;charset=UTF-8\" " 
						+ "        ] " 
						+ "    }, " 
						+ "    \"common\": { " 
						+ "        \"serviceID\": \"USER-5100\", " 
						+ "        \"languageCode\": \"en\", " 
						+ "        \"authToken\": \"\" " 
						+ "    }, " 
						+ "    \"data\": { " 
						+ "        \"clientListCnt\": 2, " 
						+ "        \"clientList\": [ " 
						+ "            { " 
						+ "                \"userName\": \"CHOU Srey Mao\", " 
						+ "                \"phoneNumber\": \"0967225506\", " 
						+ "                \"customerNo\": \"0009504\", " 
						+ "                \"dateOfBirth\": \"19820305\", " 
						+ "                \"detailAddress\": \"#01 phsa phnom den takeo\", " 
						+ "                \"idNumber\": \"030767098\", " 
						+ "                \"loanListCnt\": 4, " 
						+ "                \"loanList\": [ " 
						+ "                    { " 
						+ "                        \"loanProductCode\": \"2020202018221\", " 
						+ "                        \"loanBalance\": \"20000.00\", " 
						+ "                        \"lastTransactionDate\": \"20220203\" " 
						+ "                    }, " 
						+ "                    { " 
						+ "                        \"loanProductCode\": \"2020101018221\", " 
						+ "                        \"loanBalance\": \"0.00\", " 
						+ "                        \"lastTransactionDate\": \"20220203\" " 
						+ "                    }, " 
						+ "                    { " 
						+ "                        \"loanProductCode\": \"2020505018221\", " 
						+ "                        \"loanBalance\": \"10000.00\", " 
						+ "                        \"lastTransactionDate\": \"20220203\" " 
						+ "                    }, " 
						+ "                    { " 
						+ "                        \"loanProductCode\": \"2020101018221\", " 
						+ "                        \"loanBalance\": \"10000.00\", " 
						+ "                        \"lastTransactionDate\": \"20220203\" " 
						+ "                    } " 
						+ "                ] " 
						+ "            }, " 
						+ "            { " 
						+ "                \"userName\": \"CHOU Sreyol\", " 
						+ "                \"phoneNumber\": \"0880000000\", " 
						+ "                \"customerNo\": \"0009505\", " 
						+ "                \"dateOfBirth\": \"19850502\", " 
						+ "                \"detailAddress\": \"#01a tarong bati takeo cambodia\", " 
						+ "                \"idNumber\": \"030766897\", " 
						+ "                \"loanListCnt\": 1, " 
						+ "                \"loanList\": [ " 
						+ "                    { " 
						+ "                        \"loanProductCode\": \"2020101018221\", " 
						+ "                        \"loanBalance\": \"0.00\", " 
						+ "                        \"lastTransactionDate\": \"20220203\" " 
						+ "                    } " 
						+ "                ] " 
						+ "            } " 
						+ "        ] " 
						+ "    }, " 
						+ "    \"statusCode\": \"200\", " 
						+ "    \"statusName\": \"OK\", " 
						+ "    \"message\": \"Success\", " 
						+ "    \"success\": \"true\" " 
						+ "}";
		
		Message source = MessageFactory.getFactory().getMessage("JSON_MIXED_REQ1");
	
//		String rootItemName = ((JSONMessage) source).getLayout().getRootItem().getName();
		
		System.out.println(source);
		
//		JSONObject value = (JSONObject) JSONValue.parse(data);
//		JSONObject root = new JSONObject();
//		root.put(rootItemName, value);
//		data = root.toJSONString();
		
		System.out.println(data);
		source.setData(data);
		
		System.out.println(source);
		System.out.println(source.getData());
		if(RUN_TRANSFORM) {
			Transformer transformer = engine.getTransformer();
			Message target = transformer.transform("TRANSFORM_JSONMIXEDTEST_REQ", new Message[] { source });
		      
			System.out.println(target);
	      
			System.out.println("target bytes ["+target.getData()+"]");
		}
	}
    
        
}
