60f3ad89e1a29fb9acfea45265e3dc4da8243351
[netconf.git] / netconf / netconf-client / src / test / java / org / opendaylight / 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 package org.opendaylight.netconf.client;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18
19 import io.netty.channel.Channel;
20 import io.netty.channel.ChannelPromise;
21 import io.netty.channel.EventLoop;
22 import io.netty.util.concurrent.Future;
23 import io.netty.util.concurrent.GenericFutureListener;
24 import java.util.Set;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.internal.util.collections.Sets;
28 import org.opendaylight.netconf.api.NetconfDocumentedException;
29 import org.opendaylight.netconf.api.NetconfMessage;
30 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
31
32 public class SimpleNetconfClientSessionListenerTest {
33
34     private Channel channel;
35     private ChannelPromise channelFuture;
36     Set<String> caps;
37     private NetconfHelloMessage helloMessage;
38     private NetconfMessage message;
39     private NetconfClientSessionListener sessionListener;
40     private NetconfClientSession clientSession;
41
42     @Before
43     public void setUp() throws NetconfDocumentedException {
44         channel = mock(Channel.class);
45         channelFuture = mock(ChannelPromise.class);
46         mockEventLoop();
47         doReturn(channelFuture).when(channel).newPromise();
48         doReturn(channelFuture).when(channel).writeAndFlush(any());
49         doReturn(channelFuture).when(channel).writeAndFlush(any(), any(ChannelPromise.class));
50         doReturn(channelFuture).when(channelFuture).addListener(any(GenericFutureListener.class));
51         caps = Sets.newSet("a", "b");
52         helloMessage = NetconfHelloMessage.createServerHello(caps, 10);
53         message = new NetconfMessage(helloMessage.getDocument());
54         sessionListener = mock(NetconfClientSessionListener.class);
55         clientSession = new NetconfClientSession(sessionListener, channel, 20L, caps);
56     }
57
58     private void mockEventLoop() {
59         final EventLoop eventLoop = mock(EventLoop.class);
60         doReturn(eventLoop).when(channel).eventLoop();
61         doAnswer(invocation -> {
62             invocation.<Runnable>getArgument(0).run();
63             return null;
64         }).when(eventLoop).execute(any(Runnable.class));
65     }
66
67     @Test
68     public void testSessionDown() throws Exception {
69         SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
70         final Future<NetconfMessage> promise = simpleListener.sendRequest(message);
71         simpleListener.onSessionUp(clientSession);
72         verify(channel, times(1)).writeAndFlush(any(), any());
73
74         simpleListener.onSessionDown(clientSession, new Exception());
75         assertFalse(promise.isSuccess());
76     }
77
78     @Test
79     public void testSendRequest() {
80         SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
81         final Future<NetconfMessage> promise = simpleListener.sendRequest(message);
82         simpleListener.onSessionUp(clientSession);
83         verify(channel, times(1)).writeAndFlush(any(), any());
84
85         simpleListener.sendRequest(message);
86         assertFalse(promise.isSuccess());
87     }
88
89     @Test
90     public void testOnMessage() {
91         SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
92         final Future<NetconfMessage> promise = simpleListener.sendRequest(message);
93         simpleListener.onSessionUp(clientSession);
94         verify(channel, times(1)).writeAndFlush(any(), any());
95
96         simpleListener.onMessage(clientSession, message);
97         assertTrue(promise.isSuccess());
98     }
99 }