Move NetconfMessage into netconf.api.messages
[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
22 import io.netty.channel.Channel;
23 import io.netty.channel.ChannelFuture;
24 import io.netty.channel.ChannelHandler;
25 import io.netty.channel.ChannelPipeline;
26 import io.netty.channel.ChannelPromise;
27 import io.netty.channel.EventLoop;
28 import io.netty.handler.codec.ByteToMessageDecoder;
29 import io.netty.handler.codec.MessageToByteEncoder;
30 import java.io.EOFException;
31 import java.util.Optional;
32 import java.util.Set;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.junit.MockitoJUnitRunner;
38 import org.opendaylight.netconf.api.NetconfSessionListener;
39 import org.opendaylight.netconf.api.NetconfTerminationReason;
40 import org.opendaylight.netconf.api.messages.HelloMessage;
41 import org.opendaylight.netconf.api.messages.NetconfMessage;
42 import org.opendaylight.netconf.nettyutil.handler.exi.EXIParameters;
43 import org.opendaylight.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
45 import org.opendaylight.yangtools.yang.common.Uint32;
46
47 @RunWith(MockitoJUnitRunner.StrictStubs.class)
48 public class AbstractNetconfSessionTest {
49     private static final SessionIdType SESSION_ID = new SessionIdType(Uint32.ONE);
50
51     @Mock
52     private NetconfSessionListener<TestingNetconfSession> listener;
53     @Mock
54     private Channel channel;
55     @Mock
56     private ChannelPipeline pipeline;
57     @Mock
58     private EventLoop eventLoop;
59     @Mock
60     private ChannelPromise writeFuture;
61
62     private HelloMessage clientHello;
63
64     @Before
65     public void setUp() throws Exception {
66         doNothing().when(listener).onMessage(any(TestingNetconfSession.class), any(NetconfMessage.class));
67         doNothing().when(listener).onSessionUp(any(TestingNetconfSession.class));
68         doNothing().when(listener).onSessionDown(any(TestingNetconfSession.class), any(Exception.class));
69         doNothing().when(listener).onSessionTerminated(any(TestingNetconfSession.class),
70                 any(NetconfTerminationReason.class));
71
72         doReturn(writeFuture).when(channel).newPromise();
73         doReturn(writeFuture).when(channel).writeAndFlush(any(NetconfMessage.class), any(ChannelPromise.class));
74         doReturn(pipeline).when(channel).pipeline();
75         doReturn(mock(ChannelFuture.class)).when(channel).close();
76
77         doReturn(null).when(pipeline).replace(anyString(), anyString(), any(ChannelHandler.class));
78
79         doReturn(eventLoop).when(channel).eventLoop();
80         doAnswer(invocation -> {
81             invocation.<Runnable>getArgument(0).run();
82             return null;
83         }).when(eventLoop).execute(any(Runnable.class));
84
85         clientHello = HelloMessage.createClientHello(Set.of(), Optional.empty());
86     }
87
88     @Test
89     public void testHandleMessage() throws Exception {
90         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, SESSION_ID);
91         testingNetconfSession.handleMessage(clientHello);
92         verify(listener).onMessage(testingNetconfSession, clientHello);
93     }
94
95     @Test
96     public void testSessionUp() throws Exception {
97         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, SESSION_ID);
98         testingNetconfSession.sessionUp();
99         verify(listener).onSessionUp(testingNetconfSession);
100         assertEquals(SESSION_ID, testingNetconfSession.sessionId());
101     }
102
103     @Test
104     public void testClose() throws Exception {
105         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, SESSION_ID);
106         testingNetconfSession.sessionUp();
107         testingNetconfSession.close();
108         verify(channel).close();
109         verify(listener).onSessionTerminated(any(TestingNetconfSession.class), any(NetconfTerminationReason.class));
110     }
111
112     @Test
113     public void testReplaceHandlers() throws Exception {
114         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, SESSION_ID);
115         final ChannelHandler mock = mock(ChannelHandler.class);
116
117         testingNetconfSession.replaceMessageDecoder(mock);
118         verify(pipeline).replace(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER,
119                 AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, mock);
120         testingNetconfSession.replaceMessageEncoder(mock);
121         verify(pipeline).replace(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER,
122                 AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, mock);
123         testingNetconfSession.replaceMessageEncoderAfterNextMessage(mock);
124         verifyNoMoreInteractions(pipeline);
125
126         testingNetconfSession.sendMessage(clientHello);
127         verify(pipeline, times(2)).replace(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER,
128                 AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, mock);
129     }
130
131     @Test
132     public void testStartExi() throws Exception {
133         TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, SESSION_ID);
134         testingNetconfSession = spy(testingNetconfSession);
135
136         testingNetconfSession.startExiCommunication(NetconfStartExiMessage.create(EXIParameters.empty(), "4"));
137         verify(testingNetconfSession).addExiHandlers(any(ByteToMessageDecoder.class), any(MessageToByteEncoder.class));
138     }
139
140     @Test
141     public void testEndOfInput() throws Exception {
142         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, SESSION_ID);
143         testingNetconfSession.endOfInput();
144         verifyNoMoreInteractions(listener);
145         testingNetconfSession.sessionUp();
146         testingNetconfSession.endOfInput();
147         verify(listener).onSessionDown(any(TestingNetconfSession.class), any(EOFException.class));
148     }
149
150     @Test
151     public void testSendMessage() throws Exception {
152         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, SESSION_ID);
153         final HelloMessage hello = HelloMessage.createClientHello(Set.of(), Optional.empty());
154         testingNetconfSession.sendMessage(hello);
155         verify(channel).writeAndFlush(hello, writeFuture);
156     }
157 }