package com.huatek.busi.ws.xstream;

import java.time.LocalDateTime;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import com.google.common.collect.Lists;
import com.huatek.busi.ws.protocol.CacAttr;
import com.huatek.busi.ws.protocol.CacDataNode;
import com.huatek.busi.ws.protocol.CacMonitorData;
import com.huatek.busi.ws.protocol.CacRequest;
import com.thoughtworks.xstream.XStream;

public class XStreamTest {
    private String xmlStr;

    @Before
    public void init() {
        // this.xmlStr = toXml();
        this.xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + 
                "<request>\r\n" + 
                "<monitordata cacid=\"26M00090990000987\" datanodenum=\"1\">\r\n" + 
                "<datanode sensorid=\"26M00090990000987\">\r\n" + 
                "<type>021005</type>\r\n" + 
                "<equipmentid>97M00090990000997</equipmentid>\r\n" + 
                "<timestamp>2010-04-10 22:10:11</timestamp>\r\n" + 
                "<attrs>\r\n" + 
                "<attr name=\"Phase \" value=\"A相\" alarm=\"FALSE \" />\r\n" + 
                "<attr name=\"OilTemperature\" value=\"80\" alarm=\"TRUE\"/>\r\n" + 
                "</attrs>\r\n" + 
                "</datanode>\r\n" + 
                "</monitordata>\r\n" + 
                "</request>";
    }

    @Test
    public void toBeanTest() {
        XStream xstream = new XStream();
        // 这种方式有懒加载，反序列化的时候类还没有被“Alias”
        // xstream.autodetectAnnotations(true);
        xstream.processAnnotations(CacRequest.class);

        System.out.println(xmlStr);
        CacRequest request = (CacRequest) xstream.fromXML(xmlStr);

        System.out.println(request);
    }

    @Test
    public void toXmlTest() {
        String xml = toXml();
        System.out.println(xml);
    }

    public static String toXml() {
        XStream xstream = new XStream();
        xstream.processAnnotations(CacRequest.class);
        xstream.addImplicitCollection(CacMonitorData.class, "cacDataNode");
        // xstream.autodetectAnnotations(true);

        CacRequest request = new CacRequest();

        CacMonitorData cacMonitorData = new CacMonitorData();
        cacMonitorData.setCacid("26M00090990000987");
        cacMonitorData.setDataNodeNum(1);

        CacDataNode cacDataNode = new CacDataNode();
        cacDataNode.setSensorId("26M00090990000987");
        cacDataNode.setType("021005");
        cacDataNode.setEquipmentId("97M00090990000997");
        cacDataNode.setTimeStamp(LocalDateTime.now());
        
        CacDataNode cacDataNode2 = new CacDataNode();
        cacDataNode2.setSensorId("26M00090990000987");
        cacDataNode2.setType("021005");
        cacDataNode2.setEquipmentId("97M00090990000997");
        cacDataNode2.setTimeStamp(LocalDateTime.now());

        List<CacAttr> attrs = Lists.newArrayList();
        CacAttr attr1 = new CacAttr();
        CacAttr attr2 = new CacAttr();
        attr1.setName("Phase");
        attr1.setValue("A相");
        attr1.setAlarm("FALSE");
        attr2.setName("OilTemperature");
        attr2.setValue("80");
        attr2.setAlarm("TRUE");
        attrs.add(attr1);
        attrs.add(attr2);

        cacDataNode.setAttrs(attrs);
        cacMonitorData.setCacDataNode(Lists.newArrayList(cacDataNode, cacDataNode2));
        request.setCacMonitorData(cacMonitorData);
        String xml = xstream.toXML(request);
        return xml;
    }
}
