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