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.BarrierInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
37 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
40 * Test for {@link HandshakeListenerImpl}.
42 @RunWith(MockitoJUnitRunner.class)
43 public class HandshakeListenerImplTest {
46 private DeviceConnectedHandler deviceConnectedHandler;
48 private GetFeaturesOutput features;
50 private ConnectionAdapter connectionAdapter;
52 private HandshakeContext handshakeContext;
54 private ArgumentCaptor<NodeId> nodeIdCaptor;
56 private ConnectionContext connectionContextSpy;
57 private HandshakeListenerImpl handshakeListener;
60 public void setUp() throws Exception {
61 Mockito.when(connectionAdapter.barrier(ArgumentMatchers.<BarrierInput>any()))
62 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
63 connectionContextSpy = Mockito.spy(new ConnectionContextImpl(connectionAdapter));
64 Mockito.when(connectionContextSpy.getConnectionAdapter()).thenReturn(connectionAdapter);
65 Mockito.when(features.getDatapathId()).thenReturn(BigInteger.TEN);
66 handshakeListener = new HandshakeListenerImpl(connectionContextSpy, deviceConnectedHandler);
67 handshakeListener.setHandshakeContext(handshakeContext);
71 public void tearDown() throws Exception {
72 Mockito.verify(handshakeContext).close();
76 public void testOnHandshakeSuccessfull() throws Exception {
77 handshakeListener.onHandshakeSuccessful(features, OFConstants.OFP_VERSION_1_3);
78 Mockito.verify(connectionContextSpy).changeStateToWorking();
79 Mockito.verify(connectionContextSpy).setFeatures(any(FeaturesReply.class));
80 Mockito.verify(connectionContextSpy).setNodeId(nodeIdCaptor.capture());
81 Mockito.verify(connectionContextSpy).handshakeSuccessful();
82 Mockito.verify(deviceConnectedHandler).deviceConnected(connectionContextSpy);
83 Mockito.verify(handshakeContext).close();
85 Assert.assertEquals("openflow:10", nodeIdCaptor.getValue().getValue());
89 public void testOnHandshakeFailure1() throws Exception {
90 connectionContextSpy.setNodeId(new NodeId("ut-device:10"));
91 handshakeListener.onHandshakeFailure();
92 Mockito.verify(handshakeContext).close();
93 Mockito.verify(connectionContextSpy).closeConnection(false);
97 public void testOnHandshakeFailure2() throws Exception {
98 Mockito.when(connectionAdapter.getRemoteAddress())
99 .thenReturn(InetSocketAddress.createUnresolved("ut-ofp.example.org", 4242));
100 connectionContextSpy.setNodeId(new NodeId("openflow:1"));
101 handshakeListener.onHandshakeFailure();
102 Mockito.verify(handshakeContext).close();
103 Mockito.verify(connectionContextSpy).closeConnection(false);