Fix codestyle
[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     @Mock
44     private DeviceConnectedHandler deviceConnectedHandler;
45     @Mock
46     private GetFeaturesOutput features;
47     @Mock
48     private ConnectionAdapter connectionAdapter;
49     @Mock
50     private HandshakeContext handshakeContext;
51     @Captor
52     private ArgumentCaptor<NodeId> nodeIdCaptor;
53
54     private ConnectionContext connectionContextSpy;
55     private HandshakeListenerImpl handshakeListener;
56
57     @Before
58     public void setUp() throws Exception {
59         Mockito.when(connectionAdapter.barrier(Matchers.<BarrierInput>any()))
60                 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
61         connectionContextSpy = Mockito.spy(new ConnectionContextImpl(connectionAdapter));
62         Mockito.when(connectionContextSpy.getConnectionAdapter()).thenReturn(connectionAdapter);
63         Mockito.when(features.getDatapathId()).thenReturn(BigInteger.TEN);
64         handshakeListener = new HandshakeListenerImpl(connectionContextSpy, deviceConnectedHandler);
65         handshakeListener.setHandshakeContext(handshakeContext);
66     }
67
68     @After
69     public void tearDown() throws Exception {
70         Mockito.verify(handshakeContext).close();
71     }
72
73     @Test
74     public void testOnHandshakeSuccessfull() throws Exception {
75         handshakeListener.onHandshakeSuccessful(features, OFConstants.OFP_VERSION_1_3);
76         Mockito.verify(connectionContextSpy).changeStateToWorking();
77         Mockito.verify(connectionContextSpy).setFeatures(Matchers.any(FeaturesReply.class));
78         Mockito.verify(connectionContextSpy).setNodeId(nodeIdCaptor.capture());
79         Mockito.verify(connectionContextSpy).handshakeSuccessful();
80         Mockito.verify(deviceConnectedHandler).deviceConnected(connectionContextSpy);
81         Mockito.verify(handshakeContext).close();
82
83         Assert.assertEquals("openflow:10", nodeIdCaptor.getValue().getValue());
84     }
85
86     @Test
87     public void testOnHandshakeFailure1() throws Exception {
88         connectionContextSpy.setNodeId(new NodeId("ut-device:10"));
89         handshakeListener.onHandshakeFailure();
90         Mockito.verify(handshakeContext).close();
91         Mockito.verify(connectionContextSpy).closeConnection(false);
92     }
93
94     @Test
95     public void testOnHandshakeFailure2() throws Exception {
96         Mockito.when(connectionAdapter.getRemoteAddress()).thenReturn(InetSocketAddress.createUnresolved("ut-ofp.example.org", 4242));
97         connectionContextSpy.setNodeId(new NodeId("openflow:1"));
98         handshakeListener.onHandshakeFailure();
99         Mockito.verify(handshakeContext).close();
100         Mockito.verify(connectionContextSpy).closeConnection(false);
101     }
102 }