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.transform.TransformManager;

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


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

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

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

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

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

    public void tearDown() throws Exception {
        //
    }

    public void testTransformXMLtoJSON() throws Exception {
    	long t = System.currentTimeMillis();
    	printLog("\n\n<<================================== testTransformXMLtoJSON ==================================>> \n");
        Message source = MessageFactory.getFactory().getMessage("CRS72001S0_REQ1");
        Message target = MessageFactory.getFactory().getMessage("CRS72001S0_REQ2");
        
        //  xmlns=\"urn:iso:std:iso:20022:tech:xsd:pain.001.001.05\"
        
        String reqXML = "" 
        		+"<CRS72001S0_REQ1>"
        		+"<Document xmlns=\"urn:iso:std:iso:20022:tech:xsd:pain.001.001.05\">"
        		+"<InData CuniqnoType=\" \" Cuniqno=\"1101110783351\"/>"  // 
        		+"<Amt><InstdAmt Ccy=\"KHR\">30000</InstdAmt></Amt>"
        		+"<AddInfo>test1</AddInfo>"
        		+"<AddInfo>test2</AddInfo>"
        		+"<AddInfo>test3</AddInfo>"
        		+"<Price>100</Price>"
        		+"<Price>200</Price>"
        		+"<Price>300</Price>" 
        		+"<BoolValue>true</BoolValue>"
//        		+"<NullValue></NullValue>"
        		+"</Document>"
        		+"</CRS72001S0_REQ1>"
        		;
        
        printLog("\nSOURCE XML STR : " + reqXML);
        int iter = 1;
        for(int i=0; i<iter; i++) {
	        source.setData(reqXML);
	        printLog("\nSOURCE  : \n" + source);
	        printLog("source.toLogString:\n" + source.toLogString());
	        printLog("TARGET    : \n" + target);
	        
	        printLog("\nTransform Start : \n");
	        
	        Transformer transformer = engine.getTransformer();
	        transformer.transform("TRANSFORM_CRS72001S0_REQ", target, new Message[] { source });
	 
	        printLog("\nSOURCE  : \n" + source);
	        printLog("TRANSFORM : \n" + TransformManager.getManager().getTransform("TRANSFORM_CRS72001S0_REQ"));
	        printLog("TARGET    : \n" + target);
	        printLog("toLogString:\n" + target.toLogString());
        }
        System.out.println("$$ Transform iter="+ iter + ", Time (ms):" +(System.currentTimeMillis() - t) );
        
    }
    
    public void testTransformJSONtoXML() throws Exception {
    	printLog("\n\n<<================================== testTransformJSONtoXML ==================================>> \n");
        Message source = MessageFactory.getFactory().getMessage("CRS72001S0_REQ2");
        Message target = MessageFactory.getFactory().getMessage("CRS72001S0_REQ1");
        
        String reqJson = "{\"CRS72001S0_REQ2\":{\"Document\":{\"NullValue\":null, \"BoolValue\":false,\"AddInfo\":[\"test1\",\"test2\",\"test3\"],\"Price\":[400,500,600],\"Amt\":{\"Ccy\":\"KHR\",\"InstdAmt\":\"30000\"},\"InData\":{\"CuniqnoType\":\"7\",\"Cuniqno\":\"1101110783351\"}}}}";
        reqJson = "{\"Document\":{\"NullValue\":null, \"BoolValue\":false,\"AddInfo\":[\"test1\",\"test2\",\"test3\"],\"Price\":[400,500,600],\"Amt\":{\"Ccy\":\"KHR\",\"InstdAmt\":\"30000\"},\"InData\":{\"CuniqnoType\":\"7\",\"Cuniqno\":\"1101110783351\"}}}";
        reqJson = "{\"Document\":{\"BoolValue\":false,\"AddInfo\":[\"test1\",\"test2\",\"test3\"],\"Price\":[400,500,600],\"Amt\":{\"Ccy\":\"KHR\",\"InstdAmt\":\"30000\"},\"InData\":{\"CuniqnoType\":\"7\",\"Cuniqno\":\"1101110783351\"}}}";
        printLog(reqJson);
        source.setData(reqJson);
        
        printLog("\nSOURCE  : \n" + source);
        printLog(source.toLogString());
        printLog("TARGET    : \n" + target);
                
        printLog("\nTransform Start : \n");
        
        Transformer transformer = engine.getTransformer();
        transformer.transform("TRANSFORM_CRS72001S0_RES", target, new Message[] { source });
 
        printLog("\nSOURCE  : \n" + source);
        printLog(source.toLogString());
        printLog("TRANSFORM : \n" + TransformManager.getManager().getTransform("TRANSFORM_CRS72001S0_RES"));
        printLog("TARGET    : \n" + target);
        printLog(target.toLogString());
    }
    
    public static void printLog(String message) {
    	 System.out.println(message);
    }
}
