Merge "OPNFLWPLUG-1062 Include additional LLDP fields in liblldp"
[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     @Mock
42     private OutboundQueueProvider outboundQueueProvider;
43
44     private ConnectionContextImpl connectionContext;
45
46     @Before
47     public void setUp() {
48         Mockito.when(connetionAdapter.getRemoteAddress())
49                 .thenReturn(InetSocketAddress.createUnresolved("ofp-ut.example.org", 4242));
50         Mockito.when(connetionAdapter.isAlive()).thenReturn(true);
51
52         connectionContext = new ConnectionContextImpl(connetionAdapter);
53         connectionContext.setHandshakeContext(handshakeContext);
54         connectionContext.setNodeId(new NodeId("ut-node:123"));
55         connectionContext.setOutboundQueueHandleRegistration(outboundQueueRegistration);
56         connectionContext.setDeviceDisconnectedHandler(deviceDisconnectedHandler);
57
58         Assert.assertNull(connectionContext.getConnectionState());
59     }
60
61     @Test
62     public void testCloseConnection1() {
63         connectionContext.closeConnection(true);
64         Mockito.verify(outboundQueueRegistration).close();
65         Mockito.verify(handshakeContext).close();
66         Mockito.verify(connetionAdapter).disconnect();
67         Assert.assertEquals(ConnectionContext.CONNECTION_STATE.RIP, connectionContext.getConnectionState());
68
69         Mockito.verify(deviceDisconnectedHandler).onDeviceDisconnected(connectionContext);
70     }
71
72     @Test
73     public void testCloseConnection2() {
74         connectionContext.closeConnection(false);
75         Mockito.verify(outboundQueueRegistration).close();
76         Mockito.verify(handshakeContext).close();
77         Mockito.verify(connetionAdapter).disconnect();
78         Assert.assertEquals(ConnectionContext.CONNECTION_STATE.RIP, connectionContext.getConnectionState());
79
80         Mockito.verify(deviceDisconnectedHandler, Mockito.never()).onDeviceDisconnected(connectionContext);
81     }
82
83     @Test
84     public void testOnConnectionClosed() {
85         connectionContext.onConnectionClosed();
86         Assert.assertEquals(ConnectionContext.CONNECTION_STATE.RIP, connectionContext.getConnectionState());
87         Mockito.verify(outboundQueueRegistration).close();
88         Mockito.verify(handshakeContext).close();
89         Assert.assertEquals(ConnectionContext.CONNECTION_STATE.RIP, connectionContext.getConnectionState());
90         Mockito.verify(deviceDisconnectedHandler).onDeviceDisconnected(connectionContext);
91     }
92
93     @Test
94     public void testChangeStateToHandshaking() {
95         connectionContext.changeStateToHandshaking();
96         Assert.assertEquals(ConnectionContext.CONNECTION_STATE.HANDSHAKING, connectionContext.getConnectionState());
97     }
98
99     @Test
100     public void testChangeStateToTimeouting() {
101         connectionContext.changeStateToTimeouting();
102         Assert.assertEquals(ConnectionContext.CONNECTION_STATE.TIMEOUTING, connectionContext.getConnectionState());
103     }
104
105     @Test
106     public void testChangeStateToWorking() {
107         connectionContext.changeStateToWorking();
108         Assert.assertEquals(ConnectionContext.CONNECTION_STATE.WORKING, connectionContext.getConnectionState());
109     }
110 }