package cz.moneta.test.harness.tests.support.xml;

import cz.moneta.test.harness.support.xml.XmlUtils;
import org.apache.commons.lang3.tuple.Triple;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertTrue;

@SuppressWarnings("unchecked")
class XmlUtilsTest {

    private static final String EXPECTED_RESULT = "<?xml version=\"1.0\" encoding=\"UTF-8\" " +
            "standalone=\"no\"?><parent_document>\n" +
            "    <some_node>xaxa</some_node>\n" +
            "    <some_node>replacement 1</some_node>\n" +
            "    <some_node>xaxa</some_node>\n" +
            "    <parents_parent_node>\n" +
            "        <some_node>replacement 1</some_node>\n" +
            "        <some_node>xaxa</some_node>\n" +
            "        <parent_node>\n" +
            "            <some_node>replacement 2</some_node>\n" +
            "            <some_node>replacement 2</some_node>\n" +
            "        <child_node>\n" +
            "    <some_node>child 1 replacement 1</some_node>\n" +
            "    <some_node>fsd</some_node>\n" +
            "    <some_node>\n" +
            "        <some_node>fsd</some_node>\n" +
            "        <some_node>\n" +
            "            <some_node>child 1 replacement 1</some_node>\n" +
            "            <some_node>child 1 replacement 2</some_node>\n" +
            "        </some_node>\n" +
            "    </some_node>\n" +
            "</child_node><child_node>\n" +
            "    <some_node>child 2 replacement 1</some_node>\n" +
            "    <some_node>fsd</some_node>\n" +
            "    <some_node>\n" +
            "        <some_node>fsd</some_node>\n" +
            "        <some_node>\n" +
            "            <some_node>child 2 replacement 1</some_node>\n" +
            "            <some_node>child 2 replacement 2</some_node>\n" +
            "        </some_node>\n" +
            "    </some_node>\n" +
            "</child_node><child_node>\n" +
            "    <some_node>child 3 replacement 1</some_node>\n" +
            "    <some_node>fsd</some_node>\n" +
            "    <some_node>\n" +
            "        <some_node>fsd</some_node>\n" +
            "        <some_node>\n" +
            "            <some_node>child 3 replacement 1</some_node>\n" +
            "            <some_node>child 3 replacement 2</some_node>\n" +
            "        </some_node>\n" +
            "    </some_node>\n" +
            "</child_node></parent_node>\n" +
            "        <some_node>replacement 2</some_node>\n" +
            "    </parents_parent_node>\n" +
            "    <some_node>xaxa</some_node>\n" +
            "</parent_document>";

    @Test
    void testXmlTemplateResolving() {
        Map<String, String> parentReplacements = new HashMap<String, String>() {{
            put("#{placeholder 1}", "replacement 1");
            put("#{placeholder 2}", "replacement 2");
        }};
        Triple<String, String, Map<String, String>>[] children = Stream.of("child 1", "child 2", "child 3")
                .map(child -> Triple.of("parent_node", "xml/child_node.xml", new HashMap<String, String>() {{
                    put("#{placeholder 1}", child + " replacement 1");
                    put("#{placeholder 2}", child + " replacement 2");
                }}))
                .toArray(Triple[]::new);

        String result = XmlUtils.resolveTemplate("xml/parent_node.xml", parentReplacements, children);

        assertTrue(XmlUtils.documentsEqual(result, EXPECTED_RESULT));
    }

}