Update MRI upstreams for Phosphorus
[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.junit.Assert.assertEquals;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.spy;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
16
17 import java.net.InetSocketAddress;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.ArgumentCaptor;
23 import org.mockito.Captor;
24 import org.mockito.Mock;
25 import org.mockito.junit.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.DeviceConnectionStatusProvider;
30 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
31 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
32 import org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
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;
38 import org.opendaylight.yangtools.yang.common.Uint64;
39 import org.opendaylight.yangtools.yang.common.Uint8;
40
41 /**
42  * Test for {@link HandshakeListenerImpl}.
43  */
44 @RunWith(MockitoJUnitRunner.class)
45 public class HandshakeListenerImplTest {
46
47     @Mock
48     private DeviceConnectedHandler deviceConnectedHandler;
49     @Mock
50     private GetFeaturesOutput features;
51     @Mock
52     private ConnectionAdapter connectionAdapter;
53     @Mock
54     private HandshakeContext handshakeContext;
55     @Mock
56     private DeviceConnectionStatusProvider deviceConnectionStatusProvider;
57     @Captor
58     private ArgumentCaptor<NodeId> nodeIdCaptor;
59
60     private ConnectionContext connectionContextSpy;
61     private HandshakeListenerImpl handshakeListener;
62
63     @Before
64     public void setUp() {
65         when(connectionAdapter.barrier(any()))
66                 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
67         connectionContextSpy = spy(new ConnectionContextImpl(connectionAdapter, deviceConnectionStatusProvider));
68         when(connectionContextSpy.getConnectionAdapter()).thenReturn(connectionAdapter);
69         when(features.getDatapathId()).thenReturn(Uint64.TEN);
70         when(features.getVersion()).thenReturn(Uint8.ONE);
71         handshakeListener = new HandshakeListenerImpl(connectionContextSpy, deviceConnectedHandler);
72         handshakeListener.setHandshakeContext(handshakeContext);
73     }
74
75     @After
76     public void tearDown() {
77         verify(handshakeContext).close();
78     }
79
80     @Test
81     public void testOnHandshakeSuccessfull() {
82         handshakeListener.onHandshakeSuccessful(features, OFConstants.OFP_VERSION_1_3);
83         verify(connectionContextSpy).changeStateToWorking();
84         verify(connectionContextSpy).setFeatures(any(FeaturesReply.class));
85         verify(connectionContextSpy).setNodeId(nodeIdCaptor.capture());
86         verify(connectionContextSpy).handshakeSuccessful();
87         verify(deviceConnectedHandler).deviceConnected(connectionContextSpy);
88         verify(handshakeContext).close();
89
90         assertEquals("openflow:10", nodeIdCaptor.getValue().getValue());
91     }
92
93     @Test
94     public void testOnHandshakeFailure1() {
95         connectionContextSpy.setNodeId(new NodeId("ut-device:10"));
96         handshakeListener.onHandshakeFailure();
97         verify(handshakeContext).close();
98         verify(connectionContextSpy).closeConnection(false);
99     }
100
101     @Test
102     public void testOnHandshakeFailure2() {
103         when(connectionAdapter.getRemoteAddress())
104                 .thenReturn(InetSocketAddress.createUnresolved("ut-ofp.example.org", 4242));
105         connectionContextSpy.setNodeId(new NodeId("openflow:1"));
106         handshakeListener.onHandshakeFailure();
107         verify(handshakeContext).close();
108         verify(connectionContextSpy).closeConnection(false);
109     }
110 }