Remove unnecessary casting
[netconf.git] / netconf / netconf-netty-util / src / test / java / org / opendaylight / netconf / nettyutil / Netconf539Test.java
1 /*
2  * Copyright (c) 2018 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;
9
10 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
11 import static org.junit.jupiter.api.Assertions.assertNotNull;
12 import static org.opendaylight.netconf.nettyutil.AbstractChannelInitializer.NETCONF_MESSAGE_AGGREGATOR;
13 import static org.opendaylight.netconf.nettyutil.AbstractChannelInitializer.NETCONF_MESSAGE_FRAME_ENCODER;
14
15 import io.netty.channel.ChannelInboundHandlerAdapter;
16 import io.netty.channel.embedded.EmbeddedChannel;
17 import io.netty.util.concurrent.Promise;
18 import java.util.Optional;
19 import java.util.Set;
20 import org.junit.jupiter.api.BeforeEach;
21 import org.junit.jupiter.api.Test;
22 import org.junit.jupiter.api.extension.ExtendWith;
23 import org.mockito.Mock;
24 import org.mockito.junit.jupiter.MockitoExtension;
25 import org.opendaylight.netconf.api.CapabilityURN;
26 import org.opendaylight.netconf.api.NetconfSessionListener;
27 import org.opendaylight.netconf.api.messages.HelloMessage;
28 import org.opendaylight.netconf.common.impl.DefaultNetconfTimer;
29 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
30 import org.opendaylight.netconf.nettyutil.handler.EOMFramingMechanismEncoder;
31 import org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator;
32 import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
33 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
34 import org.opendaylight.netconf.test.util.XmlFileLoader;
35 import org.w3c.dom.Document;
36
37 @ExtendWith(MockitoExtension.class)
38 class Netconf539Test {
39     @Mock
40     private NetconfSessionListener<TestingNetconfSession> listener;
41     @Mock
42     private Promise<TestingNetconfSession> promise;
43
44     private EmbeddedChannel channel;
45     private TestSessionNegotiator negotiator;
46
47     @BeforeEach
48     void setUp() {
49         channel = new EmbeddedChannel();
50         channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER,
51             new ChannelInboundHandlerAdapter());
52         channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER,
53             new NetconfXMLToHelloMessageDecoder());
54         channel.pipeline().addLast(NETCONF_MESSAGE_FRAME_ENCODER, new EOMFramingMechanismEncoder());
55         channel.pipeline().addLast(NETCONF_MESSAGE_AGGREGATOR, new NetconfEOMAggregator());
56         final HelloMessage serverHello = HelloMessage.createClientHello(Set.of(CapabilityURN.BASE_1_1),
57             Optional.empty());
58         negotiator = new TestSessionNegotiator(serverHello, promise, channel, new DefaultNetconfTimer(), listener,
59             100L);
60     }
61
62     @Test
63     void testGetSessionForHelloMessageDefaultNs() throws Exception {
64         testGetSessionForHelloMessage("netconf539/client_hello_1.1.xml");
65     }
66
67     @Test
68     void testGetSessionForHelloMessageNsPrefix() throws Exception {
69         testGetSessionForHelloMessage("netconf539/client_hello_1.1_ns.xml");
70     }
71
72     private void testGetSessionForHelloMessage(final String fileName) throws Exception {
73         final Document helloDocument = XmlFileLoader.xmlFileToDocument(fileName);
74         negotiator.startNegotiation();
75         final HelloMessage helloMessage = new HelloMessage(helloDocument);
76         final TestingNetconfSession session = negotiator.getSessionForHelloMessage(helloMessage);
77         assertNotNull(session);
78         assertInstanceOf(NetconfChunkAggregator.class, channel.pipeline().get(NETCONF_MESSAGE_AGGREGATOR),
79             "NetconfChunkAggregator was not installed in the Netconf pipeline");
80         assertInstanceOf(ChunkedFramingMechanismEncoder.class, channel.pipeline().get(NETCONF_MESSAGE_FRAME_ENCODER),
81             "ChunkedFramingMechanismEncoder was not installed in the Netconf pipeline");
82     }
83 }