package com.eactive.eai.transformer;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import com.eactive.eai.common.util.Logger;
import com.eactive.eai.transformer.engine.TransformResult;
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;

class TransformXmlVarGridTest extends TransformExcelLoadConfiguration {

	protected static Logger logger = Logger.getLogger("transformer");

	@BeforeAll
	public static void props() {
		if (System.getProperty("transformer.rule.dir") == null) {
			System.setProperty("transformer.rule.dir", "src/test/resources/com/eactive/eai/transformer");
		}
	}

	@Test
	void testTransformXMLtoFLAT() throws Exception {
		long t = System.currentTimeMillis();

		System.out.println(
				"\n\n<<================================ testTransformXMLtoFLAT ================================>> \n");

		Message source = MessageFactory.getFactory().getMessage("XML_VARGRID_REQ1");
		Message target = MessageFactory.getFactory().getMessage("FLAT_VARGRID_REQ1");

		System.out.println("\nSOURCE  : \n" + source);

		// @formatter:off
		String value1 = "grid1val1";
        String reqXML = ""
        		+ "<XML_VARGRID_REQ1>"
        		+ "<Group>"
        		+ "<value1>" + value1 + "</value1>"
        		+ "<value2>12</value2>"
        		+ "<value3>13</value3>"
        		+ "</Group>"
        		+ "<Group>"
        		+ "<value1>grid2val1</value1>"
        		+ "</Group>"
        		+ "<end>endVal</end>"
        		+ "</XML_VARGRID_REQ1>"
        		;
        // @formatter:on

		// when
		System.out.println("\nSOURCE XML STR : " + reqXML);
		int iter = 1;
		TransformResult transformResult = null;
		for (int i = 0; i < iter; i++) {
			source.setData(reqXML);
			System.out.println("\nSOURCE  : \n" + source);
			System.out.println("source.toLogString:\n" + source.toLogString());
			System.out.println("TARGET    : \n" + target);

			System.out.println("\nTransform Start : \n");

			Transformer transformer = engine.getTransformer();
			transformResult = transformer.transform("TRANSFORM_VARGRID_REQ", target, new Message[] { source });

			assertThat(transformResult).isNotNull().extracting(TransformResult::hasWarning).isEqualTo(false);
			assertThat(target.toLogString()).contains(value1);

			System.out.println("\nSOURCE  : \n" + source);
			System.out.println("TRANSFORM : \n" + TransformManager.getManager().getTransform("TRANSFORM_VARGRID_REQ"));
			System.out.println("TARGET    : \n" + target);
			System.out.println("toLogString:\n" + target.toLogString());
		}
		System.out.println("$$ Transform iter=" + iter + ", Time (ms):" + (System.currentTimeMillis() - t));
	}

}
