Integrate MRI projects for Neon
[netconf.git] / netconf / netconf-netty-util / src / test / java / org / opendaylight / netconf / nettyutil / AbstractNetconfSessionTest.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.nettyutil;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.anyString;
14 import static org.mockito.Mockito.doAnswer;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.spy;
19 import static org.mockito.Mockito.times;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.verifyNoMoreInteractions;
22 import static org.mockito.Mockito.verifyZeroInteractions;
23
24 import com.google.common.base.Optional;
25 import io.netty.channel.Channel;
26 import io.netty.channel.ChannelFuture;
27 import io.netty.channel.ChannelHandler;
28 import io.netty.channel.ChannelPipeline;
29 import io.netty.channel.ChannelPromise;
30 import io.netty.channel.EventLoop;
31 import io.netty.handler.codec.ByteToMessageDecoder;
32 import io.netty.handler.codec.MessageToByteEncoder;
33 import io.netty.util.concurrent.GenericFutureListener;
34 import java.util.Collections;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.opendaylight.netconf.api.NetconfMessage;
40 import org.opendaylight.netconf.api.NetconfSessionListener;
41 import org.opendaylight.netconf.api.NetconfTerminationReason;
42 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
43 import org.opendaylight.netconf.nettyutil.handler.exi.EXIParameters;
44 import org.opendaylight.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
45
46 public class AbstractNetconfSessionTest {
47
48     @Mock
49     private NetconfSessionListener<TestingNetconfSession> listener;
50     @Mock
51     private Channel channel;
52     @Mock
53     private ChannelPipeline pipeline;
54     @Mock
55     private EventLoop eventLoop;
56     @Mock
57     private ChannelPromise writeFuture;
58
59     private NetconfHelloMessage clientHello;
60
61     @Before
62     public void setUp() throws Exception {
63         MockitoAnnotations.initMocks(this);
64         doNothing().when(listener).onMessage(any(TestingNetconfSession.class), any(NetconfMessage.class));
65         doNothing().when(listener).onSessionUp(any(TestingNetconfSession.class));
66         doNothing().when(listener).onSessionDown(any(TestingNetconfSession.class), any(Exception.class));
67         doNothing().when(listener).onSessionTerminated(any(TestingNetconfSession.class),
68                 any(NetconfTerminationReason.class));
69
70         doReturn(writeFuture).when(writeFuture).addListener(any(GenericFutureListener.class));
71
72         doReturn(writeFuture).when(channel).newPromise();
73         doReturn(writeFuture).when(channel).writeAndFlush(any(NetconfMessage.class));
74         doReturn(writeFuture).when(channel).writeAndFlush(any(NetconfMessage.class), any(ChannelPromise.class));
75         doReturn(pipeline).when(channel).pipeline();
76         doReturn("mockChannel").when(channel).toString();
77         doReturn(mock(ChannelFuture.class)).when(channel).close();
78
79         doReturn(null).when(pipeline).replace(anyString(), anyString(), any(ChannelHandler.class));
80
81         doReturn(eventLoop).when(channel).eventLoop();
82         doAnswer(invocation -> {
83             invocation.<Runnable>getArgument(0).run();
84             return null;
85         }).when(eventLoop).execute(any(Runnable.class));
86
87         clientHello = NetconfHelloMessage.createClientHello(Collections.emptySet(), Optional.absent());
88     }
89
90     @Test
91     public void testHandleMessage() throws Exception {
92         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
93         testingNetconfSession.handleMessage(clientHello);
94         verify(listener).onMessage(testingNetconfSession, clientHello);
95     }
96
97     @Test
98     public void testSessionUp() throws Exception {
99         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
100         testingNetconfSession.sessionUp();
101         verify(listener).onSessionUp(testingNetconfSession);
102         assertEquals(1L, testingNetconfSession.getSessionId());
103     }
104
105     @Test
106     public void testClose() throws Exception {
107         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
108         testingNetconfSession.sessionUp();
109         testingNetconfSession.close();
110         verify(channel).close();
111         verify(listener).onSessionTerminated(any(TestingNetconfSession.class), any(NetconfTerminationReason.class));
112     }
113
114     @Test
115     public void testReplaceHandlers() throws Exception {
116         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
117         final ChannelHandler mock = mock(ChannelHandler.class);
118         doReturn("handler").when(mock).toString();
119
120         testingNetconfSession.replaceMessageDecoder(mock);
121         verify(pipeline).replace(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER,
122                 AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, mock);
123         testingNetconfSession.replaceMessageEncoder(mock);
124         verify(pipeline).replace(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER,
125                 AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, mock);
126         testingNetconfSession.replaceMessageEncoderAfterNextMessage(mock);
127         verifyNoMoreInteractions(pipeline);
128
129         testingNetconfSession.sendMessage(clientHello);
130         verify(pipeline, times(2)).replace(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER,
131                 AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, mock);
132     }
133
134     @Test
135     public void testStartExi() throws Exception {
136         TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
137         testingNetconfSession = spy(testingNetconfSession);
138
139         testingNetconfSession.startExiCommunication(NetconfStartExiMessage.create(EXIParameters.empty(), "4"));
140         verify(testingNetconfSession).addExiHandlers(any(ByteToMessageDecoder.class), any(MessageToByteEncoder.class));
141     }
142
143     @Test
144     public void testEndOfInput() throws Exception {
145         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
146         testingNetconfSession.endOfInput();
147         verifyZeroInteractions(listener);
148         testingNetconfSession.sessionUp();
149         testingNetconfSession.endOfInput();
150         verify(listener).onSessionDown(any(TestingNetconfSession.class), any(Exception.class));
151     }
152
153     @Test
154     public void testSendMessage() throws Exception {
155         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
156         final NetconfHelloMessage hello = NetconfHelloMessage.createClientHello(Collections.emptySet(),
157             Optional.absent());
158         testingNetconfSession.sendMessage(hello);
159         verify(channel).writeAndFlush(hello, writeFuture);
160     }
161
162 }