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.client;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyObject;
13 import static org.mockito.Matchers.anyString;
14 import static org.mockito.Mockito.doAnswer;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
20 import com.google.common.base.Optional;
21 import io.netty.channel.Channel;
22 import io.netty.channel.ChannelFuture;
23 import io.netty.channel.ChannelHandler;
24 import io.netty.channel.ChannelHandlerContext;
25 import io.netty.channel.ChannelInboundHandlerAdapter;
26 import io.netty.channel.ChannelPipeline;
27 import io.netty.channel.ChannelProgressivePromise;
28 import io.netty.channel.EventLoop;
29 import io.netty.handler.ssl.SslHandler;
30 import io.netty.util.HashedWheelTimer;
31 import io.netty.util.Timer;
32 import io.netty.util.concurrent.GenericFutureListener;
33 import io.netty.util.concurrent.Promise;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.internal.util.collections.Sets;
38 import org.mockito.invocation.InvocationOnMock;
39 import org.mockito.stubbing.Answer;
40 import org.opendaylight.netconf.api.NetconfClientSessionPreferences;
41 import org.opendaylight.netconf.api.NetconfMessage;
42 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
43 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
44 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
45 import org.opendaylight.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
46 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
47 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
48 import org.opendaylight.netconf.util.test.XmlFileLoader;
49 import org.openexi.proc.common.EXIOptions;
50 import org.w3c.dom.Document;
52 public class NetconfClientSessionNegotiatorTest {
54 private NetconfHelloMessage helloMessage;
55 private ChannelPipeline pipeline;
56 private ChannelFuture future;
57 private Channel channel;
58 private ChannelInboundHandlerAdapter channelInboundHandlerAdapter;
61 public void setUp() throws Exception {
62 helloMessage = NetconfHelloMessage.createClientHello(Sets.newSet("exi:1.0"), Optional.<NetconfHelloMessageAdditionalHeader>absent());
63 pipeline = mockChannelPipeline();
64 future = mockChannelFuture();
65 channel = mockChannel();
69 private static ChannelHandler mockChannelHandler() {
70 ChannelHandler handler = mock(ChannelHandler.class);
74 private Channel mockChannel() {
75 Channel channel = mock(Channel.class);
76 ChannelHandler channelHandler = mockChannelHandler();
77 doReturn("").when(channel).toString();
78 doReturn(future).when(channel).close();
79 doReturn(future).when(channel).writeAndFlush(anyObject());
80 doReturn(true).when(channel).isOpen();
81 doReturn(pipeline).when(channel).pipeline();
82 doReturn("").when(pipeline).toString();
83 doReturn(pipeline).when(pipeline).remove(any(ChannelHandler.class));
84 doReturn(channelHandler).when(pipeline).remove(anyString());
88 private static ChannelFuture mockChannelFuture() {
89 ChannelFuture future = mock(ChannelFuture.class);
90 doReturn(future).when(future).addListener(any(GenericFutureListener.class));
94 private static ChannelPipeline mockChannelPipeline() {
95 ChannelPipeline pipeline = mock(ChannelPipeline.class);
96 ChannelHandler handler = mock(ChannelHandler.class);
97 doReturn(pipeline).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
98 doReturn(null).when(pipeline).get(SslHandler.class);
99 doReturn(pipeline).when(pipeline).addLast(anyString(), any(ChannelHandler.class));
100 doReturn(handler).when(pipeline).replace(anyString(), anyString(), any(ChunkedFramingMechanismEncoder.class));
102 NetconfXMLToHelloMessageDecoder messageDecoder = new NetconfXMLToHelloMessageDecoder();
103 doReturn(messageDecoder).when(pipeline).replace(anyString(), anyString(), any(NetconfXMLToMessageDecoder.class));
104 doReturn(pipeline).when(pipeline).replace(any(ChannelHandler.class), anyString(), any(NetconfClientSession.class));
108 private void mockEventLoop() {
109 final EventLoop eventLoop = mock(EventLoop.class);
110 doReturn(eventLoop).when(channel).eventLoop();
111 doAnswer(new Answer<Void>() {
113 public Void answer(InvocationOnMock invocation) throws Throwable {
114 final Object[] args = invocation.getArguments();
115 final Runnable runnable = (Runnable) args[0];
119 }).when(eventLoop).execute(any(Runnable.class));
122 private NetconfClientSessionNegotiator createNetconfClientSessionNegotiator(final Promise<NetconfClientSession> promise,
123 final NetconfMessage startExi) {
124 ChannelProgressivePromise progressivePromise = mock(ChannelProgressivePromise.class);
125 NetconfClientSessionPreferences preferences = new NetconfClientSessionPreferences(helloMessage, startExi);
126 doReturn(progressivePromise).when(promise).setFailure(any(Throwable.class));
129 NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
130 Timer timer = new HashedWheelTimer();
131 return new NetconfClientSessionNegotiator(preferences, promise, channel, timer, sessionListener, timeout);
135 public void testNetconfClientSessionNegotiator() throws Exception {
136 Promise promise = mock(Promise.class);
137 doReturn(promise).when(promise).setSuccess(anyObject());
138 NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
140 negotiator.channelActive(null);
141 Set<String> caps = Sets.newSet("a", "b");
142 NetconfHelloMessage helloServerMessage = NetconfHelloMessage.createServerHello(caps, 10);
143 negotiator.handleMessage(helloServerMessage);
144 verify(promise).setSuccess(anyObject());
148 public void testNetconfClientSessionNegotiatorWithEXI() throws Exception {
149 Promise promise = mock(Promise.class);
150 EXIOptions exiOptions = new EXIOptions();
151 NetconfStartExiMessage exiMessage = NetconfStartExiMessage.create(exiOptions, "msg-id");
152 doReturn(promise).when(promise).setSuccess(anyObject());
153 NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, exiMessage);
155 negotiator.channelActive(null);
156 Set<String> caps = Sets.newSet("exi:1.0");
157 NetconfHelloMessage helloMessage = NetconfHelloMessage.createServerHello(caps, 10);
159 doAnswer(new Answer<Object>() {
161 public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
162 channelInboundHandlerAdapter = ((ChannelInboundHandlerAdapter) invocationOnMock.getArguments()[2]);
165 }).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
167 ChannelHandlerContext handlerContext = mock(ChannelHandlerContext.class);
168 doReturn(pipeline).when(handlerContext).pipeline();
169 negotiator.handleMessage(helloMessage);
170 Document expectedResult = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
171 channelInboundHandlerAdapter.channelRead(handlerContext, new NetconfMessage(expectedResult));
173 verify(promise).setSuccess(anyObject());
175 // two calls for exiMessage, 2 for hello message
176 verify(pipeline, times(4)).replace(anyString(), anyString(), any(ChannelHandler.class));