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