BUG-4340: raise test coverage to 80% - statistics related
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / connection / ConnectionManagerImplTest.java
1 /**
2  * Copyright (c) 2015 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.openflowplugin.impl.connection;
9
10 import com.google.common.util.concurrent.SettableFuture;
11 import java.math.BigInteger;
12 import java.net.InetSocketAddress;
13 import org.junit.After;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.ArgumentCaptor;
18 import org.mockito.Captor;
19 import org.mockito.Matchers;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.runners.MockitoJUnitRunner;
23 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
24 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
25 import org.opendaylight.openflowplugin.api.OFConstants;
26 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
27 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
37
38 /**
39  * test of {@link ConnectionManagerImpl} - lightweight version, using basic ways (TDD)
40  */
41 @RunWith(MockitoJUnitRunner.class)
42 public class ConnectionManagerImplTest {
43
44     /** timeout of final step [ms] */
45     private static final int FINAL_STEP_TIMEOUT = 500;
46     private ConnectionManagerImpl connectionManagerImpl;
47     @Mock
48     private ConnectionAdapter connection;
49     @Mock
50     private DeviceConnectedHandler deviceConnectedHandler;
51     @Captor
52     private ArgumentCaptor<ConnectionReadyListener> connectionReadyListenerAC;
53     @Captor
54     private ArgumentCaptor<OpenflowProtocolListener> ofpListenerAC;
55
56     /**
57      * before each test method
58      */
59     @Before
60     public void setUp() {
61         connectionManagerImpl = new ConnectionManagerImpl();
62         connectionManagerImpl.setDeviceConnectedHandler(deviceConnectedHandler);
63         final InetSocketAddress deviceAddress = InetSocketAddress.createUnresolved("yahoo", 42);
64         Mockito.when(connection.getRemoteAddress()).thenReturn(deviceAddress);
65         Mockito.when(connection.isAlive()).thenReturn(true);
66     }
67
68     /**
69      * after each test method
70      * @throws InterruptedException
71      */
72     @After
73     public void tearDown() throws InterruptedException {
74         Thread.sleep(200L);
75     }
76
77     /**
78      * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
79      * invoking onConnectionReady first, scenario:
80      * <ol>
81      *  <li>send hello to device (rpc with void output)</li>
82      *  <li>receive hello from device (notification)</li>
83      *  <li>send getFeature to device (rpc with getFeatureOutput)</li>
84      *  <li>wait for rpc to finish with getFeatureOutput</li>
85      * </ol>
86      * @throws InterruptedException
87      */
88     @Test
89     public void testOnSwitchConnected1() throws InterruptedException {
90         connectionManagerImpl.onSwitchConnected(connection);
91         Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
92         Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
93
94         // prepare void reply (hello rpc output)
95         final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
96         Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
97         // prepare getFeature reply (getFeture rpc output)
98         final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
99         Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
100
101
102         // fire handshake
103         connectionReadyListenerAC.getValue().onConnectionReady();
104
105         // deliver hello send output (void)
106         Thread.sleep(100L);
107         final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
108         voidResponseFx.set(helloResponse);
109
110         // send hello reply
111         final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
112         ofpListenerAC.getValue().onHelloMessage(hello);
113
114         // deliver getFeature output
115         Thread.sleep(100L);
116         final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
117         .setDatapathId(BigInteger.TEN)
118         .setVersion(OFConstants.OFP_VERSION_1_3)
119         .setXid(2L)
120         .setTables((short) 15)
121         .build();
122         final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
123         featureResponseFx.set(rpcFeaturesOutput);
124
125         Mockito.verify(deviceConnectedHandler, Mockito.timeout(500)).deviceConnected(Matchers.any(ConnectionContext.class));
126     }
127
128     /**
129      * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
130      * invoking onHelloMessage, scenario:
131      * <ol>
132      *  <li>receive hello from device (notification)</li>
133      *  <li>send hello to device (rpc with void output)</li>
134      *  <li>send getFeature to device (rpc with getFeatureOutput)</li>
135      *  <li>wait for rpc to finish with getFeatureOutput</li>
136      * </ol>
137      * @throws InterruptedException
138      */
139     @Test
140     public void testOnSwitchConnected2() throws InterruptedException {
141         connectionManagerImpl.onSwitchConnected(connection);
142         Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
143         Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
144
145         // prepare void reply (hello rpc output)
146         final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
147         Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
148         // prepare getFeature reply (getFeture rpc output)
149         final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
150         Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
151
152
153         // fire handshake - send hello reply
154         final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
155         ofpListenerAC.getValue().onHelloMessage(hello);
156
157         // notify about connection ready
158         connectionReadyListenerAC.getValue().onConnectionReady();
159
160         // deliver hello send output (void)
161         Thread.sleep(100L);
162         final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
163         voidResponseFx.set(helloResponse);
164
165         // deliver getFeature output
166         Thread.sleep(100L);
167         final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
168         .setDatapathId(BigInteger.TEN)
169         .setVersion(OFConstants.OFP_VERSION_1_3)
170         .setXid(2L)
171         .setTables((short) 15)
172         .build();
173         final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
174         featureResponseFx.set(rpcFeaturesOutput);
175
176         Mockito.verify(deviceConnectedHandler, Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(Matchers.any(ConnectionContext.class));
177     }
178 }