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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertThat;
13 import static org.junit.Assert.assertTrue;
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;
25 public class NetconfXMLToHelloMessageDecoderTest {
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);
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\""));
46 public void testDecodeNoHeader() throws Exception {
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);
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());
59 public void testDecodeCaching() throws Exception {
61 Unpooled.wrappedBuffer("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>".getBytes());
63 Unpooled.wrappedBuffer("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>".getBytes());
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);
72 assertEquals(1, out.size());
74 assertEquals(2, Iterables.size(decoder.getPostHelloNetconfMessages()));
77 @Test(expected = IllegalStateException.class)
78 public void testDecodeNotHelloReceived() throws Exception {
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);