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