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