2fb353223e2019caf0c5bfc1a2bd67c7f8f00b66
[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 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;
33
34 /**
35  * Test for {@link HandshakeListenerImpl}.
36  */
37 @RunWith(MockitoJUnitRunner.class)
38 public class HandshakeListenerImplTest {
39
40     private final Short version = OFConstants.OFP_VERSION_1_3;
41
42     @Mock
43     private DeviceConnectedHandler deviceConnectedHandler;
44     @Mock
45     private GetFeaturesOutput features;
46     @Mock
47     private ConnectionAdapter connectionAdapter;
48     @Mock
49     private HandshakeContext handshakeContext;
50     @Captor
51     private ArgumentCaptor<NodeId> nodeIdCaptor;
52
53     private ConnectionContext connectionContextSpy;
54     private HandshakeListenerImpl handshakeListener;
55
56     @Before
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);
63     }
64
65     @After
66     public void tearDown() throws Exception {
67         Mockito.verify(handshakeContext).close();
68     }
69
70     @Test
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();
78
79         Assert.assertEquals("openflow:10", nodeIdCaptor.getValue().getValue());
80     }
81
82     @Test
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);
88     }
89
90     @Test
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);
96     }
97 }