Merge "BUG-4326: Eliminate dependency to org.apache.mina"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / connection / ConnectionContextImplTest.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
9 package org.opendaylight.openflowplugin.impl.connection;
10
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;
26
27 /**
28  * Test for {@link ConnectionContextImpl}.
29  */
30 @RunWith(MockitoJUnitRunner.class)
31 public class ConnectionContextImplTest {
32
33     @Mock
34     private ConnectionAdapter connetionAdapter;
35     @Mock
36     private HandshakeContext handshakeContext;
37     @Mock
38     private OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueRegistration;
39     @Mock
40     private DeviceDisconnectedHandler deviceDisconnectedHandler;
41
42     private ConnectionContextImpl connectionContext;
43
44     @Before
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);
48
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);
54
55         Assert.assertNull(connectionContext.getConnectionState());
56     }
57
58     @Test
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());
65
66         Mockito.verify(deviceDisconnectedHandler).onDeviceDisconnected(connectionContext);
67     }
68
69     @Test
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());
76
77         Mockito.verify(deviceDisconnectedHandler, Mockito.never()).onDeviceDisconnected(connectionContext);
78     }
79
80     @Test
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);
88     }
89
90     @Test
91     public void testChangeStateToHandshaking() throws Exception {
92         connectionContext.changeStateToHandshaking();
93         Assert.assertEquals(ConnectionContext.CONNECTION_STATE.HANDSHAKING, connectionContext.getConnectionState());
94     }
95
96     @Test
97     public void testChangeStateToTimeouting() throws Exception {
98         connectionContext.changeStateToTimeouting();
99         Assert.assertEquals(ConnectionContext.CONNECTION_STATE.TIMEOUTING, connectionContext.getConnectionState());
100     }
101
102     @Test
103     public void testChangeStateToWorking() throws Exception {
104         connectionContext.changeStateToWorking();
105         Assert.assertEquals(ConnectionContext.CONNECTION_STATE.WORKING, connectionContext.getConnectionState());
106     }
107 }