NETCONF-241: Switch to using Exificient
[netconf.git] / 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.NetconfSessionListener;
42 import org.opendaylight.netconf.api.NetconfTerminationReason;
43 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
44 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
45 import org.opendaylight.netconf.nettyutil.handler.exi.EXIParameters;
46 import org.opendaylight.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
47
48 public class AbstractNetconfSessionTest {
49
50     @Mock
51     private NetconfSessionListener<TestingNetconfSession> listener;
52     @Mock
53     private Channel channel;
54     @Mock
55     private ChannelPipeline pipeline;
56     @Mock
57     private EventLoop eventLoop;
58     @Mock
59     private ChannelFuture writeFuture;
60
61     private NetconfHelloMessage clientHello;
62
63     @Before
64     public void setUp() throws Exception {
65         MockitoAnnotations.initMocks(this);
66         doNothing().when(listener).onMessage(any(TestingNetconfSession.class), any(NetconfMessage.class));
67         doNothing().when(listener).onSessionUp(any(TestingNetconfSession.class));
68         doNothing().when(listener).onSessionDown(any(TestingNetconfSession.class), any(Exception.class));
69         doNothing().when(listener).onSessionTerminated(any(TestingNetconfSession.class),
70                 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(final 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(),
93                 Optional.<NetconfHelloMessageAdditionalHeader>absent());
94     }
95
96     @Test
97     public void testHandleMessage() throws Exception {
98         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
99         testingNetconfSession.handleMessage(clientHello);
100         verify(listener).onMessage(testingNetconfSession, clientHello);
101     }
102
103     @Test
104     public void testSessionUp() throws Exception {
105         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
106         testingNetconfSession.sessionUp();
107         verify(listener).onSessionUp(testingNetconfSession);
108         assertEquals(1L, testingNetconfSession.getSessionId());
109     }
110
111     @Test
112     public void testClose() throws Exception {
113         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
114         testingNetconfSession.sessionUp();
115         testingNetconfSession.close();
116         verify(channel).close();
117         verify(listener).onSessionTerminated(any(TestingNetconfSession.class), any(NetconfTerminationReason.class));
118     }
119
120     @Test
121     public void testReplaceHandlers() throws Exception {
122         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
123         final ChannelHandler mock = mock(ChannelHandler.class);
124         doReturn("handler").when(mock).toString();
125
126         testingNetconfSession.replaceMessageDecoder(mock);
127         verify(pipeline).replace(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER,
128                 AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, mock);
129         testingNetconfSession.replaceMessageEncoder(mock);
130         verify(pipeline).replace(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER,
131                 AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, mock);
132         testingNetconfSession.replaceMessageEncoderAfterNextMessage(mock);
133         verifyNoMoreInteractions(pipeline);
134
135         testingNetconfSession.sendMessage(clientHello);
136         verify(pipeline, times(2)).replace(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER,
137                 AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, mock);
138     }
139
140     @Test
141     public void testStartExi() throws Exception {
142         TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
143         testingNetconfSession = spy(testingNetconfSession);
144
145         testingNetconfSession.startExiCommunication(NetconfStartExiMessage.create(EXIParameters.empty(), "4"));
146         verify(testingNetconfSession).addExiHandlers(any(ByteToMessageDecoder.class), any(MessageToByteEncoder.class));
147     }
148
149     @Test
150     public void testEndOfInput() throws Exception {
151         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
152         testingNetconfSession.endOfInput();
153         verifyZeroInteractions(listener);
154         testingNetconfSession.sessionUp();
155         testingNetconfSession.endOfInput();
156         verify(listener).onSessionDown(any(TestingNetconfSession.class), any(Exception.class));
157     }
158
159     @Test
160     public void testSendMessage() throws Exception {
161         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
162         final NetconfHelloMessage hello = NetconfHelloMessage.createClientHello(Collections.<String>emptySet(),
163                 Optional.<NetconfHelloMessageAdditionalHeader>absent());
164         testingNetconfSession.sendMessage(hello);
165         verify(channel).writeAndFlush(hello);
166     }
167
168 }