2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.client;
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;
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;
31 @RunWith(MockitoJUnitRunner.StrictStubs.class)
32 public class NetconfClientSessionTest {
35 ChannelHandler channelHandler;
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");
46 final NetconfEXICodec codec = NetconfEXICodec.forParameters(EXIParameters.empty());
47 final ChannelPipeline pipeline = mock(ChannelPipeline.class);
49 Mockito.doReturn(pipeline).when(channel).pipeline();
50 Mockito.doReturn(channelHandler).when(pipeline).replace(anyString(), anyString(), any(ChannelHandler.class));
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();
58 assertEquals(caps, session.getServerCapabilities());
59 assertEquals(session, session.thisInstance());
61 Mockito.verify(pipeline, Mockito.times(4)).replace(anyString(), anyString(), Mockito.any(ChannelHandler.class));