package net.northking.cctp.element;

import com.alibaba.fastjson.JSON;
import net.northking.cctp.element.core.IExecuteContext;
import net.northking.cctp.element.test.SampleExecuteContext;
import net.northking.cctp.element.xml.keywords.XML;
import net.northking.cctp.element.core.annotation.Autowired;
import net.northking.cctp.element.test.AbstractLibraryTest;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.testng.annotations.Test;

import java.util.List;

public class XMLLibraryTest extends AbstractLibraryTest {

    @Autowired
    protected XML xml;

    @Test
    public void xml() throws Exception {
//        String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><exam><student idcard=\"111\" "
//                           + "examid=\"222\"><grade>89</grade><location>沈阳</location><name>张三</name></student"
//                           + "><student idcard=\"333\" examid=\"444\"><grade>97</grade><location>大连</location><name"
//                           + "><![CDATA[<李四>]]></name></student></exam>";
//        ParamsBuilder builder = new ParamsBuilder();
//        builder.add("xml", xmlString);
//        Object document = library.runKeyword("parseXml", builder.build());
//
//        builder.add("source", document).add("pretty", true);
//        library.runKeyword("logElement", builder.build());
//
//        //选择节点
//        builder.add("xpath", "/exam/student");
//        Object node = library.runKeyword("getElement", builder.build());
//
//        builder.add("source", node);
//        library.runKeyword("logElement", builder.build());
//
//        //获取节点值
//        builder.add("source", document);
//        builder.add("xpath", "/exam/student/name");
//        Object value = library.runKeyword("getElementText", builder.build());
//        System.out.println(value);
//        Object values = library.runKeyword("getElementTexts", builder.build());
//        System.out.println(JSON.toJSONString(values));
//
//        builder.add("xpath", "/exam/student[2]");
//        builder.add("name", "idcard");
//        value = library.runKeyword("getElementAttribute", builder.build());
//        System.out.println(value);
//        value = library.runKeyword("getElementAttributes", builder.build());
//        System.out.println(JSON.toJSONString(value));
//
//
//        //设置节点值
//        builder.add("source", document);
//        builder.add("xpath", "/exam/student[2]")
//                .add("name", "idcard")
//                .add("value", "3333");
//        library.runKeyword("setElementAttribute", builder.build());
//        builder.add("xpath", "/exam/student[2]/location").add("value", "大连2");
//        library.runKeyword("setElementText", builder.build());
//
//        builder.add("xpath", "/exam/student[2]")
//                .add("name", "ext")
//                .add("value", "abc");
//        library.runKeyword("setElementAttribute", builder.build());
//        String elementXml = "<a idcard=\"555\" examid=\"555\"><grade>100</grade><location>广州</location><name"
//                            + ">王五</name></a>";
//        builder.add("xpath", "/exam/student").add("element", elementXml);
//        library.runKeyword("addElement", builder.build());
//
//        // 删除节点
//        builder.add("xpath", "/exam/student[1]");
//        library.runKeyword("removeElement", builder.build());
//        library.runKeyword("logElement", builder.build());
//
//        builder.add("xpath", "/exam/student");
//        builder.add("name", "ext");
//        library.runKeyword("removeElementAttribute", builder.build());
//        library.runKeyword("logElement", builder.build());
//        library.runKeyword("removeElementAttributes", builder.build());
//        library.runKeyword("logElement", builder.build());
//
//        value = library.runKeyword("getChildElements", builder.build());
//        for (Object object : (List) value) {
//            System.out.println(((Node) object).asXML());
//        }

    }

    @Test
    public void xml1() throws Exception {
//        String xmlString = "<bookstore><book><title lang=\"eng\">Harry Potter</title><price>29"
//                           + ".99</price></book><book><title lang=\"eng\">Learning XML</title><price>39"
//                           + ".95</price></book><test><book><title lang=\"eng\">test</title><price>39"
//                           + ".95</price></book></test></bookstore>";
//        Document document = xml.parseXml(xmlString);
//        xml.logElement(document, "info");
//        System.out.println(xml.getElements(document, "/bookstore/test").size());
//        Element first = xml.getElement(document, "/bookstore/book[1]");
//        System.out.println(first.asXML());
//        xml.assertElementTextEquals(first, "book/title", "test", "");
    }

    @Test
    public void test() throws Exception {
        IExecuteContext build = new SampleExecuteContext.Builder().build();
        //addElement
//        System.out.println(xml.addElement("<root></root>","/root","<c/>","#{aaa}",build));
        //getElement
//        System.out.println(xml.getElement("0","<roo>t><c1></c1></root>","/root/c1","#{aaa}",build));
//        System.out.println(xml.getElement("1","<root id=\"10\"><c1 id=\"10\"></c1><c1 id=\"20\"></c1></root>","/root/c1","#{aaa}",build));
//        System.out.println(xml.getElement("2","<root id=\"10\"><c1 id=\"10\">10</c1><c1 id=\"20\">20</c1></root>","/root/c1","#{aaa}",build));
//        System.out.println(xml.getElement("3","<root id=\"10\"><c1 id=\"10\">10</c1><c1 id=\"20\">20</c1></root>","/root/c1","#{aaa}",build));
//        System.out.println(xml.getElement("4","<root id=\"10\">666<c1 id=\"10\">10</c1><c1 id=\"20\">20</c1></root>","/root/c1","#{aaa}",build));
//        System.out.println(xml.getElement("5","<root id=\"10\">666<c1 id=\"10\">10</c1><c1 id=\"20\">20</c1></root>","/root","#{aaa}",build));
        //setElement
        System.out.println(xml.setElement("0","<root id=\"10\"><c1 id=\"10\">10</c1><c1 id=\"20\">20</c1></root>","/root/c1","null","666","#{aaa}",build));
//        System.out.println(xml.setElement("1","<root id=\"10\">666<c1 id=\"10\">10</c1><c1 id=\"20\">20</c1></root>","/root/c1",null,"666","#{aaa}",build));
//        System.out.println(xml.setElement("2","<root id=\"10\">666<c1 id=\"10\">10</c1><c1 id=\"20\">20</c1></root>","/root/c1","field","666","#{aaa}",build));
//        System.out.println(xml.setElement("3","<root id=\"10\">666<c1 id=\"10\">10</c1><c1 id=\"20\">20</c1></root>","/root/c1","field","666","#{aaa}",build));
        //
//        System.out.println(xml.removeElement("0","<root id=\"10\">666<c1 id=\"10\">10</c1><c1 id=\"20\">20</c1></root>","/root/c1",null,"#{aaa}",build));
//        System.out.println(xml.removeElement("1","<root id=\"10\">666<c1 id=\"10\">10</c1><c1 id=\"20\">20</c1></root>","/root/c1","id","#{aaa}",build));
//        System.out.println(xml.removeElement("2","<root id=\"10\">666<c1 id=\"10\">10</c1><c1 id=\"20\">20</c1></root>","/root/c1",null,"#{aaa}",build));
//        System.out.println(xml.removeElement("3","<root id=\"10\">666<c1 id=\"10\">10</c1><c1 id=\"20\">20</c1></root>","/root/c1","id","#{aaa}",build));
        //
//        System.out.println(xml.parseXml("<root id=\"10\">666<c1 id=\"10\">10</c1><c1 id=\"20\">20</c1></root>","#{aaa}",build));

    }

}