7eebcd5009a7a0fdf6d8ab2b416f88886539e1da
[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.opendaylight.netconf.nettyutil.AbstractChannelInitializer.NETCONF_MESSAGE_AGGREGATOR;
11 import static org.opendaylight.netconf.nettyutil.AbstractChannelInitializer.NETCONF_MESSAGE_FRAME_ENCODER;
12
13 import io.netty.channel.ChannelInboundHandlerAdapter;
14 import io.netty.channel.embedded.EmbeddedChannel;
15 import io.netty.util.HashedWheelTimer;
16 import io.netty.util.concurrent.Promise;
17 import java.util.Collections;
18 import java.util.Optional;
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.netconf.api.NetconfSessionListener;
26 import org.opendaylight.netconf.api.NetconfSessionPreferences;
27 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
28 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
29 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
30 import org.opendaylight.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
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.util.messages.FramingMechanism;
35 import org.opendaylight.netconf.util.test.XmlFileLoader;
36 import org.w3c.dom.Document;
37
38 @RunWith(MockitoJUnitRunner.StrictStubs.class)
39 public class Netconf539Test {
40     @Mock
41     private NetconfSessionListener<TestingNetconfSession> listener;
42     @Mock
43     private Promise<TestingNetconfSession> promise;
44
45     private EmbeddedChannel channel;
46     private AbstractNetconfSessionNegotiator negotiator;
47
48     @Before
49     public void setUp() throws Exception {
50         channel = new EmbeddedChannel();
51         channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER,
52             new ChannelInboundHandlerAdapter());
53         channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER,
54             new NetconfXMLToHelloMessageDecoder());
55         channel.pipeline().addLast(NETCONF_MESSAGE_FRAME_ENCODER,
56             FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM));
57         channel.pipeline().addLast(NETCONF_MESSAGE_AGGREGATOR, new NetconfEOMAggregator());
58         final NetconfHelloMessage serverHello = NetconfHelloMessage.createClientHello(Collections
59             .singleton(XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1), Optional.empty());
60         negotiator = new TestSessionNegotiator(new NetconfSessionPreferences(serverHello), promise, channel,
61             new HashedWheelTimer(), listener, 100L);
62     }
63
64     @Test
65     public void testGetSessionForHelloMessageDefaultNs() throws Exception {
66         testGetSessionForHelloMessage("netconf539/client_hello_1.1.xml");
67     }
68
69     @Test
70     public void testGetSessionForHelloMessageNsPrefix() throws Exception {
71         testGetSessionForHelloMessage("netconf539/client_hello_1.1_ns.xml");
72     }
73
74     private void testGetSessionForHelloMessage(final String fileName) throws Exception {
75         final Document helloDocument = XmlFileLoader.xmlFileToDocument(fileName);
76         negotiator.startNegotiation();
77         final NetconfHelloMessage helloMessage = new NetconfHelloMessage(helloDocument);
78         final AbstractNetconfSession session = negotiator.getSessionForHelloMessage(helloMessage);
79         Assert.assertNotNull(session);
80         Assert.assertTrue("NetconfChunkAggregator was not installed in the Netconf pipeline",
81             channel.pipeline().get(NETCONF_MESSAGE_AGGREGATOR) instanceof NetconfChunkAggregator);
82         Assert.assertTrue("ChunkedFramingMechanismEncoder was not installed in the Netconf pipeline",
83             channel.pipeline().get(NETCONF_MESSAGE_FRAME_ENCODER) instanceof ChunkedFramingMechanismEncoder);
84     }
85 }