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.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;
19 import com.google.common.base.Optional;
20 import io.netty.channel.Channel;
21 import io.netty.channel.ChannelFuture;
22 import io.netty.channel.ChannelHandler;
23 import io.netty.channel.ChannelHandlerContext;
24 import io.netty.channel.ChannelInboundHandlerAdapter;
25 import io.netty.channel.ChannelPipeline;
26 import io.netty.channel.ChannelProgressivePromise;
27 import io.netty.handler.ssl.SslHandler;
28 import io.netty.util.HashedWheelTimer;
29 import io.netty.util.Timer;
30 import io.netty.util.concurrent.GenericFutureListener;
31 import io.netty.util.concurrent.Promise;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.internal.util.collections.Sets;
36 import org.mockito.invocation.InvocationOnMock;
37 import org.mockito.stubbing.Answer;
38 import org.opendaylight.controller.netconf.api.NetconfClientSessionPreferences;
39 import org.opendaylight.controller.netconf.api.NetconfMessage;
40 import org.opendaylight.controller.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
41 import org.opendaylight.controller.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
42 import org.opendaylight.controller.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
43 import org.opendaylight.controller.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
44 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
45 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
46 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
47 import org.openexi.proc.common.EXIOptions;
48 import org.w3c.dom.Document;
50 public class NetconfClientSessionNegotiatorTest {
52 private NetconfHelloMessage helloMessage;
53 private ChannelPipeline pipeline;
54 private ChannelFuture future;
55 private Channel channel;
56 private ChannelInboundHandlerAdapter channelInboundHandlerAdapter;
59 public void setUp() throws Exception {
60 helloMessage = NetconfHelloMessage.createClientHello(Sets.newSet("exi:1.0"), Optional.<NetconfHelloMessageAdditionalHeader>absent());
61 pipeline = mockChannelPipeline();
62 future = mockChannelFuture();
63 channel = mockChannel();
64 System.out.println("setup done");
67 private ChannelHandler mockChannelHandler() {
68 ChannelHandler handler = mock(ChannelHandler.class);
72 private Channel mockChannel() {
73 Channel channel = mock(Channel.class);
74 ChannelHandler channelHandler = mockChannelHandler();
75 doReturn("").when(channel).toString();
76 doReturn(future).when(channel).close();
77 doReturn(future).when(channel).writeAndFlush(anyObject());
78 doReturn(true).when(channel).isOpen();
79 doReturn(pipeline).when(channel).pipeline();
80 doReturn("").when(pipeline).toString();
81 doReturn(pipeline).when(pipeline).remove(any(ChannelHandler.class));
82 doReturn(channelHandler).when(pipeline).remove(anyString());
86 private ChannelFuture mockChannelFuture() {
87 ChannelFuture future = mock(ChannelFuture.class);
88 doReturn(future).when(future).addListener(any(GenericFutureListener.class));
92 private ChannelPipeline mockChannelPipeline() {
93 ChannelPipeline pipeline = mock(ChannelPipeline.class);
94 ChannelHandler handler = mock(ChannelHandler.class);
95 doReturn(pipeline).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
96 doReturn(null).when(pipeline).get(SslHandler.class);
97 doReturn(pipeline).when(pipeline).addLast(anyString(), any(ChannelHandler.class));
98 doReturn(handler).when(pipeline).replace(anyString(), anyString(), any(ChunkedFramingMechanismEncoder.class));
100 NetconfXMLToHelloMessageDecoder messageDecoder = new NetconfXMLToHelloMessageDecoder();
101 doReturn(messageDecoder).when(pipeline).replace(anyString(), anyString(), any(NetconfXMLToMessageDecoder.class));
102 doReturn(pipeline).when(pipeline).replace(any(ChannelHandler.class), anyString(), any(NetconfClientSession.class));
106 private NetconfClientSessionNegotiator createNetconfClientSessionNegotiator(final Promise promise,
107 final NetconfMessage startExi) {
108 ChannelProgressivePromise progressivePromise = mock(ChannelProgressivePromise.class);
109 NetconfClientSessionPreferences preferences = new NetconfClientSessionPreferences(helloMessage, startExi);
110 doReturn(progressivePromise).when(promise).setFailure(any(Throwable.class));
113 NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
114 Timer timer = new HashedWheelTimer();
115 return new NetconfClientSessionNegotiator(preferences, promise, channel, timer, sessionListener, timeout);
119 public void testNetconfClientSessionNegotiator() throws Exception {
120 Promise promise = mock(Promise.class);
121 doReturn(promise).when(promise).setSuccess(anyObject());
122 NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
124 negotiator.channelActive(null);
125 Set caps = Sets.newSet("a", "b");
126 NetconfHelloMessage helloServerMessage = NetconfHelloMessage.createServerHello(caps, 10);
127 negotiator.handleMessage(helloServerMessage);
128 verify(promise).setSuccess(anyObject());
132 public void testNetconfClientSessionNegotiatorWithEXI() throws Exception {
133 Promise promise = mock(Promise.class);
134 EXIOptions exiOptions = new EXIOptions();
135 NetconfStartExiMessage exiMessage = NetconfStartExiMessage.create(exiOptions, "msg-id");
136 doReturn(promise).when(promise).setSuccess(anyObject());
137 NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, exiMessage);
139 negotiator.channelActive(null);
140 Set caps = Sets.newSet("exi:1.0");
141 NetconfHelloMessage helloMessage = NetconfHelloMessage.createServerHello(caps, 10);
143 doAnswer(new Answer<Object>() {
145 public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
146 channelInboundHandlerAdapter = ((ChannelInboundHandlerAdapter) invocationOnMock.getArguments()[2]);
149 }).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
151 ChannelHandlerContext handlerContext = mock(ChannelHandlerContext.class);
152 doReturn(pipeline).when(handlerContext).pipeline();
153 negotiator.handleMessage(helloMessage);
154 Document expectedResult = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
155 channelInboundHandlerAdapter.channelRead(handlerContext, new NetconfMessage(expectedResult));
157 verify(promise).setSuccess(anyObject());
159 // two calls for exiMessage, 2 for hello message
160 verify(pipeline, times(4)).replace(anyString(), anyString(), any(ChannelHandler.class));