Merge "Bug 8153: Enforce check-style rules for netconf - mdsal-netconf-monitoring...
[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.google.common.collect.Lists;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.io.ByteArrayInputStream;
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
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.config.util.xml.XmlUtil;
26 import org.opendaylight.netconf.api.NetconfMessage;
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 = NetconfMessageToEXIEncoder.create(codec);
45         netconfEXIToMessageDecoder = NetconfEXIToMessageDecoder.create(codec);
46
47         msg = new NetconfMessage(XmlUtil.readXmlToDocument(msgAsString));
48         this.msgAsExi = msgToExi(msgAsString, codec);
49     }
50
51     private static byte[] msgToExi(final String msgAsString,final NetconfEXICodec codec)
52             throws EXIOptionsException, TransmogrifierException, IOException {
53         final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
54         final Transmogrifier transmogrifier = codec.getTransmogrifier();
55         transmogrifier.setOutputStream(byteArrayOutputStream);
56         transmogrifier.encode(new InputSource(new ByteArrayInputStream(msgAsString.getBytes())));
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 = Lists.newArrayList();
74         netconfEXIToMessageDecoder.decode(null, buffer, out);
75
76         XMLUnit.compareXML(msg.getDocument(), ((NetconfMessage) out.get(0)).getDocument());
77     }
78 }