2982270b0f811b9a44fc41cc2eb5bec60950c053
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / connection / listener / HandshakeListenerImplTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.impl.connection.listener;
10
11 import static org.mockito.ArgumentMatchers.any;
12
13 import java.net.InetSocketAddress;
14 import org.junit.After;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.ArgumentCaptor;
20 import org.mockito.ArgumentMatchers;
21 import org.mockito.Captor;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
26 import org.opendaylight.openflowplugin.api.OFConstants;
27 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
28 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
29 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
30 import org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
35 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
36 import org.opendaylight.yangtools.yang.common.Uint64;
37
38 /**
39  * Test for {@link HandshakeListenerImpl}.
40  */
41 @RunWith(MockitoJUnitRunner.class)
42 public class HandshakeListenerImplTest {
43
44     @Mock
45     private DeviceConnectedHandler deviceConnectedHandler;
46     @Mock
47     private GetFeaturesOutput features;
48     @Mock
49     private ConnectionAdapter connectionAdapter;
50     @Mock
51     private HandshakeContext handshakeContext;
52     @Captor
53     private ArgumentCaptor<NodeId> nodeIdCaptor;
54
55     private ConnectionContext connectionContextSpy;
56     private HandshakeListenerImpl handshakeListener;
57
58     @Before
59     public void setUp() {
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(Uint64.valueOf(10));
65         handshakeListener = new HandshakeListenerImpl(connectionContextSpy, deviceConnectedHandler);
66         handshakeListener.setHandshakeContext(handshakeContext);
67     }
68
69     @After
70     public void tearDown() {
71         Mockito.verify(handshakeContext).close();
72     }
73
74     @Test
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();
83
84         Assert.assertEquals("openflow:10", nodeIdCaptor.getValue().getValue());
85     }
86
87     @Test
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);
93     }
94
95     @Test
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);
103     }
104 }