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.controller.netconf.nettyutil.handler;
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
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.Arrays;
20 import java.util.List;
21 import org.custommonkey.xmlunit.XMLUnit;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.controller.netconf.api.NetconfMessage;
25 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
26 import org.openexi.proc.common.EXIOptions;
27 import org.openexi.proc.common.EXIOptionsException;
28 import org.openexi.sax.Transmogrifier;
29 import org.openexi.sax.TransmogrifierException;
30 import org.xml.sax.InputSource;
32 public class NetconfEXIHandlersTest {
34 private final String msgAsString = "<netconf-message/>";
35 private NetconfMessageToEXIEncoder netconfMessageToEXIEncoder;
36 private NetconfEXIToMessageDecoder netconfEXIToMessageDecoder;
37 private NetconfMessage msg;
38 private byte[] msgAsExi;
41 public void setUp() throws Exception {
42 final NetconfEXICodec codec = new NetconfEXICodec(new EXIOptions());
43 netconfMessageToEXIEncoder = NetconfMessageToEXIEncoder.create(codec);
44 netconfEXIToMessageDecoder = new NetconfEXIToMessageDecoder(codec);
46 msg = new NetconfMessage(XmlUtil.readXmlToDocument(msgAsString));
47 this.msgAsExi = msgToExi(msgAsString, codec);
50 private byte[] msgToExi(final String msgAsString, final NetconfEXICodec codec) throws EXIOptionsException, TransmogrifierException, IOException {
51 final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
52 final Transmogrifier transmogrifier = codec.getTransmogrifier();
53 transmogrifier.setOutputStream(byteArrayOutputStream);
54 transmogrifier.encode(new InputSource(new ByteArrayInputStream(msgAsString.getBytes())));
55 return byteArrayOutputStream.toByteArray();
59 public void testEncodeDecode() throws Exception {
60 final ByteBuf buffer = Unpooled.buffer();
61 netconfMessageToEXIEncoder.encode(null, msg, buffer);
62 final int exiLength = msgAsExi.length;
63 // array from buffer is cca 256 n length, compare only subarray
64 assertArrayEquals(msgAsExi, Arrays.copyOfRange(buffer.array(), 0, exiLength));
66 // assert all other bytes in buffer be 0
67 for (int i = exiLength; i < buffer.array().length; i++) {
68 assertEquals((byte)0, buffer.array()[i]);
71 final List<Object> out = Lists.newArrayList();
72 netconfEXIToMessageDecoder.decode(null, buffer, out);
74 XMLUnit.compareXML(msg.getDocument(), ((NetconfMessage) out.get(0)).getDocument());