-/**
+/*
* 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 com.google.common.util.concurrent.ListenableFuture;
-import com.google.common.util.concurrent.SettableFuture;
+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.runners.MockitoJUnitRunner;
+import org.mockito.Mockito;
+import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
-import org.opendaylight.openflowplugin.impl.connection.testutil.MsgGeneratorTestUtils;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
-import java.util.Collection;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
+import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
+import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
+import org.opendaylight.openflowplugin.api.openflow.connection.DeviceConnectionStatusProvider;
+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 {
- private ConnectionContextImpl conContext;
-
@Mock
- private ConnectionAdapter conAdapter;
-
- @Before
- public void initialization() {
- // place for mocking method's general behavior for ConnectorAdapter
- conContext = new ConnectionContextImpl(conAdapter);
- }
-
- /**
- * 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");
- }
+ private ConnectionAdapter connetionAdapter;
+ @Mock
+ private HandshakeContext handshakeContext;
+ @Mock
+ private OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueRegistration;
+ @Mock
+ private DeviceDisconnectedHandler deviceDisconnectedHandler;
+ @Mock
+ private OutboundQueueProvider outboundQueueProvider;
+ @Mock
+ private DeviceConnectionStatusProvider deviceConnectionStatusProvider;
- /**
- * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl#getConnectionAdapter()}.
- */
- @Test
- @Ignore
- public void testGetConnectionAdapter() {
- fail("Not yet implemented");
- }
+ private ConnectionContextImpl connectionContext;
- /**
- * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl#getConnectionState()}.
- */
- @Test
- @Ignore
- public void testGetConnectionState() {
- fail("Not yet implemented");
+ @Before
+ public void setUp() {
+ Mockito.when(connetionAdapter.getRemoteAddress())
+ .thenReturn(InetSocketAddress.createUnresolved("ofp-ut.example.org", 4242));
+ Mockito.when(connetionAdapter.isAlive()).thenReturn(true);
+
+ connectionContext = new ConnectionContextImpl(connetionAdapter, deviceConnectionStatusProvider);
+ 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#getNodeId()}.
- */
@Test
- @Ignore
- public void testGetNodeId() {
- fail("Not yet implemented");
+ public void testCloseConnection1() {
+ 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#setConnectionState(org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext.CONNECTION_STATE)}.
- */
@Test
- @Ignore
- public void testSetConnectionState() {
- fail("Not yet implemented");
+ public void testCloseConnection2() {
+ 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#getFeatures()}.
- */
@Test
- @Ignore
- public void testGetFeatures() {
- fail("Not yet implemented");
+ public void testOnConnectionClosed() {
+ 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#setFeatures(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply)}.
- */
@Test
- @Ignore
- public void testSetFeatures() {
- fail("Not yet implemented");
+ public void testChangeStateToHandshaking() {
+ connectionContext.changeStateToHandshaking();
+ Assert.assertEquals(ConnectionContext.CONNECTION_STATE.HANDSHAKING, connectionContext.getConnectionState());
}
- /**
- * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl#registerMultipartMsg(long)}.
- */
@Test
- public void testRegisterMultipartMsg() {
- final long xid = 1l;
- final ListenableFuture<Collection<MultipartReply>> futureObj = conContext.registerMultipartMsg(xid);
- Assert.assertNotNull(futureObj);
- Assert.assertTrue(futureObj instanceof SettableFuture);
+ public void testChangeStateToTimeouting() {
+ connectionContext.changeStateToTimeouting();
+ Assert.assertEquals(ConnectionContext.CONNECTION_STATE.TIMEOUTING, connectionContext.getConnectionState());
}
- /**
- * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl#addMultipartMsg(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply)}.
- *
- * @throws TimeoutException
- * @throws ExecutionException
- * @throws InterruptedException
- */
@Test
- public void testAddMultipartMsg() throws InterruptedException, ExecutionException, TimeoutException {
- final long xid = 1l;
- final ListenableFuture<Collection<MultipartReply>> futureObj = conContext.registerMultipartMsg(xid);
- Assert.assertNotNull(futureObj);
- final MultipartReply muplipartReply = MsgGeneratorTestUtils.makeMultipartDescReply(xid, "test-value", false);
- conContext.addMultipartMsg(muplipartReply);
- final Collection<MultipartReply> response = futureObj.get(1L, TimeUnit.SECONDS);
- Assert.assertNotNull(response);
- Assert.assertFalse(response.isEmpty());
- Assert.assertTrue(response.size() == 1);
- Assert.assertTrue(response.contains(muplipartReply));
+ public void testChangeStateToWorking() {
+ connectionContext.changeStateToWorking();
+ Assert.assertEquals(ConnectionContext.CONNECTION_STATE.WORKING, connectionContext.getConnectionState());
}
-}
+}
\ No newline at end of file