Post "Clustering optimization" updates
[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.BarrierInput;
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
37 /**
38  * Test for {@link HandshakeListenerImpl}.
39  */
40 @RunWith(MockitoJUnitRunner.class)
41 public class HandshakeListenerImplTest {
42
43     private final Short version = OFConstants.OFP_VERSION_1_3;
44
45     @Mock
46     private DeviceConnectedHandler deviceConnectedHandler;
47     @Mock
48     private GetFeaturesOutput features;
49     @Mock
50     private ConnectionAdapter connectionAdapter;
51     @Mock
52     private HandshakeContext handshakeContext;
53     @Captor
54     private ArgumentCaptor<NodeId> nodeIdCaptor;
55
56     private ConnectionContext connectionContextSpy;
57     private HandshakeListenerImpl handshakeListener;
58
59     @Before
60     public void setUp() throws Exception {
61         Mockito.when(connectionAdapter.barrier(Matchers.<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);
68     }
69
70     @After
71     public void tearDown() throws Exception {
72         Mockito.verify(handshakeContext).close();
73     }
74
75     @Test
76     public void testOnHandshakeSuccessfull() throws Exception {
77         handshakeListener.onHandshakeSuccessful(features, version);
78         Mockito.verify(connectionContextSpy).changeStateToWorking();
79         Mockito.verify(connectionContextSpy).setFeatures(Matchers.any(FeaturesReply.class));
80         Mockito.verify(connectionContextSpy).setNodeId(nodeIdCaptor.capture());
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() throws Exception {
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() throws Exception {
97         Mockito.when(connectionAdapter.getRemoteAddress()).thenReturn(InetSocketAddress.createUnresolved("ut-ofp.example.org", 4242));
98         handshakeListener.onHandshakeFailure();
99         Mockito.verify(handshakeContext).close();
100         Mockito.verify(connectionContextSpy).closeConnection(false);
101     }
102 }