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 com.siemens.ct.exi.api.sax.SAXEncoder;
16 import com.siemens.ct.exi.exceptions.EXIException;
17 import io.netty.buffer.ByteBuf;
18 import io.netty.buffer.Unpooled;
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.util.Arrays;
22 import java.util.List;
23 import javax.xml.transform.TransformerException;
24 import javax.xml.transform.dom.DOMSource;
25 import javax.xml.transform.sax.SAXResult;
26 import org.custommonkey.xmlunit.XMLUnit;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.opendaylight.netconf.api.NetconfMessage;
30 import org.opendaylight.netconf.api.xml.XmlUtil;
31 import org.opendaylight.netconf.nettyutil.handler.exi.EXIParameters;
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 = NetconfEXICodec.forParameters(EXIParameters.empty());
44 netconfMessageToEXIEncoder = NetconfMessageToEXIEncoder.create(codec);
45 netconfEXIToMessageDecoder = NetconfEXIToMessageDecoder.create(codec);
47 msg = new NetconfMessage(XmlUtil.readXmlToDocument(msgAsString));
48 this.msgAsExi = msgToExi(msg, codec);
51 private static byte[] msgToExi(final NetconfMessage msg, final NetconfEXICodec codec)
52 throws IOException, EXIException, TransformerException {
53 final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
54 final SAXEncoder encoder = codec.getWriter();
55 encoder.setOutputStream(byteArrayOutputStream);
56 ThreadLocalTransformers.getDefaultTransformer().transform(new DOMSource(msg.getDocument()),
57 new SAXResult(encoder));
58 return byteArrayOutputStream.toByteArray();
62 public void testEncodeDecode() throws Exception {
63 final ByteBuf buffer = Unpooled.buffer();
64 netconfMessageToEXIEncoder.encode(null, msg, buffer);
65 final int exiLength = msgAsExi.length;
66 // array from buffer is cca 256 n length, compare only subarray
67 assertArrayEquals(msgAsExi, Arrays.copyOfRange(buffer.array(), 0, exiLength));
69 // assert all other bytes in buffer be 0
70 for (int i = exiLength; i < buffer.array().length; i++) {
71 assertEquals((byte)0, buffer.array()[i]);
74 final List<Object> out = Lists.newArrayList();
75 netconfEXIToMessageDecoder.decode(null, buffer, out);
77 XMLUnit.compareXML(msg.getDocument(), ((NetconfMessage) out.get(0)).getDocument());