2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.connection.listener;
11 import java.math.BigInteger;
12 import java.net.InetSocketAddress;
13 import org.junit.After;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.ArgumentCaptor;
19 import org.mockito.Captor;
20 import org.mockito.Matchers;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.runners.MockitoJUnitRunner;
24 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
25 import org.opendaylight.openflowplugin.api.OFConstants;
26 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
27 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
28 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
29 import org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
35 * Test for {@link HandshakeListenerImpl}.
37 @RunWith(MockitoJUnitRunner.class)
38 public class HandshakeListenerImplTest {
40 private final Short version = OFConstants.OFP_VERSION_1_3;
43 private DeviceConnectedHandler deviceConnectedHandler;
45 private GetFeaturesOutput features;
47 private ConnectionAdapter connectionAdapter;
49 private HandshakeContext handshakeContext;
51 private ArgumentCaptor<NodeId> nodeIdCaptor;
53 private ConnectionContext connectionContextSpy;
54 private HandshakeListenerImpl handshakeListener;
57 public void setUp() throws Exception {
58 connectionContextSpy = Mockito.spy(new ConnectionContextImpl(connectionAdapter));
59 Mockito.when(connectionContextSpy.getConnectionAdapter()).thenReturn(connectionAdapter);
60 Mockito.when(features.getDatapathId()).thenReturn(BigInteger.TEN);
61 handshakeListener = new HandshakeListenerImpl(connectionContextSpy, deviceConnectedHandler);
62 handshakeListener.setHandshakeContext(handshakeContext);
66 public void tearDown() throws Exception {
67 Mockito.verify(handshakeContext).close();
71 public void testOnHandshakeSuccessfull() throws Exception {
72 handshakeListener.onHandshakeSuccessfull(features, version);
73 Mockito.verify(connectionContextSpy).changeStateToWorking();
74 Mockito.verify(connectionContextSpy).setFeatures(Matchers.any(FeaturesReply.class));
75 Mockito.verify(connectionContextSpy).setNodeId(nodeIdCaptor.capture());
76 Mockito.verify(deviceConnectedHandler).deviceConnected(connectionContextSpy);
77 Mockito.verify(handshakeContext).close();
79 Assert.assertEquals("openflow:10", nodeIdCaptor.getValue().getValue());
83 public void testOnHandshakeFailure1() throws Exception {
84 connectionContextSpy.setNodeId(new NodeId("ut-device:10"));
85 handshakeListener.onHandshakeFailure();
86 Mockito.verify(handshakeContext).close();
87 Mockito.verify(connectionContextSpy).closeConnection(false);
91 public void testOnHandshakeFailure2() throws Exception {
92 Mockito.when(connectionAdapter.getRemoteAddress()).thenReturn(InetSocketAddress.createUnresolved("ut-ofp.example.org", 4242));
93 handshakeListener.onHandshakeFailure();
94 Mockito.verify(handshakeContext).close();
95 Mockito.verify(connectionContextSpy).closeConnection(false);