Do not use OpenflowProtocolListener in ConnectionAdapter
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / connection / listener / HandshakeListenerImplTest.java
index 90d908b6b460ccf609c6ed3ec34107f0b13c3b66..2cb43deaa399ccaabcdb54f2b09c13e487254f27 100644 (file)
@@ -8,31 +8,34 @@
 
 package org.opendaylight.openflowplugin.impl.connection.listener;
 
-import java.math.BigInteger;
-import java.net.InetSocketAddress;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
 import org.junit.After;
-import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.ArgumentCaptor;
 import org.mockito.Captor;
-import org.mockito.Matchers;
 import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.mockito.runners.MockitoJUnitRunner;
+import org.mockito.junit.MockitoJUnitRunner;
 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
 import org.opendaylight.openflowplugin.api.OFConstants;
 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.device.handlers.DeviceConnectedHandler;
 import org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+import org.opendaylight.yangtools.yang.common.Uint64;
+import org.opendaylight.yangtools.yang.common.Uint8;
 
 /**
  * Test for {@link HandshakeListenerImpl}.
@@ -40,8 +43,6 @@ import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 @RunWith(MockitoJUnitRunner.class)
 public class HandshakeListenerImplTest {
 
-    private final Short version = OFConstants.OFP_VERSION_1_3;
-
     @Mock
     private DeviceConnectedHandler deviceConnectedHandler;
     @Mock
@@ -50,6 +51,8 @@ public class HandshakeListenerImplTest {
     private ConnectionAdapter connectionAdapter;
     @Mock
     private HandshakeContext handshakeContext;
+    @Mock
+    private DeviceConnectionStatusProvider deviceConnectionStatusProvider;
     @Captor
     private ArgumentCaptor<NodeId> nodeIdCaptor;
 
@@ -57,46 +60,48 @@ public class HandshakeListenerImplTest {
     private HandshakeListenerImpl handshakeListener;
 
     @Before
-    public void setUp() throws Exception {
-        Mockito.when(connectionAdapter.barrier(Matchers.<BarrierInput>any()))
+    public void setUp() {
+        when(connectionAdapter.barrier(any()))
                 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
-        connectionContextSpy = Mockito.spy(new ConnectionContextImpl(connectionAdapter));
-        Mockito.when(connectionContextSpy.getConnectionAdapter()).thenReturn(connectionAdapter);
-        Mockito.when(features.getDatapathId()).thenReturn(BigInteger.TEN);
+        connectionContextSpy = spy(new ConnectionContextImpl(connectionAdapter, deviceConnectionStatusProvider));
+        when(connectionContextSpy.getConnectionAdapter()).thenReturn(connectionAdapter);
+        when(features.getDatapathId()).thenReturn(Uint64.TEN);
+        when(features.getVersion()).thenReturn(Uint8.ONE);
         handshakeListener = new HandshakeListenerImpl(connectionContextSpy, deviceConnectedHandler);
         handshakeListener.setHandshakeContext(handshakeContext);
     }
 
     @After
-    public void tearDown() throws Exception {
-        Mockito.verify(handshakeContext).close();
+    public void tearDown() {
+        verify(handshakeContext).close();
     }
 
     @Test
-    public void testOnHandshakeSuccessfull() throws Exception {
-        handshakeListener.onHandshakeSuccessful(features, version);
-        Mockito.verify(connectionContextSpy).changeStateToWorking();
-        Mockito.verify(connectionContextSpy).setFeatures(Matchers.any(FeaturesReply.class));
-        Mockito.verify(connectionContextSpy).setNodeId(nodeIdCaptor.capture());
-        Mockito.verify(deviceConnectedHandler).deviceConnected(connectionContextSpy);
-        Mockito.verify(handshakeContext).close();
+    public void testOnHandshakeSuccessfull() {
+        handshakeListener.onHandshakeSuccessful(features, OFConstants.OFP_VERSION_1_3);
+        verify(connectionContextSpy).changeStateToWorking();
+        verify(connectionContextSpy).setFeatures(any(FeaturesReply.class));
+        verify(connectionContextSpy).setNodeId(nodeIdCaptor.capture());
+        verify(connectionContextSpy).handshakeSuccessful();
+        verify(deviceConnectedHandler).deviceConnected(connectionContextSpy);
+        verify(handshakeContext).close();
 
-        Assert.assertEquals("openflow:10", nodeIdCaptor.getValue().getValue());
+        assertEquals("openflow:10", nodeIdCaptor.getValue().getValue());
     }
 
     @Test
-    public void testOnHandshakeFailure1() throws Exception {
+    public void testOnHandshakeFailure1() {
         connectionContextSpy.setNodeId(new NodeId("ut-device:10"));
         handshakeListener.onHandshakeFailure();
-        Mockito.verify(handshakeContext).close();
-        Mockito.verify(connectionContextSpy).closeConnection(false);
+        verify(handshakeContext).close();
+        verify(connectionContextSpy).closeConnection(false);
     }
 
     @Test
-    public void testOnHandshakeFailure2() throws Exception {
-        Mockito.when(connectionAdapter.getRemoteAddress()).thenReturn(InetSocketAddress.createUnresolved("ut-ofp.example.org", 4242));
+    public void testOnHandshakeFailure2() {
+        connectionContextSpy.setNodeId(new NodeId("openflow:1"));
         handshakeListener.onHandshakeFailure();
-        Mockito.verify(handshakeContext).close();
-        Mockito.verify(connectionContextSpy).closeConnection(false);
+        verify(handshakeContext).close();
+        verify(connectionContextSpy).closeConnection(false);
     }
-}
\ No newline at end of file
+}