Remove trailing whitespace
[openflowjava.git] / 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.Matchers.any;
12 import static org.mockito.Mockito.times;
13 import static org.mockito.Mockito.verify;
14 import io.netty.channel.ChannelHandlerContext;
15
16 import java.util.concurrent.TimeUnit;
17
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.mockito.MockitoAnnotations;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
24
25 /**
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      */
39     @Before
40     public void setUp() {
41         MockitoAnnotations.initMocks(this);
42         idleHandler = new IdleHandler(60L, TimeUnit.MINUTES ) ;
43     }
44
45     /**
46      * Test message passing on channel read
47      */
48     @Test
49     public void testChannelRead() {
50         try {
51             idleHandler.channelRead(mockChHndlrCtx, new Object() );
52         } catch (Exception e) {
53             Assert.fail();
54         }
55
56         // Verify that a read was fired for the next handler ...
57         verify(mockChHndlrCtx, times(1)).fireChannelRead(any(SwitchIdleEvent.class)) ;
58     }
59
60     /**
61      * Test channel read timeout
62      */
63     @Test
64     public void testReadTimedOut() {
65         try {
66             idleHandler.readTimedOut( mockChHndlrCtx );
67         } catch (Exception e) {
68             Assert.fail();
69         }
70
71         // Verify a read was fired for the next handler to process ...
72         verify(mockChHndlrCtx, times(1)).fireChannelRead(any(SwitchIdleEvent.class)) ;
73     }
74
75     /**
76      * Test only one timeout notification
77      */
78     @Test
79     public void testReadTimedOutNoOpNotFirst() {
80         try {
81             idleHandler.readTimedOut(mockChHndlrCtx);
82             idleHandler.readTimedOut(mockChHndlrCtx);
83         } catch (Exception e) {
84             Assert.fail();
85         }
86
87         // Verify that only one notification was sent to the next handler ...
88         verify(mockChHndlrCtx, times(1)).fireChannelRead(any(Object.class)) ;
89     }
90
91     /**
92      * Test two timeout notifications
93      */
94     @Test
95     public void testReadTimedOutTwice() {
96         try {
97             idleHandler.readTimedOut(mockChHndlrCtx);
98             verify(mockChHndlrCtx, times(1)).fireChannelRead(any(Object.class)) ;
99
100             idleHandler.channelRead(mockChHndlrCtx, new String() );
101             verify(mockChHndlrCtx, times(2)).fireChannelRead(any(Object.class)) ;
102
103             idleHandler.readTimedOut(mockChHndlrCtx);
104             verify(mockChHndlrCtx, times(3)).fireChannelRead(any(Object.class)) ;
105         } catch (Exception e) {
106             Assert.fail();
107         }
108     }
109
110 }