Merge "Prevent ssh hang during shutdown"
[netconf.git] / netconf / netconf-netty-util / src / test / java / org / opendaylight / netconf / nettyutil / handler / NetconfXMLToHelloMessageDecoderTest.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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertThat;
13 import static org.junit.Assert.assertTrue;
14
15 import com.google.common.collect.Iterables;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import java.util.ArrayList;
19 import java.util.List;
20 import org.hamcrest.CoreMatchers;
21 import org.junit.Test;
22 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
23 import org.opendaylight.netconf.api.xml.XmlUtil;
24
25 public class NetconfXMLToHelloMessageDecoderTest {
26
27     @Test
28     public void testDecodeWithHeader() throws Exception {
29         final ByteBuf src = Unpooled.wrappedBuffer(String.format("%s\n%s",
30                 "[tomas;10.0.0.0:10000;tcp;client;]",
31                 "<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>").getBytes());
32         final List<Object> out = new ArrayList<>();
33         new NetconfXMLToHelloMessageDecoder().decode(null, src, out);
34
35         assertEquals(1, out.size());
36         assertThat(out.get(0), CoreMatchers.instanceOf(NetconfHelloMessage.class));
37         final NetconfHelloMessage hello = (NetconfHelloMessage) out.get(0);
38         assertTrue(hello.getAdditionalHeader().isPresent());
39         assertEquals("[tomas;10.0.0.0:10000;tcp;client;]" + System.lineSeparator(),
40                 hello.getAdditionalHeader().get().toFormattedString());
41         assertThat(XmlUtil.toString(hello.getDocument()),
42                 CoreMatchers.containsString("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\""));
43     }
44
45     @Test
46     public void testDecodeNoHeader() throws Exception {
47         final ByteBuf src =
48                 Unpooled.wrappedBuffer("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>".getBytes());
49         final List<Object> out = new ArrayList<>();
50         new NetconfXMLToHelloMessageDecoder().decode(null, src, out);
51
52         assertEquals(1, out.size());
53         assertThat(out.get(0), CoreMatchers.instanceOf(NetconfHelloMessage.class));
54         final NetconfHelloMessage hello = (NetconfHelloMessage) out.get(0);
55         assertFalse(hello.getAdditionalHeader().isPresent());
56     }
57
58     @Test
59     public void testDecodeCaching() throws Exception {
60         final ByteBuf msg1 =
61                 Unpooled.wrappedBuffer("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>".getBytes());
62         final ByteBuf msg2 =
63                 Unpooled.wrappedBuffer("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>".getBytes());
64         final ByteBuf src =
65                 Unpooled.wrappedBuffer("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>".getBytes());
66         final List<Object> out = new ArrayList<>();
67         final NetconfXMLToHelloMessageDecoder decoder = new NetconfXMLToHelloMessageDecoder();
68         decoder.decode(null, src, out);
69         decoder.decode(null, msg1, out);
70         decoder.decode(null, msg2, out);
71
72         assertEquals(1, out.size());
73
74         assertEquals(2, Iterables.size(decoder.getPostHelloNetconfMessages()));
75     }
76
77     @Test(expected = IllegalStateException.class)
78     public void testDecodeNotHelloReceived() throws Exception {
79         final ByteBuf msg1 =
80                 Unpooled.wrappedBuffer("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>".getBytes());
81         final List<Object> out = new ArrayList<>();
82         NetconfXMLToHelloMessageDecoder decoder = new NetconfXMLToHelloMessageDecoder();
83         decoder.decode(null, msg1, out);
84     }
85 }