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