package net.northking.cctp.element;

import net.northking.cctp.element.core.IExecuteContext;
import net.northking.cctp.element.json.keywords.JSON;
import net.northking.cctp.element.core.annotation.Autowired;
import net.northking.cctp.element.test.AbstractLibraryTest;
import net.northking.cctp.element.test.SampleExecuteContext;
import org.testng.annotations.Test;

public class JSONLibraryTest extends AbstractLibraryTest {

    @Autowired
    private JSON json;

    @Test
    public void json() throws Exception {
//        String a = "{\"a\":1,\"b\":[{\"bb\":3443,\"pretty\":null}]}";
//        Object object = json.parseJSON(a);
//        System.out.println(json.toJSONString(object, true));
//
//        Object b = json.getNode(object, "$.b[0]");
//        json.setNode(b, "$.pretty", false);
//        System.out.println(json.toJSONString(b, true));
//
//        String s = "{\"cc\":5555}";
//        ParamsBuilder builder = new ParamsBuilder();
//        builder.add("parent", object);
//        builder.add("jsonPath", "$.b[1]");
//        builder.add("value", s);
//        library.runKeyword("setNode", builder.build());
//
//        json.setNode(object, "$.b[1].cc", 666);
//        System.out.println(json.toJSONString(object, true));
//
//        json.removeNode(object, "$.b[0].bb");
//        System.out.println(json.toJSONString(object, true));
    }

    @Test
    public void jsonPath() throws Exception {
//        Object root = "{\"bookstore\":{\"book\":[{\"title\":{\"lang\":\"eng\",\"text\":\"Harry Potter\"},"
//                      + "\"price\":\"29"
//                      + ".99\"},{\"title\":{\"lang\":\"eng\",\"text\":\"Learning XML\"},\"price\":\"39.95\"}]}}";
//        int size = json.size(root, "$..book.price");
//        System.out.println(size);
//
//        root = json.parseJSON(root.toString());
//        Object result = json.getNode(root, "$.bookstore.book[0].price");
//        System.out.println(json.toJSONString(result, false));


//        Object value = json.getNode(root, "$.bookstore");
//        System.out.println(value);
//        value = json.getNode(root, "$..book");
//        System.out.println(value);
//        System.out.println(value);
//        value = json.getNode(root, "$.bookstore..book");
//        System.out.println(value);
//        value = json.getNode(root, "$.bookstore.book[1]");
//        System.out.println(value);
//        value = json.getNode(root, "$.bookstore.book[price > '35.00']");
//        System.out.println(value);
//        value = json.getNode(root, "$.bookstore.*");
//        System.out.println(value);
    }

    @Test
    public void testJson() throws Exception {
        IExecuteContext context = new SampleExecuteContext.Builder().build();
        Object root = "[{\"username\":111}]";
        root = json.getNode(root, "$[0].username","", context);
        System.out.println(root);
    }

    @Test
    public void test() {
//        json.assertNodeExist("{\"data\":\"ABC\"}","$.data", null);
//        json.assertNodeEqual("{\"data\":\"ABC\"}","$.data","ABC", null);
//        String str = "{\"className\":\"class-1\",\"students\":[{\"name\":\"张三\",\"age\":18}]}";
//        json.assertNodeExist(str,"$.students[0]", null);
//        json.logJSON(str, "INFO");
//        int result = json.size(str, "$.students[0]");
//        System.out.println(result);
    }

    @Test
    public void test1() {
        IExecuteContext build = new SampleExecuteContext.Builder().build();
        //toJSONString
//        System.out.println(json.toJSONString(com.alibaba.fastjson.JSON.parse("2222"), true,"#{ddd}",build));
        //getNode
        System.out.println(json.getNode(com.alibaba.fastjson.JSON.parse("[{\"name\":\"张三\",\"age\":18}]"), "$[0].age","#{ddd}",build));
//        System.out.println(json.getNode(com.alibaba.fastjson.JSON.parse("{\"a\":1,\"b\":[{\"bb\":3443,\"pretty\":null}]}"), "$.a","#{ddd}",build));
        //removeNode
//        System.out.println(json.removeNode("{\"className\":\"class-1\",\"students\":[{\"name\":\"张三\",\"age\":18}]}", "$.className","#{ddd}",build));
        //setNode
//        System.out.println(json.setNode("{\"className\":\"class-1\",\"students\":[{\"name\":\"张三\",\"age\":18}]}", "$.students[2]","{\"name\":\"李四\",\"age\":19}","#{ddd}",build));
    }

    @Test
    public void test2() {
        String str = "{\"className\":\"class-1\",\"students\":[{\"name\":\"张三\",\"age\":18, \"score\":[1,5,6]},{\"name\":\"李四\",\"age\":28, \"score\":[1,2,3]}]}";
        IExecuteContext build = new SampleExecuteContext.Builder().build();
        System.out.println(json.getNumber("{\"className\":\"class-1\",\"students\":[{\"name\":\"张三\",\"age\":18},{\"name\":\"李四\",\"age\":19}]}", "$.students.name", "#{ddd}", build));
    }

}