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.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 io.netty.handler.codec.ByteToMessageDecoder;
29 import io.netty.handler.codec.MessageToByteEncoder;
30 import java.util.Collections;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.opendaylight.netconf.util.messages.NetconfHelloMessage;
36 import org.opendaylight.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
37 import org.opendaylight.netconf.api.NetconfMessage;
38 import org.opendaylight.netconf.api.NetconfSession;
39 import org.opendaylight.netconf.api.NetconfSessionListener;
40 import org.opendaylight.netconf.api.NetconfTerminationReason;
41 import org.opendaylight.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
42 import org.openexi.proc.common.EXIOptions;
44 public class AbstractNetconfSessionTest {
47 private NetconfSessionListener<NetconfSession> listener;
49 private Channel channel;
51 private ChannelPipeline pipeline;
52 private NetconfHelloMessage clientHello;
55 public void setUp() throws Exception {
56 MockitoAnnotations.initMocks(this);
57 doNothing().when(listener).onMessage(any(NetconfSession.class), any(NetconfMessage.class));
58 doNothing().when(listener).onSessionUp(any(NetconfSession.class));
59 doNothing().when(listener).onSessionDown(any(NetconfSession.class), any(Exception.class));
60 doNothing().when(listener).onSessionTerminated(any(NetconfSession.class), any(NetconfTerminationReason.class));
62 doReturn(mock(ChannelFuture.class)).when(channel).writeAndFlush(any(NetconfMessage.class));
63 doReturn(pipeline).when(channel).pipeline();
64 doReturn("mockChannel").when(channel).toString();
65 doReturn(mock(ChannelFuture.class)).when(channel).close();
67 doReturn(null).when(pipeline).replace(anyString(), anyString(), any(ChannelHandler.class));
69 clientHello = NetconfHelloMessage.createClientHello(Collections.<String>emptySet(), Optional.<NetconfHelloMessageAdditionalHeader>absent());
73 public void testHandleMessage() throws Exception {
74 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
75 testingNetconfSession.handleMessage(clientHello);
76 verify(listener).onMessage(testingNetconfSession, clientHello);
80 public void testSessionUp() throws Exception {
81 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
82 testingNetconfSession.sessionUp();
83 verify(listener).onSessionUp(testingNetconfSession);
84 assertEquals(1L, testingNetconfSession.getSessionId());
88 public void testClose() throws Exception {
89 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
90 testingNetconfSession.sessionUp();
91 testingNetconfSession.close();
92 verify(channel).close();
93 verify(listener).onSessionTerminated(any(NetconfSession.class), any(NetconfTerminationReason.class));
97 public void testReplaceHandlers() throws Exception {
98 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
99 final ChannelHandler mock = mock(ChannelHandler.class);
100 doReturn("handler").when(mock).toString();
102 testingNetconfSession.replaceMessageDecoder(mock);
103 verify(pipeline).replace(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, mock);
104 testingNetconfSession.replaceMessageEncoder(mock);
105 verify(pipeline).replace(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, mock);
106 testingNetconfSession.replaceMessageEncoderAfterNextMessage(mock);
107 verifyNoMoreInteractions(pipeline);
109 testingNetconfSession.sendMessage(clientHello);
110 verify(pipeline, times(2)).replace(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, mock);
114 public void testStartExi() throws Exception {
115 TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
116 testingNetconfSession = spy(testingNetconfSession);
118 testingNetconfSession.startExiCommunication(NetconfStartExiMessage.create(new EXIOptions(), "4"));
119 verify(testingNetconfSession).addExiHandlers(any(ByteToMessageDecoder.class), any(MessageToByteEncoder.class));
123 public void testEndOfInput() throws Exception {
124 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
125 testingNetconfSession.endOfInput();
126 verifyZeroInteractions(listener);
127 testingNetconfSession.sessionUp();
128 testingNetconfSession.endOfInput();
129 verify(listener).onSessionDown(any(NetconfSession.class), any(Exception.class));
133 public void testSendMessage() throws Exception {
134 final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
135 final NetconfHelloMessage clientHello = NetconfHelloMessage.createClientHello(Collections.<String>emptySet(), Optional.<NetconfHelloMessageAdditionalHeader>absent());
136 testingNetconfSession.sendMessage(clientHello);
137 verify(channel).writeAndFlush(clientHello);
140 private static class TestingNetconfSession extends AbstractNetconfSession<NetconfSession, NetconfSessionListener<NetconfSession>> {
142 protected TestingNetconfSession(final NetconfSessionListener<NetconfSession> sessionListener, final Channel channel, final long sessionId) {
143 super(sessionListener, channel, sessionId);
147 protected NetconfSession thisInstance() {
152 protected void addExiHandlers(final ByteToMessageDecoder decoder, final MessageToByteEncoder<NetconfMessage> encoder) {}
155 public void stopExiCommunication() {}