Move NetconfHelloMessage to netconf-api
[netconf.git] / opendaylight / netconf / netconf-client / src / test / java / org / opendaylight / netconf / client / NetconfClientSessionNegotiatorTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.client;
10
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
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;
34 import java.util.Set;
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;
51
52 public class NetconfClientSessionNegotiatorTest {
53
54     private NetconfHelloMessage helloMessage;
55     private ChannelPipeline pipeline;
56     private ChannelFuture future;
57     private Channel channel;
58     private ChannelInboundHandlerAdapter channelInboundHandlerAdapter;
59
60     @Before
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();
66         mockEventLoop();
67     }
68
69     private static ChannelHandler mockChannelHandler() {
70         ChannelHandler handler = mock(ChannelHandler.class);
71         return handler;
72     }
73
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());
85         return channel;
86     }
87
88     private static ChannelFuture mockChannelFuture() {
89         ChannelFuture future = mock(ChannelFuture.class);
90         doReturn(future).when(future).addListener(any(GenericFutureListener.class));
91         return future;
92     }
93
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));
101
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));
105         return pipeline;
106     }
107
108     private void mockEventLoop() {
109         final EventLoop eventLoop = mock(EventLoop.class);
110         doReturn(eventLoop).when(channel).eventLoop();
111         doAnswer(new Answer<Void>() {
112             @Override
113             public Void answer(InvocationOnMock invocation) throws Throwable {
114                 final Object[] args = invocation.getArguments();
115                 final Runnable runnable = (Runnable) args[0];
116                 runnable.run();
117                 return null;
118             }
119         }).when(eventLoop).execute(any(Runnable.class));
120     }
121
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));
127
128         long timeout = 10L;
129         NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
130         Timer timer = new HashedWheelTimer();
131         return new NetconfClientSessionNegotiator(preferences, promise, channel, timer, sessionListener, timeout);
132     }
133
134     @Test
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);
139
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());
145     }
146
147     @Test
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);
154
155         negotiator.channelActive(null);
156         Set<String> caps = Sets.newSet("exi:1.0");
157         NetconfHelloMessage helloMessage = NetconfHelloMessage.createServerHello(caps, 10);
158
159         doAnswer(new Answer<Object>() {
160             @Override
161             public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
162                 channelInboundHandlerAdapter = ((ChannelInboundHandlerAdapter) invocationOnMock.getArguments()[2]);
163                 return null;
164             }
165         }).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
166
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));
172
173         verify(promise).setSuccess(anyObject());
174
175         // two calls for exiMessage, 2 for hello message
176         verify(pipeline, times(4)).replace(anyString(), anyString(), any(ChannelHandler.class));
177     }
178 }