Eliminate use of deprecated mockito methods
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / TransactionRateLimitingCallbackTest.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.controller.cluster.datastore;
9
10 import static org.junit.Assert.fail;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.anyLong;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.never;
15 import static org.mockito.Mockito.verify;
16
17 import com.codahale.metrics.Timer;
18 import com.google.common.base.Ticker;
19 import java.util.concurrent.TimeUnit;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mock;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
25
26 /**
27  * Unit tests for TransactionRateLimitingCallback.
28  *
29  * @author Thomas Pantelis
30  */
31 public class TransactionRateLimitingCallbackTest {
32
33     @Mock
34     ActorContext mockContext;
35
36     @Mock
37     Timer mockTimer;
38
39     @Mock
40     Ticker mockTicker;
41
42     TransactionRateLimitingCallback callback;
43
44     @Before
45     public void setUp() {
46         MockitoAnnotations.initMocks(this);
47         doReturn(mockTimer).when(mockContext).getOperationTimer(ActorContext.COMMIT);
48         callback = new TransactionRateLimitingCallback(mockContext);
49         TransactionRateLimitingCallback.setTicker(mockTicker);
50     }
51
52     @Test
53     public void testSuccessWithoutPause() {
54         doReturn(1L).doReturn(201L).when(mockTicker).read();
55
56         callback.run();
57         callback.success();
58
59         verify(mockTimer).update(200L, TimeUnit.NANOSECONDS);
60     }
61
62     @Test
63     public void testSuccessWithPause() {
64         doReturn(1L).doReturn(201L).doReturn(301L).doReturn(351L).when(mockTicker).read();
65
66         callback.run();
67         callback.pause();
68         callback.pause();
69         callback.resume();
70         callback.resume();
71         callback.success();
72
73         verify(mockTimer).update(250L, TimeUnit.NANOSECONDS);
74     }
75
76     @Test
77     public void testFailure() {
78         doReturn(1L).when(mockTicker).read();
79
80         callback.run();
81         callback.failure();
82
83         verify(mockTimer, never()).update(anyLong(), any(TimeUnit.class));
84     }
85
86     @Test
87     public void testSuccessWithoutRun() {
88         try {
89             callback.success();
90             fail("Expected IllegalStateException");
91         } catch (IllegalStateException e) {
92             // expected
93         }
94
95         verify(mockTimer, never()).update(anyLong(), any(TimeUnit.class));
96     }
97 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.