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