Bump exificient to 1.0.1
[netconf.git] / netconf / netconf-netty-util / src / test / java / org / opendaylight / netconf / nettyutil / handler / NetconfEXIHandlersTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.netconf.nettyutil.handler;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13
14 import com.siemens.ct.exi.core.exceptions.EXIException;
15 import com.siemens.ct.exi.main.api.sax.SAXEncoder;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.List;
23 import javax.xml.transform.TransformerException;
24 import javax.xml.transform.dom.DOMSource;
25 import javax.xml.transform.sax.SAXResult;
26 import org.custommonkey.xmlunit.XMLUnit;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.opendaylight.netconf.api.NetconfMessage;
30 import org.opendaylight.netconf.api.xml.XmlUtil;
31 import org.opendaylight.netconf.nettyutil.handler.exi.EXIParameters;
32
33 public class NetconfEXIHandlersTest {
34
35     private final String msgAsString = "<netconf-message/>";
36     private NetconfMessageToEXIEncoder netconfMessageToEXIEncoder;
37     private NetconfEXIToMessageDecoder netconfEXIToMessageDecoder;
38     private NetconfMessage msg;
39     private byte[] msgAsExi;
40
41     @Before
42     public void setUp() throws Exception {
43         final NetconfEXICodec codec = NetconfEXICodec.forParameters(EXIParameters.empty());
44         netconfMessageToEXIEncoder = NetconfMessageToEXIEncoder.create(codec);
45         netconfEXIToMessageDecoder = NetconfEXIToMessageDecoder.create(codec);
46
47         msg = new NetconfMessage(XmlUtil.readXmlToDocument(msgAsString));
48         this.msgAsExi = msgToExi(msg, codec);
49     }
50
51     private static byte[] msgToExi(final NetconfMessage msg, final NetconfEXICodec codec)
52             throws IOException, EXIException, TransformerException {
53         final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
54         final SAXEncoder encoder = codec.getWriter();
55         encoder.setOutputStream(byteArrayOutputStream);
56         ThreadLocalTransformers.getDefaultTransformer().transform(new DOMSource(msg.getDocument()),
57                 new SAXResult(encoder));
58         return byteArrayOutputStream.toByteArray();
59     }
60
61     @Test
62     public void testEncodeDecode() throws Exception {
63         final ByteBuf buffer = Unpooled.buffer();
64         netconfMessageToEXIEncoder.encode(null, msg, buffer);
65         final int exiLength = msgAsExi.length;
66         // array from buffer is cca 256 n length, compare only subarray
67         assertArrayEquals(msgAsExi, Arrays.copyOfRange(buffer.array(), 0, exiLength));
68
69         // assert all other bytes in buffer be 0
70         for (int i = exiLength; i < buffer.array().length; i++) {
71             assertEquals((byte)0, buffer.array()[i]);
72         }
73
74         final List<Object> out = new ArrayList<>();
75         netconfEXIToMessageDecoder.decode(null, buffer, out);
76
77         XMLUnit.compareXML(msg.getDocument(), ((NetconfMessage) out.get(0)).getDocument());
78     }
79 }