2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.nettyutil;
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;
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.NetconfStartExiMessage;
46 import org.openexi.proc.common.EXIOptions;
48 public class AbstractNetconfSessionTest {
51 private NetconfSessionListener<TestingNetconfSession> listener;
53 private Channel channel;
55 private ChannelPipeline pipeline;
57 private EventLoop eventLoop;
59 private ChannelFuture writeFuture;
61 private NetconfHelloMessage clientHello;
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), any(NetconfTerminationReason.class));
71 doReturn(writeFuture).when(writeFuture).addListener(any(GenericFutureListener.class));
73 doReturn(writeFuture).when(channel).writeAndFlush(any(NetconfMessage.class));
74 doReturn(pipeline).when(channel).pipeline();
75 doReturn("mockChannel").when(channel).toString();
76 doReturn(mock(ChannelFuture.class)).when(channel).close();
78 doReturn(null).when(pipeline).replace(anyString(), anyString(), any(ChannelHandler.class));
80 doReturn(eventLoop).when(channel).eventLoop();
81 doAnswer(new Answer<Void>() {
83 public Void answer(final InvocationOnMock invocation) throws Throwable {
84 final Object[] args = invocation.getArguments();
85 final Runnable runnable = (Runnable) args[0];
89 }).when(eventLoop).execute(any(Runnable.class));
91 clientHello = NetconfHelloMessage.createClientHello(Collections.<String>emptySet(), Optional.<NetconfHelloMessageAdditionalHeader>absent());
95 public void testHandleMessage() throws Exception {
96 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
97 testingNetconfSession.handleMessage(clientHello);
98 verify(listener).onMessage(testingNetconfSession, clientHello);
102 public void testSessionUp() throws Exception {
103 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
104 testingNetconfSession.sessionUp();
105 verify(listener).onSessionUp(testingNetconfSession);
106 assertEquals(1L, testingNetconfSession.getSessionId());
110 public void testClose() throws Exception {
111 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
112 testingNetconfSession.sessionUp();
113 testingNetconfSession.close();
114 verify(channel).close();
115 verify(listener).onSessionTerminated(any(TestingNetconfSession.class), any(NetconfTerminationReason.class));
119 public void testReplaceHandlers() throws Exception {
120 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
121 final ChannelHandler mock = mock(ChannelHandler.class);
122 doReturn("handler").when(mock).toString();
124 testingNetconfSession.replaceMessageDecoder(mock);
125 verify(pipeline).replace(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, mock);
126 testingNetconfSession.replaceMessageEncoder(mock);
127 verify(pipeline).replace(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, mock);
128 testingNetconfSession.replaceMessageEncoderAfterNextMessage(mock);
129 verifyNoMoreInteractions(pipeline);
131 testingNetconfSession.sendMessage(clientHello);
132 verify(pipeline, times(2)).replace(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, mock);
136 public void testStartExi() throws Exception {
137 TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
138 testingNetconfSession = spy(testingNetconfSession);
140 testingNetconfSession.startExiCommunication(NetconfStartExiMessage.create(new EXIOptions(), "4"));
141 verify(testingNetconfSession).addExiHandlers(any(ByteToMessageDecoder.class), any(MessageToByteEncoder.class));
145 public void testEndOfInput() throws Exception {
146 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
147 testingNetconfSession.endOfInput();
148 verifyZeroInteractions(listener);
149 testingNetconfSession.sessionUp();
150 testingNetconfSession.endOfInput();
151 verify(listener).onSessionDown(any(TestingNetconfSession.class), any(Exception.class));
155 public void testSendMessage() throws Exception {
156 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
157 final NetconfHelloMessage clientHello = NetconfHelloMessage.createClientHello(Collections.<String>emptySet(), Optional.<NetconfHelloMessageAdditionalHeader>absent());
158 testingNetconfSession.sendMessage(clientHello);
159 verify(channel).writeAndFlush(clientHello);