2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.nettyutil.handler;
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
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;
33 public class NetconfEXIHandlersTest {
35 private final String msgAsString = "<netconf-message/>";
36 private NetconfMessageToEXIEncoder netconfMessageToEXIEncoder;
37 private NetconfEXIToMessageDecoder netconfEXIToMessageDecoder;
38 private NetconfMessage msg;
39 private byte[] msgAsExi;
42 public void setUp() throws Exception {
43 final NetconfEXICodec codec = new NetconfEXICodec(new EXIOptions());
44 netconfMessageToEXIEncoder = NetconfMessageToEXIEncoder.create(codec);
45 netconfEXIToMessageDecoder = NetconfEXIToMessageDecoder.create(codec);
47 msg = new NetconfMessage(XmlUtil.readXmlToDocument(msgAsString));
48 this.msgAsExi = msgToExi(msgAsString, codec);
51 private static 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();
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));
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]);
72 final List<Object> out = Lists.newArrayList();
73 netconfEXIToMessageDecoder.decode(null, buffer, out);
75 XMLUnit.compareXML(msg.getDocument(), ((NetconfMessage) out.get(0)).getDocument());