Merge "Cleanup use of Guava Function"
[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 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.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 =
32         input -> input.endsWith(": no activity detected");
33
34     @Override
35     public void doSetUp() {
36         showStatsCommandProvider = new ShowStatsCommandProvider();
37         messageIntelligenceAgency = OpenFlowPluginProviderImpl.getMessageIntelligenceAgency();
38         messageIntelligenceAgency.resetStatistics();
39     }
40
41     @After
42     public void tearDown() {
43         // Pattern.DOTALL is set inline via "(?s)" at the beginning
44         Mockito.verify(console).print(matches("(?s).+"));
45         messageIntelligenceAgency.resetStatistics();
46     }
47
48     /**
49      * Test for {@link ShowEventTimesComandProvider#doExecute()} when no stats were touched before.
50      */
51     @Test
52     public void testDoExecute_clean() throws Exception {
53         Assert.assertTrue(checkNoActivity(messageIntelligenceAgency.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
54         showStatsCommandProvider.execute(cmdSession);
55         Assert.assertTrue(checkNoActivity(messageIntelligenceAgency.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
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         Assert.assertTrue(checkNoActivity(messageIntelligenceAgency.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
64
65         messageIntelligenceAgency.spyMessage(OfHeader.class, MessageSpy.StatisticsGroup.FROM_SWITCH);
66         Assert.assertFalse(checkNoActivity(messageIntelligenceAgency.provideIntelligence(),
67                 CHECK_NO_ACTIVITY_FUNCTION));
68
69         showStatsCommandProvider.execute(cmdSession);
70         Assert.assertFalse(checkNoActivity(messageIntelligenceAgency.provideIntelligence(),
71                 CHECK_NO_ACTIVITY_FUNCTION));
72     }
73 }