Merge "Add NodeConfiguratorImpl enqueue trace"
[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.EchoRequestMessageBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
26
27 /**
28  * Test for {@link OpenflowProtocolListenerInitialImpl}.
29  */
30 @RunWith(MockitoJUnitRunner.class)
31 public class OpenflowProtocolListenerInitialImplTest {
32
33     @Mock
34     private ConnectionContext connectionContext;
35     @Mock
36     private HandshakeContext handshakeContext;
37     @Mock
38     private ConnectionAdapter connectionAdapter;
39     @Mock
40     private HandshakeManager handshakeManager;
41
42     private OpenflowProtocolListenerInitialImpl openflowProtocolListenerInitial;
43
44     @Before
45     public void setUp() {
46         Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
47         Mockito.when(connectionContext.getConnectionAdapter()).thenReturn(connectionAdapter);
48         Mockito.when(connectionContext.getConnectionState())
49                 .thenReturn(null, ConnectionContext.CONNECTION_STATE.HANDSHAKING);
50         Mockito.when(handshakeContext.getHandshakeManager()).thenReturn(handshakeManager);
51
52         openflowProtocolListenerInitial = new OpenflowProtocolListenerInitialImpl(connectionContext, handshakeContext);
53     }
54
55     @Test
56     public void testOnEchoRequestMessage() {
57         EchoRequestMessageBuilder echoRequestMessageBld = new EchoRequestMessageBuilder()
58                 .setXid(42L)
59                 .setVersion(OFConstants.OFP_VERSION_1_3);
60         openflowProtocolListenerInitial.onEchoRequestMessage(echoRequestMessageBld.build());
61
62         Mockito.verify(connectionAdapter).echoReply(ArgumentMatchers.any());
63     }
64
65     @Test
66     public void testOnHelloMessage() {
67         HelloMessageBuilder helloMessageBld = new HelloMessageBuilder()
68                 .setXid(42L)
69                 .setVersion(OFConstants.OFP_VERSION_1_3);
70         openflowProtocolListenerInitial.onHelloMessage(helloMessageBld.build());
71
72         Mockito.verify(handshakeManager).shake(ArgumentMatchers.any());
73     }
74
75     @Test
76     public void testCheckState() {
77         Assert.assertFalse(openflowProtocolListenerInitial.checkState(ConnectionContext.CONNECTION_STATE.HANDSHAKING));
78         Assert.assertTrue(openflowProtocolListenerInitial.checkState(ConnectionContext.CONNECTION_STATE.HANDSHAKING));
79     }
80 }