Move NetconfMessage into netconf.api.messages
[netconf.git] / netconf / netconf-netty-util / src / test / java / org / opendaylight / netconf / nettyutil / handler / NetconfHelloMessageToXMLEncoderTest.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.netconf.nettyutil.handler;
9
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;
14
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;
26
27 @RunWith(MockitoJUnitRunner.StrictStubs.class)
28 public class NetconfHelloMessageToXMLEncoderTest {
29
30     @Mock
31     private ChannelHandlerContext ctx;
32
33     @Test
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);
40
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\"/>"));
44     }
45
46     @Test
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);
52
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\"/>"));
56     }
57
58     @Test
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));
63     }
64 }