2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.connection;
11 import java.net.InetSocketAddress;
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.mockito.Mock;
17 import org.mockito.Mockito;
18 import org.mockito.runners.MockitoJUnitRunner;
19 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
20 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
21 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
22 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
23 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
24 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceDisconnectedHandler;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
28 * Test for {@link ConnectionContextImpl}.
30 @RunWith(MockitoJUnitRunner.class)
31 public class ConnectionContextImplTest {
34 private ConnectionAdapter connetionAdapter;
36 private HandshakeContext handshakeContext;
38 private OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueRegistration;
40 private DeviceDisconnectedHandler deviceDisconnectedHandler;
42 private ConnectionContextImpl connectionContext;
45 public void setUp() throws Exception {
46 Mockito.when(connetionAdapter.getRemoteAddress()).thenReturn(InetSocketAddress.createUnresolved("ofp-ut.example.org", 4242));
47 Mockito.when(connetionAdapter.isAlive()).thenReturn(true);
49 connectionContext = new ConnectionContextImpl(connetionAdapter);
50 connectionContext.setHandshakeContext(handshakeContext);
51 connectionContext.setNodeId(new NodeId("ut-node:123"));
52 connectionContext.setOutboundQueueHandleRegistration(outboundQueueRegistration);
53 connectionContext.setDeviceDisconnectedHandler(deviceDisconnectedHandler);
55 Assert.assertNull(connectionContext.getConnectionState());
59 public void testCloseConnection1() throws Exception {
60 connectionContext.closeConnection(true);
61 Mockito.verify(outboundQueueRegistration).close();
62 Mockito.verify(handshakeContext).close();
63 Mockito.verify(connetionAdapter).disconnect();
64 Assert.assertEquals(ConnectionContext.CONNECTION_STATE.RIP, connectionContext.getConnectionState());
66 Mockito.verify(deviceDisconnectedHandler).onDeviceDisconnected(connectionContext);
70 public void testCloseConnection2() throws Exception {
71 connectionContext.closeConnection(false);
72 Mockito.verify(outboundQueueRegistration).close();
73 Mockito.verify(handshakeContext).close();
74 Mockito.verify(connetionAdapter).disconnect();
75 Assert.assertEquals(ConnectionContext.CONNECTION_STATE.RIP, connectionContext.getConnectionState());
77 Mockito.verify(deviceDisconnectedHandler, Mockito.never()).onDeviceDisconnected(connectionContext);
81 public void testOnConnectionClosed() throws Exception {
82 connectionContext.onConnectionClosed();
83 Assert.assertEquals(ConnectionContext.CONNECTION_STATE.RIP, connectionContext.getConnectionState());
84 Mockito.verify(outboundQueueRegistration).close();
85 Mockito.verify(handshakeContext).close();
86 Assert.assertEquals(ConnectionContext.CONNECTION_STATE.RIP, connectionContext.getConnectionState());
87 Mockito.verify(deviceDisconnectedHandler).onDeviceDisconnected(connectionContext);
91 public void testChangeStateToHandshaking() throws Exception {
92 connectionContext.changeStateToHandshaking();
93 Assert.assertEquals(ConnectionContext.CONNECTION_STATE.HANDSHAKING, connectionContext.getConnectionState());
97 public void testChangeStateToTimeouting() throws Exception {
98 connectionContext.changeStateToTimeouting();
99 Assert.assertEquals(ConnectionContext.CONNECTION_STATE.TIMEOUTING, connectionContext.getConnectionState());
103 public void testChangeStateToWorking() throws Exception {
104 connectionContext.changeStateToWorking();
105 Assert.assertEquals(ConnectionContext.CONNECTION_STATE.WORKING, connectionContext.getConnectionState());