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.hamcrest.MatcherAssert.assertThat;
11 import static org.junit.jupiter.api.Assertions.assertEquals;
12 import static org.junit.jupiter.api.Assertions.assertFalse;
13 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
14 import static org.junit.jupiter.api.Assertions.assertThrows;
15 import static org.junit.jupiter.api.Assertions.assertTrue;
17 import com.google.common.collect.Iterables;
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.Unpooled;
20 import java.util.ArrayList;
21 import java.util.List;
22 import org.hamcrest.CoreMatchers;
23 import org.junit.jupiter.api.Test;
24 import org.opendaylight.netconf.api.messages.HelloMessage;
25 import org.opendaylight.netconf.api.xml.XmlUtil;
27 class NetconfXMLToHelloMessageDecoderTest {
30 void testDecodeWithHeader() throws Exception {
31 final ByteBuf src = Unpooled.wrappedBuffer(String.format("%s\n%s",
32 "[tomas;10.0.0.0:10000;tcp;client;]",
33 "<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>").getBytes());
34 final List<Object> out = new ArrayList<>();
35 new NetconfXMLToHelloMessageDecoder().decode(null, src, out);
37 assertEquals(1, out.size());
38 final HelloMessage hello = assertInstanceOf(HelloMessage.class, out.get(0));
39 assertTrue(hello.getAdditionalHeader().isPresent());
40 assertEquals("[tomas;10.0.0.0:10000;tcp;client;]" + System.lineSeparator(),
41 hello.getAdditionalHeader().orElseThrow().toFormattedString());
42 assertThat(XmlUtil.toString(hello.getDocument()),
43 CoreMatchers.containsString("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\""));
47 void testDecodeNoHeader() throws Exception {
49 Unpooled.wrappedBuffer("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>".getBytes());
50 final List<Object> out = new ArrayList<>();
51 new NetconfXMLToHelloMessageDecoder().decode(null, src, out);
53 assertEquals(1, out.size());
54 final HelloMessage hello = assertInstanceOf(HelloMessage.class, out.get(0));
55 assertFalse(hello.getAdditionalHeader().isPresent());
59 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()));
78 void testDecodeNotHelloReceived() {
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 assertThrows(IllegalStateException.class, () -> decoder.decode(null, msg1, out));