Merge "Migrate restconf to mockito ArgumentMatchers"
[netconf.git] / netconf / 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.Matchers.any;
13 import static org.mockito.Matchers.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.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.MockitoAnnotations;
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 public class NetconfClientSessionTest {
32
33     @Mock
34     ChannelHandler channelHandler;
35
36     @Mock
37     Channel channel;
38
39     @Before
40     public void setUp() throws Exception {
41         MockitoAnnotations.initMocks(this);
42     }
43
44     @Test
45     public void testNetconfClientSession() throws Exception {
46         final NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
47         final long sessId = 20L;
48         final Collection<String> caps = Lists.newArrayList("cap1", "cap2");
49
50         final NetconfEXICodec codec = NetconfEXICodec.forParameters(EXIParameters.empty());
51         final ChannelPipeline pipeline = mock(ChannelPipeline.class);
52
53         Mockito.doReturn(pipeline).when(channel).pipeline();
54         Mockito.doReturn(channelHandler).when(pipeline).replace(anyString(), anyString(), any(ChannelHandler.class));
55         Mockito.doReturn("").when(channelHandler).toString();
56
57         final NetconfClientSession session = new NetconfClientSession(sessionListener, channel, sessId, caps);
58         final NetconfMessageToEXIEncoder exiEncoder = NetconfMessageToEXIEncoder.create(codec);
59         final NetconfEXIToMessageDecoder exiDecoder = NetconfEXIToMessageDecoder.create(codec);
60         session.addExiHandlers(exiDecoder, exiEncoder);
61         session.stopExiCommunication();
62
63         assertEquals(caps, session.getServerCapabilities());
64         assertEquals(session, session.thisInstance());
65
66         Mockito.verify(pipeline, Mockito.times(4)).replace(anyString(), anyString(), Mockito.any(ChannelHandler.class));
67     }
68 }