BUG 4547: Make sure all channel writes are from a netty thread.
[netconf.git] / opendaylight / netconf / netconf-netty-util / src / test / java / org / opendaylight / netconf / nettyutil / AbstractNetconfSessionTest.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.nettyutil;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.anyString;
14 import static org.mockito.Mockito.doAnswer;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.spy;
19 import static org.mockito.Mockito.times;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.verifyNoMoreInteractions;
22 import static org.mockito.Mockito.verifyZeroInteractions;
23
24 import com.google.common.base.Optional;
25 import io.netty.channel.Channel;
26 import io.netty.channel.ChannelFuture;
27 import io.netty.channel.ChannelHandler;
28 import io.netty.channel.ChannelPipeline;
29 import io.netty.channel.EventLoop;
30 import io.netty.handler.codec.ByteToMessageDecoder;
31 import io.netty.handler.codec.MessageToByteEncoder;
32 import io.netty.util.concurrent.GenericFutureListener;
33 import java.util.Collections;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.mockito.invocation.InvocationOnMock;
39 import org.mockito.stubbing.Answer;
40 import org.opendaylight.netconf.api.NetconfMessage;
41 import org.opendaylight.netconf.api.NetconfSession;
42 import org.opendaylight.netconf.api.NetconfSessionListener;
43 import org.opendaylight.netconf.api.NetconfTerminationReason;
44 import org.opendaylight.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
45 import org.opendaylight.netconf.util.messages.NetconfHelloMessage;
46 import org.opendaylight.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
47 import org.openexi.proc.common.EXIOptions;
48
49 public class AbstractNetconfSessionTest {
50
51     @Mock
52     private NetconfSessionListener<NetconfSession> listener;
53     @Mock
54     private Channel channel;
55     @Mock
56     private ChannelPipeline pipeline;
57     @Mock
58     private EventLoop eventLoop;
59     @Mock
60     private ChannelFuture writeFuture;
61
62     private NetconfHelloMessage clientHello;
63
64     @Before
65     public void setUp() throws Exception {
66         MockitoAnnotations.initMocks(this);
67         doNothing().when(listener).onMessage(any(NetconfSession.class), any(NetconfMessage.class));
68         doNothing().when(listener).onSessionUp(any(NetconfSession.class));
69         doNothing().when(listener).onSessionDown(any(NetconfSession.class), any(Exception.class));
70         doNothing().when(listener).onSessionTerminated(any(NetconfSession.class), any(NetconfTerminationReason.class));
71
72         doReturn(writeFuture).when(writeFuture).addListener(any(GenericFutureListener.class));
73
74         doReturn(writeFuture).when(channel).writeAndFlush(any(NetconfMessage.class));
75         doReturn(pipeline).when(channel).pipeline();
76         doReturn("mockChannel").when(channel).toString();
77         doReturn(mock(ChannelFuture.class)).when(channel).close();
78
79         doReturn(null).when(pipeline).replace(anyString(), anyString(), any(ChannelHandler.class));
80
81         doReturn(eventLoop).when(channel).eventLoop();
82         doAnswer(new Answer<Void>() {
83             @Override
84             public Void answer(InvocationOnMock invocation) throws Throwable {
85                 final Object[] args = invocation.getArguments();
86                 final Runnable runnable = (Runnable) args[0];
87                 runnable.run();
88                 return null;
89             }
90         }).when(eventLoop).execute(any(Runnable.class));
91
92         clientHello = NetconfHelloMessage.createClientHello(Collections.<String>emptySet(), Optional.<NetconfHelloMessageAdditionalHeader>absent());
93     }
94
95     @Test
96     public void testHandleMessage() throws Exception {
97         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
98         testingNetconfSession.handleMessage(clientHello);
99         verify(listener).onMessage(testingNetconfSession, clientHello);
100     }
101
102     @Test
103     public void testSessionUp() throws Exception {
104         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
105         testingNetconfSession.sessionUp();
106         verify(listener).onSessionUp(testingNetconfSession);
107         assertEquals(1L, testingNetconfSession.getSessionId());
108     }
109
110     @Test
111     public void testClose() throws Exception {
112         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
113         testingNetconfSession.sessionUp();
114         testingNetconfSession.close();
115         verify(channel).close();
116         verify(listener).onSessionTerminated(any(NetconfSession.class), any(NetconfTerminationReason.class));
117     }
118
119     @Test
120     public void testReplaceHandlers() throws Exception {
121         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
122         final ChannelHandler mock = mock(ChannelHandler.class);
123         doReturn("handler").when(mock).toString();
124
125         testingNetconfSession.replaceMessageDecoder(mock);
126         verify(pipeline).replace(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, mock);
127         testingNetconfSession.replaceMessageEncoder(mock);
128         verify(pipeline).replace(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, mock);
129         testingNetconfSession.replaceMessageEncoderAfterNextMessage(mock);
130         verifyNoMoreInteractions(pipeline);
131
132         testingNetconfSession.sendMessage(clientHello);
133         verify(pipeline, times(2)).replace(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, mock);
134     }
135
136     @Test
137     public void testStartExi() throws Exception {
138         TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
139         testingNetconfSession = spy(testingNetconfSession);
140
141         testingNetconfSession.startExiCommunication(NetconfStartExiMessage.create(new EXIOptions(), "4"));
142         verify(testingNetconfSession).addExiHandlers(any(ByteToMessageDecoder.class), any(MessageToByteEncoder.class));
143     }
144
145     @Test
146     public void testEndOfInput() throws Exception {
147         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
148         testingNetconfSession.endOfInput();
149         verifyZeroInteractions(listener);
150         testingNetconfSession.sessionUp();
151         testingNetconfSession.endOfInput();
152         verify(listener).onSessionDown(any(NetconfSession.class), any(Exception.class));
153     }
154
155     @Test
156     public void testSendMessage() throws Exception {
157         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
158         final NetconfHelloMessage clientHello = NetconfHelloMessage.createClientHello(Collections.<String>emptySet(), Optional.<NetconfHelloMessageAdditionalHeader>absent());
159         testingNetconfSession.sendMessage(clientHello);
160         verify(channel).writeAndFlush(clientHello);
161     }
162
163     private static class TestingNetconfSession extends AbstractNetconfSession<NetconfSession, NetconfSessionListener<NetconfSession>> {
164
165         protected TestingNetconfSession(final NetconfSessionListener<NetconfSession> sessionListener, final Channel channel, final long sessionId) {
166             super(sessionListener, channel, sessionId);
167         }
168
169         @Override
170         protected NetconfSession thisInstance() {
171             return this;
172         }
173
174         @Override
175         protected void addExiHandlers(final ByteToMessageDecoder decoder, final MessageToByteEncoder<NetconfMessage> encoder) {}
176
177         @Override
178         public void stopExiCommunication() {}
179     }
180 }