OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / karaf / ShowSessionStatsCommandProviderTest.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.contains;
12
13 import com.google.common.base.Function;
14 import javax.annotation.Nullable;
15 import org.junit.After;
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.mockito.Mockito;
19 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.SessionStatistics;
20
21 /**
22  * Test for {@link ShowSessionStatsCommandProvider}.
23  */
24 public class ShowSessionStatsCommandProviderTest extends AbstractKarafTest {
25
26     private ShowSessionStatsCommandProvider showSessionStatsCommandProvider;
27     private static final Function<String, Boolean> CHECK_NO_ACTIVITY_FUNCTION = new Function<String, Boolean>() {
28         @Nullable
29         @Override
30         public Boolean apply(String input) {
31             return input.isEmpty();
32         }
33     };
34
35     @Override
36
37     public void doSetUp() {
38         showSessionStatsCommandProvider = new ShowSessionStatsCommandProvider();
39         SessionStatistics.resetAllCounters();
40     }
41
42     @After
43     public void tearDown() {
44         SessionStatistics.resetAllCounters();
45     }
46
47     /**
48      * Test for {@link ShowEventTimesComandProvider#doExecute()} when no stats were touched before.
49      */
50     @Test
51     public void testDoExecute_clean() throws Exception {
52         Assert.assertTrue(checkNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION));
53         showSessionStatsCommandProvider.execute(cmdSession);
54         Assert.assertTrue(checkNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION));
55         Mockito.verify(console).print("");
56     }
57
58     /**
59      * Test for {@link ShowEventTimesComandProvider#doExecute()} when stats were touched before.
60      */
61     @Test
62     public void testDoExecute_dirty() throws Exception {
63         final String dummySessionId = "junitSessionId";
64         Assert.assertTrue(checkNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION));
65
66         SessionStatistics.countEvent(dummySessionId, SessionStatistics.ConnectionStatus.CONNECTION_CREATED);
67         Assert.assertFalse(checkNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION));
68
69         showSessionStatsCommandProvider.execute(cmdSession);
70         Assert.assertFalse(checkNoActivity(SessionStatistics.provideStatistics(), CHECK_NO_ACTIVITY_FUNCTION));
71         Mockito.verify(console).print(contains(dummySessionId));
72     }
73 }