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
8 package org.opendaylight.netconf.nettyutil.handler;
10 import static org.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.CoreMatchers.not;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.jupiter.api.Assertions.assertThrows;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import io.netty.channel.ChannelHandlerContext;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.Mock;
21 import org.mockito.junit.MockitoJUnitRunner;
22 import org.opendaylight.netconf.api.messages.HelloMessage;
23 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
24 import org.opendaylight.netconf.api.messages.NetconfMessage;
25 import org.opendaylight.netconf.api.xml.XmlUtil;
27 @RunWith(MockitoJUnitRunner.StrictStubs.class)
28 public class NetconfHelloMessageToXMLEncoderTest {
31 private ChannelHandlerContext ctx;
34 public void testEncode() throws Exception {
35 final NetconfMessage msg = new HelloMessage(XmlUtil.readXmlToDocument(
36 "<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"),
37 NetconfHelloMessageAdditionalHeader.fromString("[tomas;10.0.0.0:10000;tcp;client;]"));
38 final ByteBuf destination = Unpooled.buffer();
39 new NetconfHelloMessageToXMLEncoder().encode(ctx, msg, destination);
41 final String encoded = new String(destination.array());
42 assertThat(encoded, containsString("[tomas;10.0.0.0:10000;tcp;client;]"));
43 assertThat(encoded, containsString("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
47 public void testEncodeNoHeader() throws Exception {
48 final NetconfMessage msg = new HelloMessage(XmlUtil.readXmlToDocument(
49 "<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
50 final ByteBuf destination = Unpooled.buffer();
51 new NetconfHelloMessageToXMLEncoder().encode(ctx, msg, destination);
53 final String encoded = new String(destination.array());
54 assertThat(encoded, not(containsString("[tomas;10.0.0.0:10000;tcp;client;]")));
55 assertThat(encoded, containsString("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
59 public void testEncodeNotHello() throws Exception {
60 final NetconfMessage msg = new NetconfMessage(XmlUtil.readXmlToDocument(
61 "<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
62 assertThrows(IllegalStateException.class, () -> new NetconfHelloMessageToXMLEncoder().encode(ctx, msg, null));