Move netconf-dom-api
[netconf.git] / protocol / netconf-client / src / test / java / org / opendaylight / netconf / client / NetconfClientSessionTest.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
9 package org.opendaylight.netconf.client;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.anyString;
14 import static org.mockito.Mockito.mock;
15
16 import com.google.common.collect.Lists;
17 import io.netty.channel.Channel;
18 import io.netty.channel.ChannelHandler;
19 import io.netty.channel.ChannelPipeline;
20 import java.util.Collection;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.junit.MockitoJUnitRunner;
26 import org.opendaylight.netconf.nettyutil.handler.NetconfEXICodec;
27 import org.opendaylight.netconf.nettyutil.handler.NetconfEXIToMessageDecoder;
28 import org.opendaylight.netconf.nettyutil.handler.NetconfMessageToEXIEncoder;
29 import org.opendaylight.netconf.nettyutil.handler.exi.EXIParameters;
30
31 @RunWith(MockitoJUnitRunner.StrictStubs.class)
32 public class NetconfClientSessionTest {
33
34     @Mock
35     ChannelHandler channelHandler;
36
37     @Mock
38     Channel channel;
39
40     @Test
41     public void testNetconfClientSession() throws Exception {
42         final NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
43         final long sessId = 20L;
44         final Collection<String> caps = Lists.newArrayList("cap1", "cap2");
45
46         final NetconfEXICodec codec = NetconfEXICodec.forParameters(EXIParameters.empty());
47         final ChannelPipeline pipeline = mock(ChannelPipeline.class);
48
49         Mockito.doReturn(pipeline).when(channel).pipeline();
50         Mockito.doReturn(channelHandler).when(pipeline).replace(anyString(), anyString(), any(ChannelHandler.class));
51
52         final NetconfClientSession session = new NetconfClientSession(sessionListener, channel, sessId, caps);
53         final NetconfMessageToEXIEncoder exiEncoder = NetconfMessageToEXIEncoder.create(codec);
54         final NetconfEXIToMessageDecoder exiDecoder = NetconfEXIToMessageDecoder.create(codec);
55         session.addExiHandlers(exiDecoder, exiEncoder);
56         session.stopExiCommunication();
57
58         assertEquals(caps, session.getServerCapabilities());
59         assertEquals(session, session.thisInstance());
60
61         Mockito.verify(pipeline, Mockito.times(4)).replace(anyString(), anyString(), Mockito.any(ChannelHandler.class));
62     }
63 }