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.hamcrest.CoreMatchers.containsString;
12 import static org.hamcrest.CoreMatchers.not;
13 import static org.junit.Assert.assertThat;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import io.netty.channel.ChannelHandlerContext;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.controller.netconf.api.NetconfMessage;
23 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
24 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
25 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
27 public class NetconfHelloMessageToXMLEncoderTest {
30 private ChannelHandlerContext ctx;
33 public void setUp() throws Exception {
34 MockitoAnnotations.initMocks(this);
38 public void testEncode() throws Exception {
39 final NetconfMessage msg = new NetconfHelloMessage(XmlUtil.readXmlToDocument("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"),
40 NetconfHelloMessageAdditionalHeader.fromString("[tomas;10.0.0.0:10000;tcp;client;]"));
41 final ByteBuf destination = Unpooled.buffer();
42 new NetconfHelloMessageToXMLEncoder().encode(ctx, msg, destination);
44 final String encoded = new String(destination.array());
45 assertThat(encoded, containsString("[tomas;10.0.0.0:10000;tcp;client;]"));
46 assertThat(encoded, containsString("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
50 public void testEncodeNoHeader() throws Exception {
51 final NetconfMessage msg = new NetconfHelloMessage(XmlUtil.readXmlToDocument("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
52 final ByteBuf destination = Unpooled.buffer();
53 new NetconfHelloMessageToXMLEncoder().encode(ctx, msg, destination);
55 final String encoded = new String(destination.array());
56 assertThat(encoded, not(containsString("[tomas;10.0.0.0:10000;tcp;client;]")));
57 assertThat(encoded, containsString("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
60 @Test(expected = IllegalStateException.class)
61 public void testEncodeNotHello() throws Exception {
62 final NetconfMessage msg = new NetconfMessage(XmlUtil.readXmlToDocument("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
63 new NetconfHelloMessageToXMLEncoder().encode(ctx, msg, null);