Bump odlparent to 4.0.11
[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
9 package org.opendaylight.netconf.nettyutil.handler;
10
11 import static org.hamcrest.CoreMatchers.containsString;
12 import static org.hamcrest.CoreMatchers.not;
13 import static org.junit.Assert.assertThat;
14
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.netconf.api.NetconfMessage;
23 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
24 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
25 import org.opendaylight.netconf.api.xml.XmlUtil;
26
27 public class NetconfHelloMessageToXMLEncoderTest {
28
29     @Mock
30     private ChannelHandlerContext ctx;
31
32     @Before
33     public void setUp() throws Exception {
34         MockitoAnnotations.initMocks(this);
35     }
36
37     @Test
38     public void testEncode() throws Exception {
39         final NetconfMessage msg = new NetconfHelloMessage(XmlUtil.readXmlToDocument(
40                 "<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"),
41                 NetconfHelloMessageAdditionalHeader.fromString("[tomas;10.0.0.0:10000;tcp;client;]"));
42         final ByteBuf destination = Unpooled.buffer();
43         new NetconfHelloMessageToXMLEncoder().encode(ctx, msg, destination);
44
45         final String encoded = new String(destination.array());
46         assertThat(encoded, containsString("[tomas;10.0.0.0:10000;tcp;client;]"));
47         assertThat(encoded, containsString("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
48     }
49
50     @Test
51     public void testEncodeNoHeader() throws Exception {
52         final NetconfMessage msg = new NetconfHelloMessage(XmlUtil.readXmlToDocument(
53                 "<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
54         final ByteBuf destination = Unpooled.buffer();
55         new NetconfHelloMessageToXMLEncoder().encode(ctx, msg, destination);
56
57         final String encoded = new String(destination.array());
58         assertThat(encoded, not(containsString("[tomas;10.0.0.0:10000;tcp;client;]")));
59         assertThat(encoded, containsString("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
60     }
61
62     @Test(expected = IllegalStateException.class)
63     public void testEncodeNotHello() throws Exception {
64         final NetconfMessage msg = new NetconfMessage(XmlUtil.readXmlToDocument(
65                 "<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
66         new NetconfHelloMessageToXMLEncoder().encode(ctx, msg, null);
67     }
68 }