OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / karaf / ClearStatsCommandProviderTest.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 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 ClearStatsCommandProvider}.
26  */
27 public class ClearStatsCommandProviderTest extends AbstractKarafTest {
28
29     private ClearStatsCommandProvider clearStatsCommandProvider;
30     private static final Function<String, Boolean> CHECK_NO_ACTIVITY_FUNCTION = new Function<String, Boolean>() {
31         @Nullable
32         @Override
33         public Boolean apply(String input) {
34             return input.endsWith(": no activity detected");
35         }
36     };
37     private MessageIntelligenceAgency mi5;
38
39     @Override
40     public void doSetUp() {
41         clearStatsCommandProvider = new ClearStatsCommandProvider();
42         mi5 = OpenFlowPluginProviderImpl.getMessageIntelligenceAgency();
43         mi5.resetStatistics();
44         Mockito.when(cmdSession.getConsole()).thenReturn(console);
45     }
46
47     @After
48     public void tearDown() {
49         Mockito.verify(console).print(anyString());
50         mi5.resetStatistics();
51     }
52
53     /**
54      * Test for {@link ClearStatsCommandProvider#doExecute()} when no stats were touched before.
55      */
56     @Test
57     public void testDoExecute_clean() throws Exception {
58         Assert.assertTrue(checkNoActivity(mi5.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
59         clearStatsCommandProvider.execute(cmdSession);
60
61         Assert.assertTrue(checkNoActivity(mi5.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
62     }
63
64     /**
65      * Test for {@link ClearStatsCommandProvider#doExecute()} when stats were touched before.
66      */
67     @Test
68     public void testDoExecute_dirty() throws Exception {
69         mi5 = OpenFlowPluginProviderImpl.getMessageIntelligenceAgency();
70         Assert.assertTrue(checkNoActivity(mi5.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
71         mi5.spyMessage(OfHeader.class, MessageSpy.StatisticsGroup.FROM_SWITCH);
72         Assert.assertFalse(checkNoActivity(mi5.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
73
74         clearStatsCommandProvider.execute(cmdSession);
75         Assert.assertTrue(checkNoActivity(mi5.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
76     }
77 }