53fe8e6cdadff03cb983bf16abe52fb5263e6766
[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
9 package org.opendaylight.netconf.nettyutil;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.doReturn;
13 import static org.opendaylight.netconf.nettyutil.AbstractChannelInitializer.NETCONF_MESSAGE_AGGREGATOR;
14 import static org.opendaylight.netconf.nettyutil.AbstractChannelInitializer.NETCONF_MESSAGE_FRAME_ENCODER;
15
16 import com.google.common.base.Optional;
17 import io.netty.channel.Channel;
18 import io.netty.channel.ChannelInboundHandlerAdapter;
19 import io.netty.channel.embedded.EmbeddedChannel;
20 import io.netty.util.HashedWheelTimer;
21 import io.netty.util.Timer;
22 import io.netty.util.concurrent.Promise;
23 import java.util.Collections;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.opendaylight.netconf.api.NetconfDocumentedException;
30 import org.opendaylight.netconf.api.NetconfSessionListener;
31 import org.opendaylight.netconf.api.NetconfSessionPreferences;
32 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
33 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
34 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
35 import org.opendaylight.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
36 import org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator;
37 import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
38 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
39 import org.opendaylight.netconf.util.messages.FramingMechanism;
40 import org.opendaylight.netconf.util.test.XmlFileLoader;
41 import org.w3c.dom.Document;
42
43 public class Netconf539Test {
44     @Mock
45     private NetconfSessionListener<TestingNetconfSession> listener;
46     @Mock
47     private Promise<TestingNetconfSession> promise;
48
49     private EmbeddedChannel channel;
50     private AbstractNetconfSessionNegotiator negotiator;
51     private NetconfSessionPreferences prefs;
52
53     @Before
54     public void setUp() throws Exception {
55         MockitoAnnotations.initMocks(this);
56         channel = new EmbeddedChannel();
57         channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER,
58             new ChannelInboundHandlerAdapter());
59         channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER,
60             new NetconfXMLToHelloMessageDecoder());
61         channel.pipeline().addLast(NETCONF_MESSAGE_FRAME_ENCODER,
62             FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM));
63         channel.pipeline().addLast(NETCONF_MESSAGE_AGGREGATOR, new NetconfEOMAggregator());
64         final NetconfHelloMessage serverHello = NetconfHelloMessage.createClientHello(Collections
65             .singleton(XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1), Optional.absent());
66         doReturn(promise).when(promise).setFailure(any());
67         doReturn(promise).when(promise).setSuccess(any());
68         negotiator = new TestSessionNegotiator(new NetconfSessionPreferences(serverHello), promise, channel,
69             new HashedWheelTimer(), listener, 100L);
70     }
71
72     @Test
73     public void testGetSessionForHelloMessageDefaultNs() throws Exception {
74         testGetSessionForHelloMessage("netconf539/client_hello_1.1.xml");
75     }
76
77     @Test
78     public void testGetSessionForHelloMessageNsPrefix() throws Exception {
79         testGetSessionForHelloMessage("netconf539/client_hello_1.1_ns.xml");
80     }
81
82     private void testGetSessionForHelloMessage(final String fileName) throws Exception {
83         final Document helloDocument = XmlFileLoader.xmlFileToDocument(fileName);
84         negotiator.startNegotiation();
85         final NetconfHelloMessage helloMessage = new NetconfHelloMessage(helloDocument);
86         final AbstractNetconfSession session = negotiator.getSessionForHelloMessage(helloMessage);
87         Assert.assertNotNull(session);
88         Assert.assertTrue("NetconfChunkAggregator was not installed in the Netconf pipeline",
89             channel.pipeline().get(NETCONF_MESSAGE_AGGREGATOR) instanceof NetconfChunkAggregator);
90         Assert.assertTrue("ChunkedFramingMechanismEncoder was not installed in the Netconf pipeline",
91             channel.pipeline().get(NETCONF_MESSAGE_FRAME_ENCODER) instanceof ChunkedFramingMechanismEncoder);
92     }
93
94     private static class TestSessionNegotiator extends
95         AbstractNetconfSessionNegotiator<NetconfSessionPreferences,
96             TestingNetconfSession, NetconfSessionListener<TestingNetconfSession>> {
97
98
99         TestSessionNegotiator(final NetconfSessionPreferences sessionPreferences,
100                               final Promise<TestingNetconfSession> promise, final Channel channel,
101                               final Timer timer,
102                               final NetconfSessionListener<TestingNetconfSession> sessionListener,
103                               final long connectionTimeoutMillis) {
104             super(sessionPreferences, promise, channel, timer, sessionListener, connectionTimeoutMillis);
105         }
106
107         @Override
108         protected TestingNetconfSession getSession(final NetconfSessionListener sessionListener, final Channel channel,
109                                                    final NetconfHelloMessage message)
110             throws NetconfDocumentedException {
111             return new TestingNetconfSession(sessionListener, channel, 0L);
112         }
113
114         @Override
115         protected void handleMessage(final NetconfHelloMessage netconfHelloMessage) throws Exception {
116
117         }
118     }
119 }