Fix checkstyle warnings for impl/connection package and OpenFlowPluginProviderImpl
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / connection / ConnectionContextImplTest.java
index b2257cf6dcd861f8569d240f5a35b881247781c8..bca7a967fba435e0c4f6159102529cfcbfd3b400 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
 
 package org.opendaylight.openflowplugin.impl.connection;
 
-import static org.junit.Assert.fail;
+import java.net.InetSocketAddress;
+import org.junit.Assert;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
+import org.mockito.Mockito;
 import org.mockito.runners.MockitoJUnitRunner;
 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
+import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
+import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
+import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
+import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
+import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceDisconnectedHandler;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 
 /**
- * openflowplugin-impl
- * org.opendaylight.openflowplugin.impl.connection
- * <p/>
- * test of {@link ConnectionContextImpl} - lightweight version, using basic ways (TDD)
- *
- * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
- *         <p/>
- *         Created: Mar 26, 2015
+ * Test for {@link ConnectionContextImpl}.
  */
 @RunWith(MockitoJUnitRunner.class)
 public class ConnectionContextImplTest {
 
-
     @Mock
-    private ConnectionAdapter conAdapter;
+    private ConnectionAdapter connetionAdapter;
+    @Mock
+    private HandshakeContext handshakeContext;
+    @Mock
+    private OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueRegistration;
+    @Mock
+    private DeviceDisconnectedHandler deviceDisconnectedHandler;
+    @Mock
+    private OutboundQueueProvider outboundQueueProvider;
+
+    private ConnectionContextImpl connectionContext;
 
     @Before
-    public void initialization() {
-        // place for mocking method's general behavior for ConnectorAdapter
-    }
+    public void setUp() throws Exception {
+        Mockito.when(connetionAdapter.getRemoteAddress())
+                .thenReturn(InetSocketAddress.createUnresolved("ofp-ut.example.org", 4242));
+        Mockito.when(connetionAdapter.isAlive()).thenReturn(true);
 
-    /**
-     * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl#ConnectionContextImpl(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
-     */
-    @Test
-    @Ignore
-    public void testConnectionContextImpl() {
-        fail("Not yet implemented");
+        connectionContext = new ConnectionContextImpl(connetionAdapter);
+        connectionContext.setHandshakeContext(handshakeContext);
+        connectionContext.setNodeId(new NodeId("ut-node:123"));
+        connectionContext.setOutboundQueueHandleRegistration(outboundQueueRegistration);
+        connectionContext.setDeviceDisconnectedHandler(deviceDisconnectedHandler);
+
+        Assert.assertNull(connectionContext.getConnectionState());
     }
 
-    /**
-     * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl#getConnectionAdapter()}.
-     */
     @Test
-    @Ignore
-    public void testGetConnectionAdapter() {
-        fail("Not yet implemented");
+    public void testCloseConnection1() throws Exception {
+        connectionContext.closeConnection(true);
+        Mockito.verify(outboundQueueRegistration).close();
+        Mockito.verify(handshakeContext).close();
+        Mockito.verify(connetionAdapter).disconnect();
+        Assert.assertEquals(ConnectionContext.CONNECTION_STATE.RIP, connectionContext.getConnectionState());
+
+        Mockito.verify(deviceDisconnectedHandler).onDeviceDisconnected(connectionContext);
     }
 
-    /**
-     * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl#getConnectionState()}.
-     */
     @Test
-    @Ignore
-    public void testGetConnectionState() {
-        fail("Not yet implemented");
+    public void testCloseConnection2() throws Exception {
+        connectionContext.closeConnection(false);
+        Mockito.verify(outboundQueueRegistration).close();
+        Mockito.verify(handshakeContext).close();
+        Mockito.verify(connetionAdapter).disconnect();
+        Assert.assertEquals(ConnectionContext.CONNECTION_STATE.RIP, connectionContext.getConnectionState());
+
+        Mockito.verify(deviceDisconnectedHandler, Mockito.never()).onDeviceDisconnected(connectionContext);
     }
 
-    /**
-     * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl#getNodeId()}.
-     */
     @Test
-    @Ignore
-    public void testGetNodeId() {
-        fail("Not yet implemented");
+    public void testOnConnectionClosed() throws Exception {
+        connectionContext.onConnectionClosed();
+        Assert.assertEquals(ConnectionContext.CONNECTION_STATE.RIP, connectionContext.getConnectionState());
+        Mockito.verify(outboundQueueRegistration).close();
+        Mockito.verify(handshakeContext).close();
+        Assert.assertEquals(ConnectionContext.CONNECTION_STATE.RIP, connectionContext.getConnectionState());
+        Mockito.verify(deviceDisconnectedHandler).onDeviceDisconnected(connectionContext);
     }
 
-    /**
-     * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl#setConnectionState(org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext.CONNECTION_STATE)}.
-     */
     @Test
-    @Ignore
-    public void testSetConnectionState() {
-        fail("Not yet implemented");
+    public void testChangeStateToHandshaking() throws Exception {
+        connectionContext.changeStateToHandshaking();
+        Assert.assertEquals(ConnectionContext.CONNECTION_STATE.HANDSHAKING, connectionContext.getConnectionState());
     }
 
-    /**
-     * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl#getFeatures()}.
-     */
     @Test
-    @Ignore
-    public void testGetFeatures() {
-        fail("Not yet implemented");
+    public void testChangeStateToTimeouting() throws Exception {
+        connectionContext.changeStateToTimeouting();
+        Assert.assertEquals(ConnectionContext.CONNECTION_STATE.TIMEOUTING, connectionContext.getConnectionState());
     }
 
-    /**
-     * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl#setFeatures(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply)}.
-     */
     @Test
-    @Ignore
-    public void testSetFeatures() {
-        fail("Not yet implemented");
+    public void testChangeStateToWorking() throws Exception {
+        connectionContext.changeStateToWorking();
+        Assert.assertEquals(ConnectionContext.CONNECTION_STATE.WORKING, connectionContext.getConnectionState());
     }
-}
+}
\ No newline at end of file