package com.eactive.eai.transformer;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

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

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

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

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

    public void tearDown() throws Exception {
        //
    }
    
    public static String inputStreamToString(InputStream inputStream) throws IOException {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int length;
        while ((length = inputStream.read(buffer)) != -1) {
            result.write(buffer, 0, length);
        }
        return result.toString("UTF-8");
    }

    public void testTransformXMLtoFlat() throws Exception {
    	System.out.println("\n\n<<================================== testTransformXMLtoFlat ==================================>> \n");
        
    	String reqXML = "";
		try (InputStream is = TransformAggTest.class
				.getResourceAsStream("com/eactive/eai/transformer/TransformAggTest.xml")) {
			reqXML = inputStreamToString(is);
		}
        
        System.out.println(String.format("\nSOURCE XML Size : %,d K (%,d)", reqXML.length() / 1024, reqXML.length()) );
        MessageFactory mf = MessageFactory.getFactory();
        
        long c = System.currentTimeMillis();
        long t = 0;
        long init = c;

        Message source = null;
        
//        source = mf.getMessage("PAK_MDS0002427_P02R");
//        t = System.currentTimeMillis();
//        System.out.println("getMessage source EXECUTE0 TIME(ms) : " + (t -c));
//        c = t;
//        init = c;
        
        source = mf.getMessage("PAK_MDS0002427_P02R");
        t = System.currentTimeMillis();
        System.out.println("getMessage source EXECUTE0 TIME(ms) : " + (t -c));
        c = t;
        
        Message target = mf.getMessage("APM_CBSER0001910_CBSR");
        
        t = System.currentTimeMillis();
        System.out.println("getMessage target EXECUTE1 TIME(ms) : " + (t -c));
        c = t;
        source.setData(reqXML);
        t = System.currentTimeMillis();
        System.out.println("setData source EXECUTE2 TIME(ms) : " + (t -c));
        c = t;
        
//        System.out.println("\nSOURCE  : \n" + source);
//        System.out.println("TARGET    : \n" + target);
//        System.out.println(target.toLogString());
        
//        System.out.println("\nTransform Start : \n");
        
        Transformer transformer = engine.getTransformer();
        transformer.transform("TRANSFORM_APM_CBSER0001909S1_RES", target, new Message[] { source });
        t = System.currentTimeMillis();
        System.out.println("transform EXECUTE3 TIME(ms) : " + (t -c));
        c = t;
//        System.out.println("\nSOURCE  : \n" + source);
//        System.out.println("TRANSFORM : \n" + TransformManager.getManager().getTransform("TRANSFORM_APM_CBSER0001909S1_RES"));
//        System.out.println("TARGET    : \n" + target);
        byte[] result = (byte[])target.getData();
        String res = new String(result);
//        System.out.println("target bytes ["+res+"]");
        t = System.currentTimeMillis();
        System.out.println("getData source EXECUTE2 TIME(ms) : " + (t -c));
        c = t;
        System.out.println(String.format("TOTAL EXECUTE TIME(ms) : %,d", (c - init)) );
    }    
}
