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