BUG-1521 Netconf-netty-util missing unit tests
[controller.git] / opendaylight / netconf / netconf-netty-util / src / test / java / org / opendaylight / controller / 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.controller.netconf.nettyutil.handler;
10
11 import static org.junit.Assert.*;
12
13 import com.google.common.collect.Lists;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.io.ByteArrayInputStream;
17 import java.io.ByteArrayOutputStream;
18 import java.io.IOException;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
22 import org.custommonkey.xmlunit.XMLUnit;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.controller.netconf.api.NetconfMessage;
26 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
27 import org.openexi.proc.common.EXIOptions;
28 import org.openexi.proc.common.EXIOptionsException;
29 import org.openexi.sax.Transmogrifier;
30 import org.openexi.sax.TransmogrifierException;
31 import org.xml.sax.InputSource;
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 = new NetconfEXICodec(new EXIOptions());
44         netconfMessageToEXIEncoder = new NetconfMessageToEXIEncoder(codec);
45         netconfEXIToMessageDecoder = new NetconfEXIToMessageDecoder(codec);
46
47         msg = new NetconfMessage(XmlUtil.readXmlToDocument(msgAsString));
48         this.msgAsExi = msgToExi(msgAsString, codec);
49     }
50
51     private byte[] msgToExi(final String msgAsString, final NetconfEXICodec codec) throws EXIOptionsException, TransmogrifierException, IOException {
52         final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
53         final Transmogrifier transmogrifier = codec.getTransmogrifier();
54         transmogrifier.setOutputStream(byteArrayOutputStream);
55         transmogrifier.encode(new InputSource(new ByteArrayInputStream(msgAsString.getBytes())));
56         return byteArrayOutputStream.toByteArray();
57     }
58
59     @Test
60     public void testEncodeDecode() throws Exception {
61         final ByteBuf buffer = Unpooled.buffer();
62         netconfMessageToEXIEncoder.encode(null, msg, buffer);
63         final int exiLength = msgAsExi.length;
64         // array from buffer is cca 256 n length, compare only subarray
65         assertArrayEquals(msgAsExi, Arrays.copyOfRange(buffer.array(), 0, exiLength));
66
67         // assert all other bytes in buffer be 0
68         for (int i = exiLength; i < buffer.array().length; i++) {
69             assertEquals((byte)0, buffer.array()[i]);
70         }
71
72         final List<Object> out = Lists.newArrayList();
73         netconfEXIToMessageDecoder.decode(null, buffer, out);
74
75         XMLUnit.compareXML(msg.getDocument(), ((NetconfMessage) out.get(0)).getDocument());
76     }
77 }