OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / karaf / ShowStatsCommandProviderTest.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.matches;
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.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
20 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
21 import org.opendaylight.openflowplugin.impl.OpenFlowPluginProviderImpl;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
23
24 /**
25  * Test for {@link ShowStatsCommandProvider}.
26  */
27 public class ShowStatsCommandProviderTest extends AbstractKarafTest {
28
29     private ShowStatsCommandProvider showStatsCommandProvider;
30     private MessageIntelligenceAgency messageIntelligenceAgency;
31
32     private static final Function<String, Boolean> CHECK_NO_ACTIVITY_FUNCTION = new Function<String, Boolean>() {
33         @Nullable
34         @Override
35         public Boolean apply(String input) {
36             return input.endsWith(": no activity detected");
37         }
38     };
39
40     @Override
41     public void doSetUp() {
42         showStatsCommandProvider = new ShowStatsCommandProvider();
43         messageIntelligenceAgency = OpenFlowPluginProviderImpl.getMessageIntelligenceAgency();
44         messageIntelligenceAgency.resetStatistics();
45     }
46
47     @After
48     public void tearDown() throws Exception {
49         // Pattern.DOTALL is set inline via "(?s)" at the beginning
50         Mockito.verify(console).print(matches("(?s).+"));
51         messageIntelligenceAgency.resetStatistics();
52     }
53
54     /**
55      * Test for {@link ShowEventTimesComandProvider#doExecute()} when no stats were touched before.
56      */
57     @Test
58     public void testDoExecute_clean() throws Exception {
59         Assert.assertTrue(checkNoActivity(messageIntelligenceAgency.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
60         showStatsCommandProvider.execute(cmdSession);
61         Assert.assertTrue(checkNoActivity(messageIntelligenceAgency.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
62     }
63
64     /**
65      * Test for {@link ShowEventTimesComandProvider#doExecute()} when stats were touched before.
66      */
67     @Test
68     public void testDoExecute_dirty() throws Exception {
69         Assert.assertTrue(checkNoActivity(messageIntelligenceAgency.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
70
71         messageIntelligenceAgency.spyMessage(OfHeader.class, MessageSpy.StatisticsGroup.FROM_SWITCH);
72         Assert.assertFalse(checkNoActivity(messageIntelligenceAgency.provideIntelligence(),
73                 CHECK_NO_ACTIVITY_FUNCTION));
74
75         showStatsCommandProvider.execute(cmdSession);
76         Assert.assertFalse(checkNoActivity(messageIntelligenceAgency.provideIntelligence(),
77                 CHECK_NO_ACTIVITY_FUNCTION));
78     }
79 }