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 static org.mockito.ArgumentMatchers.any;
13 import java.math.BigInteger;
14 import java.net.InetSocketAddress;
15 import org.junit.After;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.ArgumentCaptor;
21 import org.mockito.ArgumentMatchers;
22 import org.mockito.Captor;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.runners.MockitoJUnitRunner;
26 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
27 import org.opendaylight.openflowplugin.api.OFConstants;
28 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
29 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
30 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
31 import org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
36 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
39 * Test for {@link HandshakeListenerImpl}.
41 @RunWith(MockitoJUnitRunner.class)
42 public class HandshakeListenerImplTest {
45 private DeviceConnectedHandler deviceConnectedHandler;
47 private GetFeaturesOutput features;
49 private ConnectionAdapter connectionAdapter;
51 private HandshakeContext handshakeContext;
53 private ArgumentCaptor<NodeId> nodeIdCaptor;
55 private ConnectionContext connectionContextSpy;
56 private HandshakeListenerImpl handshakeListener;
60 Mockito.when(connectionAdapter.barrier(ArgumentMatchers.any()))
61 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
62 connectionContextSpy = Mockito.spy(new ConnectionContextImpl(connectionAdapter));
63 Mockito.when(connectionContextSpy.getConnectionAdapter()).thenReturn(connectionAdapter);
64 Mockito.when(features.getDatapathId()).thenReturn(BigInteger.TEN);
65 handshakeListener = new HandshakeListenerImpl(connectionContextSpy, deviceConnectedHandler);
66 handshakeListener.setHandshakeContext(handshakeContext);
70 public void tearDown() {
71 Mockito.verify(handshakeContext).close();
75 public void testOnHandshakeSuccessfull() {
76 handshakeListener.onHandshakeSuccessful(features, OFConstants.OFP_VERSION_1_3);
77 Mockito.verify(connectionContextSpy).changeStateToWorking();
78 Mockito.verify(connectionContextSpy).setFeatures(any(FeaturesReply.class));
79 Mockito.verify(connectionContextSpy).setNodeId(nodeIdCaptor.capture());
80 Mockito.verify(connectionContextSpy).handshakeSuccessful();
81 Mockito.verify(deviceConnectedHandler).deviceConnected(connectionContextSpy);
82 Mockito.verify(handshakeContext).close();
84 Assert.assertEquals("openflow:10", nodeIdCaptor.getValue().getValue());
88 public void testOnHandshakeFailure1() {
89 connectionContextSpy.setNodeId(new NodeId("ut-device:10"));
90 handshakeListener.onHandshakeFailure();
91 Mockito.verify(handshakeContext).close();
92 Mockito.verify(connectionContextSpy).closeConnection(false);
96 public void testOnHandshakeFailure2() {
97 Mockito.when(connectionAdapter.getRemoteAddress())
98 .thenReturn(InetSocketAddress.createUnresolved("ut-ofp.example.org", 4242));
99 connectionContextSpy.setNodeId(new NodeId("openflow:1"));
100 handshakeListener.onHandshakeFailure();
101 Mockito.verify(handshakeContext).close();
102 Mockito.verify(connectionContextSpy).closeConnection(false);