BUG 4547: Make sure all channel writes are from a netty thread.
[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
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Matchers.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.ChannelFuture;
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.mockito.invocation.InvocationOnMock;
31 import org.mockito.stubbing.Answer;
32 import org.opendaylight.controller.netconf.api.NetconfMessage;
33 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
34
35 public class SimpleNetconfClientSessionListenerTest {
36
37     private Channel channel;
38     private ChannelFuture channelFuture;
39     Set<String> caps;
40     private NetconfHelloMessage helloMessage;
41     private NetconfMessage message;
42     private NetconfClientSessionListener sessionListener;
43     private NetconfClientSession clientSession;
44
45     @Before
46     public void setUp() throws Exception {
47         channel = mock(Channel.class);
48         channelFuture = mock(ChannelFuture.class);
49         mockEventLoop();
50         doReturn(channelFuture).when(channel).writeAndFlush(anyObject());
51         doReturn(channelFuture).when(channelFuture).addListener(any(GenericFutureListener.class));
52         caps = Sets.newSet("a", "b");
53         helloMessage = NetconfHelloMessage.createServerHello(caps, 10);
54         message = new NetconfMessage(helloMessage.getDocument());
55         sessionListener = mock(NetconfClientSessionListener.class);
56         clientSession = new NetconfClientSession(sessionListener, channel, 20L, caps);
57     }
58
59     private void mockEventLoop() {
60         final EventLoop eventLoop = mock(EventLoop.class);
61         doReturn(eventLoop).when(channel).eventLoop();
62         doAnswer(new Answer<Void>() {
63             @Override
64             public Void answer(InvocationOnMock invocation) throws Throwable {
65                 final Object[] args = invocation.getArguments();
66                 final Runnable runnable = (Runnable) args[0];
67                 runnable.run();
68                 return null;
69             }
70         }).when(eventLoop).execute(any(Runnable.class));
71     }
72
73     @Test
74     public void testSessionDown() throws Exception {
75         SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
76         Future<NetconfMessage> promise = simpleListener.sendRequest(message);
77         simpleListener.onSessionUp(clientSession);
78         verify(channel, times(1)).writeAndFlush(anyObject());
79
80         simpleListener.onSessionDown(clientSession, new Exception());
81         assertFalse(promise.isSuccess());
82     }
83
84     @Test
85     public void testSendRequest() throws Exception {
86         SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
87         Future<NetconfMessage> promise = simpleListener.sendRequest(message);
88         simpleListener.onSessionUp(clientSession);
89         verify(channel, times(1)).writeAndFlush(anyObject());
90
91         simpleListener.sendRequest(message);
92         assertFalse(promise.isSuccess());
93     }
94
95     @Test
96     public void testOnMessage() throws Exception {
97         SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
98         Future<NetconfMessage> promise = simpleListener.sendRequest(message);
99         simpleListener.onSessionUp(clientSession);
100         verify(channel, times(1)).writeAndFlush(anyObject());
101
102         simpleListener.onMessage(clientSession, message);
103         assertTrue(promise.isSuccess());
104     }
105 }