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
9 package org.opendaylight.controller.netconf.nettyutil.handler;
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;
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;
26 public class NetconfXMLToHelloMessageDecoderTest {
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);
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\""));
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);
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());
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);
66 assertEquals(1, out.size());
68 assertEquals(2, Iterables.size(decoder.getPostHelloNetconfMessages()));
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);