Ditch use of SystemNotificationsListener
[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 package org.opendaylight.openflowplugin.impl.connection.listener;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.verify;
14 import static org.mockito.Mockito.when;
15
16 import com.google.common.util.concurrent.Futures;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.Mock;
21 import org.mockito.junit.MockitoJUnitRunner;
22 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
23 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
24 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
25 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
26 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeManager;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessageBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
29 import org.opendaylight.yangtools.yang.common.Uint32;
30
31 /**
32  * Test for {@link OpenflowProtocolListenerInitialImpl}.
33  */
34 @RunWith(MockitoJUnitRunner.class)
35 public class OpenflowProtocolListenerInitialImplTest {
36     @Mock
37     private ConnectionContext connectionContext;
38     @Mock
39     private HandshakeContext handshakeContext;
40     @Mock
41     private ConnectionAdapter connectionAdapter;
42     @Mock
43     private HandshakeManager handshakeManager;
44
45     private OpenflowProtocolListenerInitialImpl openflowProtocolListenerInitial;
46
47     @Before
48     public void setUp() {
49         when(connectionAdapter.isAlive()).thenReturn(true);
50         when(connectionContext.getConnectionAdapter()).thenReturn(connectionAdapter);
51         when(connectionContext.getConnectionState())
52                 .thenReturn(null, ConnectionContext.CONNECTION_STATE.HANDSHAKING);
53         when(handshakeContext.getHandshakeManager()).thenReturn(handshakeManager);
54
55         openflowProtocolListenerInitial = new OpenflowProtocolListenerInitialImpl(connectionContext, handshakeContext);
56     }
57
58     @Test
59     public void testOnEchoRequestMessage() {
60         when(connectionAdapter.echoReply(any())).thenReturn(Futures.immediateFuture(null));
61
62         openflowProtocolListenerInitial.onEchoRequestMessage(
63             new EchoRequestMessageBuilder()
64                 .setXid(Uint32.valueOf(42))
65                 .setVersion(EncodeConstants.OF_VERSION_1_3)
66                 .build());
67
68         verify(connectionAdapter).echoReply(any());
69     }
70
71     @Test
72     public void testOnHelloMessage() {
73         HelloMessageBuilder helloMessageBld = new HelloMessageBuilder()
74                 .setXid(Uint32.valueOf(42))
75                 .setVersion(EncodeConstants.OF_VERSION_1_3);
76         openflowProtocolListenerInitial.onHelloMessage(helloMessageBld.build());
77
78         verify(handshakeManager).shake(any());
79     }
80
81     @Test
82     public void testCheckState() {
83         assertFalse(openflowProtocolListenerInitial.checkState(ConnectionContext.CONNECTION_STATE.HANDSHAKING));
84         assertTrue(openflowProtocolListenerInitial.checkState(ConnectionContext.CONNECTION_STATE.HANDSHAKING));
85     }
86 }