Merge "Activate findbugs sonar profile"
[controller.git] / opendaylight / netconf / netconf-client / src / test / java / org / opendaylight / controller / netconf / client / SimpleNetconfClientSessionListenerTest.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 import io.netty.channel.*;
11 import io.netty.util.concurrent.Future;
12 import io.netty.util.concurrent.Promise;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.mockito.internal.util.collections.Sets;
16 import org.opendaylight.controller.netconf.api.NetconfMessage;
17 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
18
19 import java.util.Set;
20
21 import static org.junit.Assert.*;
22 import static org.mockito.Matchers.anyObject;
23 import static org.mockito.Mockito.*;
24
25 public class SimpleNetconfClientSessionListenerTest {
26
27     private Channel channel;
28     private ChannelFuture channelFuture;
29     Set caps;
30     private NetconfHelloMessage helloMessage;
31     private NetconfMessage message;
32     private NetconfClientSessionListener sessionListener;
33     private NetconfClientSession clientSession;
34
35     @Before
36     public void setUp() throws Exception {
37         channel = mock(Channel.class);
38         channelFuture = mock(ChannelFuture.class);
39         doReturn(channelFuture).when(channel).writeAndFlush(anyObject());
40         caps = Sets.newSet("a", "b");
41         helloMessage = NetconfHelloMessage.createServerHello(caps, 10);
42         message = new NetconfMessage(helloMessage.getDocument());
43         sessionListener = mock(NetconfClientSessionListener.class);
44         clientSession = new NetconfClientSession(sessionListener, channel, 20L, caps);
45     }
46
47     @Test
48     public void testSessionDown() throws Exception {
49         SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
50         Future<NetconfMessage> promise = simpleListener.sendRequest(message);
51         simpleListener.onSessionUp(clientSession);
52         verify(channel, times(1)).writeAndFlush(anyObject());
53
54         simpleListener.onSessionDown(clientSession, new Exception());
55         assertFalse(promise.isSuccess());
56     }
57
58     @Test
59     public void testSendRequest() throws Exception {
60         SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
61         Future<NetconfMessage> promise = simpleListener.sendRequest(message);
62         simpleListener.onSessionUp(clientSession);
63         verify(channel, times(1)).writeAndFlush(anyObject());
64
65         simpleListener.sendRequest(message);
66         assertFalse(promise.isSuccess());
67     }
68
69     @Test
70     public void testOnMessage() throws Exception {
71         SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
72         Future<NetconfMessage> promise = simpleListener.sendRequest(message);
73         simpleListener.onSessionUp(clientSession);
74         verify(channel, times(1)).writeAndFlush(anyObject());
75
76         simpleListener.onMessage(clientSession, message);
77         assertTrue(promise.isSuccess());
78     }
79 }