Merge "Fix checkstyle warnings in opendaylight/netconf"
[controller.git] / opendaylight / netconf / netconf-client / src / test / java / org / opendaylight / controller / 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.controller.netconf.client;
10
11 import com.google.common.collect.Lists;
12 import io.netty.channel.Channel;
13 import io.netty.channel.ChannelHandler;
14 import io.netty.channel.ChannelPipeline;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mock;
19 import org.mockito.Mockito;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.controller.netconf.client.NetconfClientSession;
22 import org.opendaylight.controller.netconf.client.NetconfClientSessionListener;
23 import org.opendaylight.controller.netconf.nettyutil.handler.NetconfEXICodec;
24 import org.openexi.proc.common.EXIOptions;
25
26 import java.util.ArrayList;
27 import java.util.Collection;
28
29 import static org.junit.Assert.assertEquals;
30 import static org.mockito.Matchers.any;
31 import static org.mockito.Matchers.anyString;
32 import static org.mockito.Mockito.mock;
33
34 public class NetconfClientSessionTest {
35
36     @Mock
37     ChannelHandler channelHandler;
38
39     @Mock
40     Channel channel;
41
42     @Before
43     public void setUp() throws Exception {
44         MockitoAnnotations.initMocks(this);
45     }
46
47     @Test
48     public void testNetconfClientSession() throws Exception {
49         NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
50         long sessId = 20L;
51         Collection<String> caps = Lists.newArrayList("cap1", "cap2");
52
53         NetconfEXICodec codec = new NetconfEXICodec(new EXIOptions());
54         ChannelPipeline pipeline = mock(ChannelPipeline.class);
55
56         Mockito.doReturn(pipeline).when(channel).pipeline();
57         Mockito.doReturn(channelHandler).when(pipeline).replace(anyString(), anyString(), any(ChannelHandler.class));
58         Mockito.doReturn("").when(channelHandler).toString();
59
60         NetconfClientSession session = new NetconfClientSession(sessionListener, channel, sessId, caps);
61         session.addExiHandlers(codec);
62         session.stopExiCommunication();
63
64         assertEquals(caps, session.getServerCapabilities());
65         assertEquals(session, session.thisInstance());
66
67         Mockito.verify(pipeline, Mockito.times(4)).replace(anyString(), anyString(), Mockito.any(ChannelHandler.class));
68     }
69 }