Initial code drop of netconf protocol implementation
[controller.git] / opendaylight / netconf / netconf-impl / src / test / java / org / opendaylight / controller / netconf / impl / MessageParserTest.java
1 /*
2  * Copyright (c) 2013 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.impl;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13
14 import java.io.IOException;
15
16 import javax.xml.parsers.ParserConfigurationException;
17
18 import org.custommonkey.xmlunit.XMLAssert;
19 import org.junit.Before;
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.opendaylight.controller.netconf.api.NetconfMessage;
23 import org.opendaylight.controller.netconf.util.messages.FramingMechanism;
24 import org.opendaylight.controller.netconf.util.messages.NetconfMessageFactory;
25 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
26 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
27 import org.opendaylight.protocol.framework.DeserializerException;
28 import org.opendaylight.protocol.framework.DocumentedException;
29 import org.opendaylight.protocol.util.ByteArray;
30 import org.w3c.dom.Document;
31 import org.xml.sax.SAXException;
32
33 public class MessageParserTest {
34
35     private NetconfMessageFactory parser = null;
36
37     @Before
38     public void setUp() {
39         this.parser = new NetconfMessageFactory();
40     }
41
42     @Test
43     public void testPutEOM() throws IOException, SAXException, ParserConfigurationException {
44         final NetconfMessage msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/client_hello.xml");
45         final byte[] bytes = this.parser.put(msg);
46         assertArrayEquals(NetconfMessageFactory.endOfMessage, ByteArray.subByte(bytes, bytes.length
47                 - NetconfMessageFactory.endOfMessage.length, NetconfMessageFactory.endOfMessage.length));
48     }
49
50     @Ignore
51     @Test
52     // TODO not working on WINDOWS
53     // arrays first differed at element [4]; expected:<49> but was:<53>
54     // at
55     // org.junit.internal.ComparisonCriteria.arrayEquals(ComparisonCriteria.java:52)
56     public void testPutChunk() throws IOException, SAXException, ParserConfigurationException {
57         final NetconfMessage msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/client_hello.xml");
58         this.parser.setFramingMechanism(FramingMechanism.CHUNK);
59         final byte[] bytes = this.parser.put(msg);
60         final byte[] header = new byte[] { (byte) 0x0a, (byte) 0x23, (byte) 0x32, (byte) 0x31, (byte) 0x31, (byte) 0x0a };
61         assertArrayEquals(header, ByteArray.subByte(bytes, 0, header.length));
62         assertArrayEquals(NetconfMessageFactory.endOfChunk, ByteArray.subByte(bytes, bytes.length
63                 - NetconfMessageFactory.endOfChunk.length, NetconfMessageFactory.endOfChunk.length));
64     }
65
66     @Test
67     public void testParseEOM() throws IOException, SAXException, DeserializerException, DocumentedException,
68             ParserConfigurationException {
69         final Document msg = XmlFileLoader.xmlFileToDocument("netconfMessages/client_hello.xml");
70         final byte[] bytes = this.parser.put(new NetconfMessage(msg));
71         final Document doc = this.parser
72                 .parse(ByteArray.subByte(bytes, 0, bytes.length - NetconfMessageFactory.endOfMessage.length))
73                 .iterator().next().getDocument();
74         assertEquals(XmlUtil.toString(msg), XmlUtil.toString(doc));
75         XMLAssert.assertXMLEqual(msg, doc);
76     }
77
78     @Test
79     public void testParseChunk() throws IOException, SAXException, DeserializerException, DocumentedException,
80             ParserConfigurationException {
81         final Document msg = XmlFileLoader.xmlFileToDocument("netconfMessages/client_hello.xml");
82         this.parser.setFramingMechanism(FramingMechanism.CHUNK);
83         final byte[] bytes = this.parser.put(new NetconfMessage(msg));
84         final Document doc = this.parser
85                 .parse(ByteArray.subByte(bytes, 6, bytes.length - NetconfMessageFactory.endOfChunk.length - 6))
86                 .iterator().next().getDocument();
87         assertEquals(XmlUtil.toString(msg), XmlUtil.toString(doc));
88         XMLAssert.assertXMLEqual(msg, doc);
89     }
90
91 }