Shade exificient
[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 package org.opendaylight.netconf.nettyutil.handler;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.io.ByteArrayOutputStream;
16 import java.io.IOException;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
20 import javax.xml.transform.TransformerException;
21 import javax.xml.transform.dom.DOMSource;
22 import javax.xml.transform.sax.SAXResult;
23 import org.custommonkey.xmlunit.XMLUnit;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.netconf.api.NetconfMessage;
27 import org.opendaylight.netconf.api.xml.XmlUtil;
28 import org.opendaylight.netconf.nettyutil.handler.exi.EXIParameters;
29 import org.opendaylight.netconf.shaded.exificient.core.exceptions.EXIException;
30 import org.opendaylight.netconf.shaded.exificient.main.api.sax.SAXEncoder;
31
32 public class NetconfEXIHandlersTest {
33
34     private final String msgAsString = "<netconf-message/>";
35     private NetconfMessageToEXIEncoder netconfMessageToEXIEncoder;
36     private NetconfEXIToMessageDecoder netconfEXIToMessageDecoder;
37     private NetconfMessage msg;
38     private byte[] msgAsExi;
39
40     @Before
41     public void setUp() throws Exception {
42         final NetconfEXICodec codec = NetconfEXICodec.forParameters(EXIParameters.empty());
43         netconfMessageToEXIEncoder = NetconfMessageToEXIEncoder.create(codec);
44         netconfEXIToMessageDecoder = NetconfEXIToMessageDecoder.create(codec);
45
46         msg = new NetconfMessage(XmlUtil.readXmlToDocument(msgAsString));
47         this.msgAsExi = msgToExi(msg, codec);
48     }
49
50     private static byte[] msgToExi(final NetconfMessage msg, final NetconfEXICodec codec)
51             throws IOException, EXIException, TransformerException {
52         final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
53         final SAXEncoder encoder = codec.getWriter();
54         encoder.setOutputStream(byteArrayOutputStream);
55         ThreadLocalTransformers.getDefaultTransformer().transform(new DOMSource(msg.getDocument()),
56                 new SAXResult(encoder));
57         return byteArrayOutputStream.toByteArray();
58     }
59
60     @Test
61     public void testEncodeDecode() throws Exception {
62         final ByteBuf buffer = Unpooled.buffer();
63         netconfMessageToEXIEncoder.encode(null, msg, buffer);
64         final int exiLength = msgAsExi.length;
65         // array from buffer is cca 256 n length, compare only subarray
66         assertArrayEquals(msgAsExi, Arrays.copyOfRange(buffer.array(), 0, exiLength));
67
68         // assert all other bytes in buffer be 0
69         for (int i = exiLength; i < buffer.array().length; i++) {
70             assertEquals((byte)0, buffer.array()[i]);
71         }
72
73         final List<Object> out = new ArrayList<>();
74         netconfEXIToMessageDecoder.decode(null, buffer, out);
75
76         XMLUnit.compareXML(msg.getDocument(), ((NetconfMessage) out.get(0)).getDocument());
77     }
78 }