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.controller.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.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;
21 import static org.mockito.Mockito.verifyZeroInteractions;
23 import com.google.common.base.Optional;
24 import io.netty.channel.Channel;
25 import io.netty.channel.ChannelFuture;
26 import io.netty.channel.ChannelHandler;
27 import io.netty.channel.ChannelPipeline;
28 import java.util.Collections;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.opendaylight.controller.netconf.api.NetconfMessage;
34 import org.opendaylight.controller.netconf.api.NetconfSession;
35 import org.opendaylight.controller.netconf.api.NetconfSessionListener;
36 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
37 import org.opendaylight.controller.netconf.nettyutil.handler.NetconfEXICodec;
38 import org.opendaylight.controller.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
39 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
40 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
41 import org.openexi.proc.common.EXIOptions;
43 public class AbstractNetconfSessionTest {
46 private NetconfSessionListener<NetconfSession> listener;
48 private Channel channel;
50 private ChannelPipeline pipeline;
51 private NetconfHelloMessage clientHello;
54 public void setUp() throws Exception {
55 MockitoAnnotations.initMocks(this);
56 doNothing().when(listener).onMessage(any(NetconfSession.class), any(NetconfMessage.class));
57 doNothing().when(listener).onSessionUp(any(NetconfSession.class));
58 doNothing().when(listener).onSessionDown(any(NetconfSession.class), any(Exception.class));
59 doNothing().when(listener).onSessionTerminated(any(NetconfSession.class), any(NetconfTerminationReason.class));
61 doReturn(mock(ChannelFuture.class)).when(channel).writeAndFlush(any(NetconfMessage.class));
62 doReturn(pipeline).when(channel).pipeline();
63 doReturn(mock(ChannelFuture.class)).when(channel).close();
65 doReturn(null).when(pipeline).replace(anyString(), anyString(), any(ChannelHandler.class));
67 clientHello = NetconfHelloMessage.createClientHello(Collections.<String>emptySet(), Optional.<NetconfHelloMessageAdditionalHeader>absent());
71 public void testHandleMessage() throws Exception {
72 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
73 testingNetconfSession.handleMessage(clientHello);
74 verify(listener).onMessage(testingNetconfSession, clientHello);
78 public void testSessionUp() throws Exception {
79 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
80 testingNetconfSession.sessionUp();
81 verify(listener).onSessionUp(testingNetconfSession);
82 assertEquals(1L, testingNetconfSession.getSessionId());
86 public void testClose() throws Exception {
87 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
88 testingNetconfSession.sessionUp();
89 testingNetconfSession.close();
90 verify(channel).close();
91 verify(listener).onSessionTerminated(any(NetconfSession.class), any(NetconfTerminationReason.class));
95 public void testReplaceHandlers() throws Exception {
96 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
97 final ChannelHandler mock = mock(ChannelHandler.class);
98 doReturn("handler").when(mock).toString();
100 testingNetconfSession.replaceMessageDecoder(mock);
101 verify(pipeline).replace(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, mock);
102 testingNetconfSession.replaceMessageEncoder(mock);
103 verify(pipeline).replace(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, mock);
104 testingNetconfSession.replaceMessageEncoderAfterNextMessage(mock);
105 verifyNoMoreInteractions(pipeline);
107 testingNetconfSession.sendMessage(clientHello);
108 verify(pipeline, times(2)).replace(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, mock);
112 public void testStartExi() throws Exception {
113 TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
114 testingNetconfSession = spy(testingNetconfSession);
116 testingNetconfSession.startExiCommunication(NetconfStartExiMessage.create(new EXIOptions(), "4"));
117 verify(testingNetconfSession).addExiHandlers(any(NetconfEXICodec.class));
121 public void testEndOfInput() throws Exception {
122 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
123 testingNetconfSession.endOfInput();
124 verifyZeroInteractions(listener);
125 testingNetconfSession.sessionUp();
126 testingNetconfSession.endOfInput();
127 verify(listener).onSessionDown(any(NetconfSession.class), any(Exception.class));
131 public void testSendMessage() throws Exception {
132 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
133 final NetconfHelloMessage clientHello = NetconfHelloMessage.createClientHello(Collections.<String>emptySet(), Optional.<NetconfHelloMessageAdditionalHeader>absent());
134 testingNetconfSession.sendMessage(clientHello);
135 verify(channel).writeAndFlush(clientHello);
138 private static class TestingNetconfSession extends AbstractNetconfSession<NetconfSession, NetconfSessionListener<NetconfSession>> {
140 protected TestingNetconfSession(final NetconfSessionListener<NetconfSession> sessionListener, final Channel channel, final long sessionId) {
141 super(sessionListener, channel, sessionId);
145 protected NetconfSession thisInstance() {
150 protected void addExiHandlers(final NetconfEXICodec exiCodec) {}
153 public void stopExiCommunication() {}