Remove unnecessary check for canceled future
[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.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;
16
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;
26
27 class NetconfXMLToHelloMessageDecoderTest {
28
29     @Test
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);
36
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\""));
44     }
45
46     @Test
47     void testDecodeNoHeader() throws Exception {
48         final ByteBuf src =
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);
52
53         assertEquals(1, out.size());
54         final HelloMessage hello = assertInstanceOf(HelloMessage.class, out.get(0));
55         assertFalse(hello.getAdditionalHeader().isPresent());
56     }
57
58     @Test
59     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
78     void testDecodeNotHelloReceived() {
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         assertThrows(IllegalStateException.class, () -> decoder.decode(null, msg1, out));
84     }
85 }