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
8 package org.opendaylight.netconf.nettyutil;
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.anyString;
13 import static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.spy;
18 import static org.mockito.Mockito.times;
19 import static org.mockito.Mockito.verify;
20 import static org.mockito.Mockito.verifyNoMoreInteractions;
22 import io.netty.channel.Channel;
23 import io.netty.channel.ChannelFuture;
24 import io.netty.channel.ChannelHandler;
25 import io.netty.channel.ChannelPipeline;
26 import io.netty.channel.ChannelPromise;
27 import io.netty.channel.EventLoop;
28 import io.netty.handler.codec.ByteToMessageDecoder;
29 import io.netty.handler.codec.MessageToByteEncoder;
30 import java.io.EOFException;
31 import java.util.Collections;
32 import java.util.Optional;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.junit.MockitoJUnitRunner;
38 import org.opendaylight.netconf.api.NetconfMessage;
39 import org.opendaylight.netconf.api.NetconfSessionListener;
40 import org.opendaylight.netconf.api.NetconfTerminationReason;
41 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
42 import org.opendaylight.netconf.nettyutil.handler.exi.EXIParameters;
43 import org.opendaylight.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
45 @RunWith(MockitoJUnitRunner.StrictStubs.class)
46 public class AbstractNetconfSessionTest {
49 private NetconfSessionListener<TestingNetconfSession> listener;
51 private Channel channel;
53 private ChannelPipeline pipeline;
55 private EventLoop eventLoop;
57 private ChannelPromise writeFuture;
59 private NetconfHelloMessage clientHello;
62 public void setUp() throws Exception {
63 doNothing().when(listener).onMessage(any(TestingNetconfSession.class), any(NetconfMessage.class));
64 doNothing().when(listener).onSessionUp(any(TestingNetconfSession.class));
65 doNothing().when(listener).onSessionDown(any(TestingNetconfSession.class), any(Exception.class));
66 doNothing().when(listener).onSessionTerminated(any(TestingNetconfSession.class),
67 any(NetconfTerminationReason.class));
69 doReturn(writeFuture).when(channel).newPromise();
70 doReturn(writeFuture).when(channel).writeAndFlush(any(NetconfMessage.class), any(ChannelPromise.class));
71 doReturn(pipeline).when(channel).pipeline();
72 doReturn(mock(ChannelFuture.class)).when(channel).close();
74 doReturn(null).when(pipeline).replace(anyString(), anyString(), any(ChannelHandler.class));
76 doReturn(eventLoop).when(channel).eventLoop();
77 doAnswer(invocation -> {
78 invocation.<Runnable>getArgument(0).run();
80 }).when(eventLoop).execute(any(Runnable.class));
82 clientHello = NetconfHelloMessage.createClientHello(Collections.emptySet(), Optional.empty());
86 public void testHandleMessage() throws Exception {
87 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
88 testingNetconfSession.handleMessage(clientHello);
89 verify(listener).onMessage(testingNetconfSession, clientHello);
93 public void testSessionUp() throws Exception {
94 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
95 testingNetconfSession.sessionUp();
96 verify(listener).onSessionUp(testingNetconfSession);
97 assertEquals(1L, testingNetconfSession.getSessionId());
101 public void testClose() throws Exception {
102 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
103 testingNetconfSession.sessionUp();
104 testingNetconfSession.close();
105 verify(channel).close();
106 verify(listener).onSessionTerminated(any(TestingNetconfSession.class), any(NetconfTerminationReason.class));
110 public void testReplaceHandlers() throws Exception {
111 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
112 final ChannelHandler mock = mock(ChannelHandler.class);
114 testingNetconfSession.replaceMessageDecoder(mock);
115 verify(pipeline).replace(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER,
116 AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, mock);
117 testingNetconfSession.replaceMessageEncoder(mock);
118 verify(pipeline).replace(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER,
119 AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, mock);
120 testingNetconfSession.replaceMessageEncoderAfterNextMessage(mock);
121 verifyNoMoreInteractions(pipeline);
123 testingNetconfSession.sendMessage(clientHello);
124 verify(pipeline, times(2)).replace(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER,
125 AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, mock);
129 public void testStartExi() throws Exception {
130 TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
131 testingNetconfSession = spy(testingNetconfSession);
133 testingNetconfSession.startExiCommunication(NetconfStartExiMessage.create(EXIParameters.empty(), "4"));
134 verify(testingNetconfSession).addExiHandlers(any(ByteToMessageDecoder.class), any(MessageToByteEncoder.class));
138 public void testEndOfInput() throws Exception {
139 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
140 testingNetconfSession.endOfInput();
141 verifyNoMoreInteractions(listener);
142 testingNetconfSession.sessionUp();
143 testingNetconfSession.endOfInput();
144 verify(listener).onSessionDown(any(TestingNetconfSession.class), any(EOFException.class));
148 public void testSendMessage() throws Exception {
149 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
150 final NetconfHelloMessage hello = NetconfHelloMessage.createClientHello(Collections.emptySet(),
152 testingNetconfSession.sendMessage(hello);
153 verify(channel).writeAndFlush(hello, writeFuture);