Fix improper cleanup of operational data in sal-netconf-connector's disconnect
[controller.git] / opendaylight / netconf / netconf-netty-util / src / test / java / org / opendaylight / controller / 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.controller.netconf.nettyutil;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.anyString;
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 com.google.common.base.Optional;
24 import io.netty.channel.Channel;
25 import io.netty.channel.ChannelFuture;
26 import io.netty.channel.ChannelHandler;
27 import io.netty.channel.ChannelPipeline;
28 import java.util.Collections;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.opendaylight.controller.netconf.api.NetconfMessage;
34 import org.opendaylight.controller.netconf.api.NetconfSession;
35 import org.opendaylight.controller.netconf.api.NetconfSessionListener;
36 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
37 import org.opendaylight.controller.netconf.nettyutil.handler.NetconfEXICodec;
38 import org.opendaylight.controller.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
39 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
40 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
41 import org.openexi.proc.common.EXIOptions;
42
43 public class AbstractNetconfSessionTest {
44
45     @Mock
46     private NetconfSessionListener<NetconfSession> listener;
47     @Mock
48     private Channel channel;
49     @Mock
50     private ChannelPipeline pipeline;
51     private NetconfHelloMessage clientHello;
52
53     @Before
54     public void setUp() throws Exception {
55         MockitoAnnotations.initMocks(this);
56         doNothing().when(listener).onMessage(any(NetconfSession.class), any(NetconfMessage.class));
57         doNothing().when(listener).onSessionUp(any(NetconfSession.class));
58         doNothing().when(listener).onSessionDown(any(NetconfSession.class), any(Exception.class));
59         doNothing().when(listener).onSessionTerminated(any(NetconfSession.class), any(NetconfTerminationReason.class));
60
61         doReturn(mock(ChannelFuture.class)).when(channel).writeAndFlush(any(NetconfMessage.class));
62         doReturn(pipeline).when(channel).pipeline();
63         doReturn(mock(ChannelFuture.class)).when(channel).close();
64
65         doReturn(null).when(pipeline).replace(anyString(), anyString(), any(ChannelHandler.class));
66
67         clientHello = NetconfHelloMessage.createClientHello(Collections.<String>emptySet(), Optional.<NetconfHelloMessageAdditionalHeader>absent());
68     }
69
70     @Test
71     public void testHandleMessage() throws Exception {
72         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
73         testingNetconfSession.handleMessage(clientHello);
74         verify(listener).onMessage(testingNetconfSession, clientHello);
75     }
76
77     @Test
78     public void testSessionUp() throws Exception {
79         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
80         testingNetconfSession.sessionUp();
81         verify(listener).onSessionUp(testingNetconfSession);
82         assertEquals(1L, testingNetconfSession.getSessionId());
83     }
84
85     @Test
86     public void testClose() throws Exception {
87         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
88         testingNetconfSession.sessionUp();
89         testingNetconfSession.close();
90         verify(channel).close();
91         verify(listener).onSessionTerminated(any(NetconfSession.class), any(NetconfTerminationReason.class));
92     }
93
94     @Test
95     public void testReplaceHandlers() throws Exception {
96         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
97         final ChannelHandler mock = mock(ChannelHandler.class);
98         doReturn("handler").when(mock).toString();
99
100         testingNetconfSession.replaceMessageDecoder(mock);
101         verify(pipeline).replace(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, mock);
102         testingNetconfSession.replaceMessageEncoder(mock);
103         verify(pipeline).replace(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, mock);
104         testingNetconfSession.replaceMessageEncoderAfterNextMessage(mock);
105         verifyNoMoreInteractions(pipeline);
106
107         testingNetconfSession.sendMessage(clientHello);
108         verify(pipeline, times(2)).replace(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, mock);
109     }
110
111     @Test
112     public void testStartExi() throws Exception {
113         TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
114         testingNetconfSession = spy(testingNetconfSession);
115
116         testingNetconfSession.startExiCommunication(NetconfStartExiMessage.create(new EXIOptions(), "4"));
117         verify(testingNetconfSession).addExiHandlers(any(NetconfEXICodec.class));
118     }
119
120     @Test
121     public void testEndOfInput() throws Exception {
122         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
123         testingNetconfSession.endOfInput();
124         verifyZeroInteractions(listener);
125         testingNetconfSession.sessionUp();
126         testingNetconfSession.endOfInput();
127         verify(listener).onSessionDown(any(NetconfSession.class), any(Exception.class));
128     }
129
130     @Test
131     public void testSendMessage() throws Exception {
132         final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
133         final NetconfHelloMessage clientHello = NetconfHelloMessage.createClientHello(Collections.<String>emptySet(), Optional.<NetconfHelloMessageAdditionalHeader>absent());
134         testingNetconfSession.sendMessage(clientHello);
135         verify(channel).writeAndFlush(clientHello);
136     }
137
138     private static class TestingNetconfSession extends AbstractNetconfSession<NetconfSession, NetconfSessionListener<NetconfSession>> {
139
140         protected TestingNetconfSession(final NetconfSessionListener<NetconfSession> sessionListener, final Channel channel, final long sessionId) {
141             super(sessionListener, channel, sessionId);
142         }
143
144         @Override
145         protected NetconfSession thisInstance() {
146             return this;
147         }
148
149         @Override
150         protected void addExiHandlers(final NetconfEXICodec exiCodec) {}
151
152         @Override
153         public void stopExiCommunication() {}
154     }
155 }