OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / IdleHandlerTest.java
1 /*
2  * Copyright (c) 2014 Brocade Communications 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.openflowjava.protocol.impl.core;
10
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.times;
13 import static org.mockito.Mockito.verify;
14
15 import io.netty.channel.ChannelHandlerContext;
16 import java.util.concurrent.TimeUnit;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
23
24 /**
25  * Unit tests for IdleHandler.
26  *
27  * @author jameshall
28  */
29 public class IdleHandlerTest {
30
31     @Mock ChannelHandlerContext mockChHndlrCtx ;
32
33     IdleHandler idleHandler ;
34
35     /**
36      * Sets up test environment.
37      */
38     @Before
39     public void setUp() {
40         MockitoAnnotations.initMocks(this);
41         idleHandler = new IdleHandler(60L, TimeUnit.MINUTES);
42     }
43
44     /**
45      * Test message passing on channel read.
46      */
47     @Test
48     public void testChannelRead() throws Exception {
49         idleHandler.channelRead(mockChHndlrCtx, new Object());
50
51         // Verify that a read was fired for the next handler ...
52         verify(mockChHndlrCtx, times(1)).fireChannelRead(Mockito.<SwitchIdleEvent>any()) ;
53     }
54
55     /**
56      * Test channel read timeout.
57      */
58     @Test
59     public void testReadTimedOut() throws Exception {
60         idleHandler.readTimedOut(mockChHndlrCtx);
61
62         // Verify a read was fired for the next handler to process ...
63         verify(mockChHndlrCtx, times(1)).fireChannelRead(any(SwitchIdleEvent.class)) ;
64     }
65
66     /**
67      * Test only one timeout notification.
68      */
69     @Test
70     public void testReadTimedOutNoOpNotFirst() throws Exception {
71         idleHandler.readTimedOut(mockChHndlrCtx);
72         idleHandler.readTimedOut(mockChHndlrCtx);
73
74         // Verify that only one notification was sent to the next handler ...
75         verify(mockChHndlrCtx, times(1)).fireChannelRead(any(Object.class)) ;
76     }
77
78     /**
79      * Test two timeout notifications.
80      */
81     @Test
82     public void testReadTimedOutTwice() throws Exception {
83         idleHandler.readTimedOut(mockChHndlrCtx);
84         verify(mockChHndlrCtx, times(1)).fireChannelRead(any(Object.class));
85
86         idleHandler.channelRead(mockChHndlrCtx, new String());
87         verify(mockChHndlrCtx, times(2)).fireChannelRead(any(Object.class));
88
89         idleHandler.readTimedOut(mockChHndlrCtx);
90         verify(mockChHndlrCtx, times(3)).fireChannelRead(any(Object.class));
91     }
92 }