Merge "Cleanup use of Guava Function"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / karaf / ResetSessionStatsComandProviderTest.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.karaf;
10
11 import static org.mockito.ArgumentMatchers.anyString;
12
13 import java.util.function.Function;
14 import org.junit.After;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.mockito.Mockito;
18 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.SessionStatistics;
19
20 /**
21  * Test for {@link ResetSessionStatsComandProvider}.
22  */
23 public class ResetSessionStatsComandProviderTest extends AbstractKarafTest {
24
25     private ResetSessionStatsComandProvider resetSessionStatsComandProvider;
26     private static final Function<String, Boolean> CHECK_NO_ACTIVITY_FUNCTION = String::isEmpty;
27
28     @Override
29     public void doSetUp() {
30         resetSessionStatsComandProvider = new ResetSessionStatsComandProvider();
31         SessionStatistics.resetAllCounters();
32     }
33
34     @After
35     public void tearDown() {
36         Mockito.verify(console).print(anyString());
37         SessionStatistics.resetAllCounters();
38     }
39
40     /**
41      * Test for {@link ResetSessionStatsComandProvider#doExecute()} when no stats were touched before.
42      */
43     @Test
44     public void testDoExecute_clean() throws Exception {
45         Assert.assertTrue(checkNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION));
46         resetSessionStatsComandProvider.execute(cmdSession);
47         Assert.assertTrue(checkNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION));
48     }
49
50     /**
51      * Test for {@link ResetSessionStatsComandProvider#doExecute()} when stats were touched before.
52      */
53     @Test
54     public void testDoExecute_dirty() throws Exception {
55         final String dummySessionId = "junitSessionId";
56         Assert.assertTrue(checkNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION));
57
58         SessionStatistics.countEvent(dummySessionId, SessionStatistics.ConnectionStatus.CONNECTION_CREATED);
59         Assert.assertFalse(checkNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION));
60
61         resetSessionStatsComandProvider.execute(cmdSession);
62         Assert.assertTrue(checkNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION));
63     }
64 }