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