OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / connection / listener / OpenflowProtocolListenerInitialImplTest.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 org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.mockito.ArgumentMatchers;
16 import org.mockito.Mock;
17 import org.mockito.Mockito;
18 import org.mockito.runners.MockitoJUnitRunner;
19 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
20 import org.opendaylight.openflowplugin.api.OFConstants;
21 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
22 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
23 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeManager;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessageBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
28
29 /**
30  * Test for {@link OpenflowProtocolListenerInitialImpl}.
31  */
32 @RunWith(MockitoJUnitRunner.class)
33 public class OpenflowProtocolListenerInitialImplTest {
34
35     @Mock
36     private ConnectionContext connectionContext;
37     @Mock
38     private HandshakeContext handshakeContext;
39     @Mock
40     private ConnectionAdapter connectionAdapter;
41     @Mock
42     private HandshakeManager handshakeManager;
43
44     private OpenflowProtocolListenerInitialImpl openflowProtocolListenerInitial;
45
46     @Before
47     public void setUp() throws Exception {
48         Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
49         Mockito.when(connectionContext.getConnectionAdapter()).thenReturn(connectionAdapter);
50         Mockito.when(connectionContext.getConnectionState())
51                 .thenReturn(null, ConnectionContext.CONNECTION_STATE.HANDSHAKING);
52         Mockito.when(handshakeContext.getHandshakeManager()).thenReturn(handshakeManager);
53
54         openflowProtocolListenerInitial = new OpenflowProtocolListenerInitialImpl(connectionContext, handshakeContext);
55     }
56
57     @Test
58     public void testOnEchoRequestMessage() throws Exception {
59         EchoRequestMessageBuilder echoRequestMessageBld = new EchoRequestMessageBuilder()
60                 .setXid(42L)
61                 .setVersion(OFConstants.OFP_VERSION_1_3);
62         openflowProtocolListenerInitial.onEchoRequestMessage(echoRequestMessageBld.build());
63
64         Mockito.verify(connectionAdapter).echoReply(ArgumentMatchers.<EchoReplyInput>any());
65     }
66
67     @Test
68     public void testOnHelloMessage() throws Exception {
69         HelloMessageBuilder helloMessageBld = new HelloMessageBuilder()
70                 .setXid(42L)
71                 .setVersion(OFConstants.OFP_VERSION_1_3);
72         openflowProtocolListenerInitial.onHelloMessage(helloMessageBld.build());
73
74         Mockito.verify(handshakeManager).shake(ArgumentMatchers.<HelloMessage>any());
75     }
76
77     @Test
78     public void testCheckState() throws Exception {
79         Assert.assertFalse(openflowProtocolListenerInitial.checkState(ConnectionContext.CONNECTION_STATE.HANDSHAKING));
80         Assert.assertTrue(openflowProtocolListenerInitial.checkState(ConnectionContext.CONNECTION_STATE.HANDSHAKING));
81     }
82 }