Remove netconf from commons/opendaylight pom
[controller.git] / opendaylight / netconf / netconf-client / src / test / java / org / opendaylight / controller / 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.controller.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 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;
32 import java.util.Set;
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;
49
50 public class NetconfClientSessionNegotiatorTest {
51
52     private NetconfHelloMessage helloMessage;
53     private ChannelPipeline pipeline;
54     private ChannelFuture future;
55     private Channel channel;
56     private ChannelInboundHandlerAdapter channelInboundHandlerAdapter;
57
58     @Before
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     }
65
66     private static ChannelHandler mockChannelHandler() {
67         ChannelHandler handler = mock(ChannelHandler.class);
68         return handler;
69     }
70
71     private Channel mockChannel() {
72         Channel channel = mock(Channel.class);
73         ChannelHandler channelHandler = mockChannelHandler();
74         doReturn("").when(channel).toString();
75         doReturn(future).when(channel).close();
76         doReturn(future).when(channel).writeAndFlush(anyObject());
77         doReturn(true).when(channel).isOpen();
78         doReturn(pipeline).when(channel).pipeline();
79         doReturn("").when(pipeline).toString();
80         doReturn(pipeline).when(pipeline).remove(any(ChannelHandler.class));
81         doReturn(channelHandler).when(pipeline).remove(anyString());
82         return channel;
83     }
84
85     private static ChannelFuture mockChannelFuture() {
86         ChannelFuture future = mock(ChannelFuture.class);
87         doReturn(future).when(future).addListener(any(GenericFutureListener.class));
88         return future;
89     }
90
91     private static ChannelPipeline mockChannelPipeline() {
92         ChannelPipeline pipeline = mock(ChannelPipeline.class);
93         ChannelHandler handler = mock(ChannelHandler.class);
94         doReturn(pipeline).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
95         doReturn(null).when(pipeline).get(SslHandler.class);
96         doReturn(pipeline).when(pipeline).addLast(anyString(), any(ChannelHandler.class));
97         doReturn(handler).when(pipeline).replace(anyString(), anyString(), any(ChunkedFramingMechanismEncoder.class));
98
99         NetconfXMLToHelloMessageDecoder messageDecoder = new NetconfXMLToHelloMessageDecoder();
100         doReturn(messageDecoder).when(pipeline).replace(anyString(), anyString(), any(NetconfXMLToMessageDecoder.class));
101         doReturn(pipeline).when(pipeline).replace(any(ChannelHandler.class), anyString(), any(NetconfClientSession.class));
102         return pipeline;
103     }
104
105     private NetconfClientSessionNegotiator createNetconfClientSessionNegotiator(final Promise<NetconfClientSession> promise,
106                                                                                 final NetconfMessage startExi) {
107         ChannelProgressivePromise progressivePromise = mock(ChannelProgressivePromise.class);
108         NetconfClientSessionPreferences preferences = new NetconfClientSessionPreferences(helloMessage, startExi);
109         doReturn(progressivePromise).when(promise).setFailure(any(Throwable.class));
110
111         long timeout = 10L;
112         NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
113         Timer timer = new HashedWheelTimer();
114         return new NetconfClientSessionNegotiator(preferences, promise, channel, timer, sessionListener, timeout);
115     }
116
117     @Test
118     public void testNetconfClientSessionNegotiator() throws Exception {
119         Promise promise = mock(Promise.class);
120         doReturn(promise).when(promise).setSuccess(anyObject());
121         NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
122
123         negotiator.channelActive(null);
124         Set<String> caps = Sets.newSet("a", "b");
125         NetconfHelloMessage helloServerMessage = NetconfHelloMessage.createServerHello(caps, 10);
126         negotiator.handleMessage(helloServerMessage);
127         verify(promise).setSuccess(anyObject());
128     }
129
130     @Test
131     public void testNetconfClientSessionNegotiatorWithEXI() throws Exception {
132         Promise promise = mock(Promise.class);
133         EXIOptions exiOptions = new EXIOptions();
134         NetconfStartExiMessage exiMessage = NetconfStartExiMessage.create(exiOptions, "msg-id");
135         doReturn(promise).when(promise).setSuccess(anyObject());
136         NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, exiMessage);
137
138         negotiator.channelActive(null);
139         Set<String> caps = Sets.newSet("exi:1.0");
140         NetconfHelloMessage helloMessage = NetconfHelloMessage.createServerHello(caps, 10);
141
142         doAnswer(new Answer<Object>() {
143             @Override
144             public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
145                 channelInboundHandlerAdapter = ((ChannelInboundHandlerAdapter) invocationOnMock.getArguments()[2]);
146                 return null;
147             }
148         }).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
149
150         ChannelHandlerContext handlerContext = mock(ChannelHandlerContext.class);
151         doReturn(pipeline).when(handlerContext).pipeline();
152         negotiator.handleMessage(helloMessage);
153         Document expectedResult = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
154         channelInboundHandlerAdapter.channelRead(handlerContext, new NetconfMessage(expectedResult));
155
156         verify(promise).setSuccess(anyObject());
157
158         // two calls for exiMessage, 2 for hello message
159         verify(pipeline, times(4)).replace(anyString(), anyString(), any(ChannelHandler.class));
160     }
161 }