BUG-4340: raise test coverage to 80% - statistics related
[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 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 ClearStatsCommandProvider}
25  */
26 public class ClearStatsCommandProviderTest extends AbstractKarafTest {
27
28     private ClearStatsCommandProvider clearStatsCommandProvider;
29     private static final Function<String, Boolean> CHECK_NO_ACTIVITY_FUNCTION = new Function<String, Boolean>() {
30         @Nullable
31         @Override
32         public Boolean apply(String input) {
33             return input.endsWith(": no activity detected");
34         }
35     };
36     private MessageIntelligenceAgency mi5;
37
38     public void doSetUp() {
39         clearStatsCommandProvider = new ClearStatsCommandProvider();
40         mi5 = OpenFlowPluginProviderImpl.getMessageIntelligenceAgency();
41         mi5.resetStatistics();
42         Mockito.when(cmdSession.getConsole()).thenReturn(console);
43     }
44
45     @After
46     public void tearDown() {
47         Mockito.verify(console).print(Matchers.anyString());
48         mi5.resetStatistics();
49     }
50
51     /**
52      * test for {@link ClearStatsCommandProvider#doExecute()} when no stats were touched before
53      *
54      * @throws Exception
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      * @throws Exception
68      */
69     @Test
70     public void testDoExecute_dirty() throws Exception {
71         final MessageIntelligenceAgency mi5 = OpenFlowPluginProviderImpl.getMessageIntelligenceAgency();
72         Assert.assertTrue(checkNoActivity(mi5.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
73         mi5.spyMessage(OfHeader.class, MessageSpy.STATISTIC_GROUP.FROM_SWITCH);
74         Assert.assertFalse(checkNoActivity(mi5.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
75
76         clearStatsCommandProvider.execute(cmdSession);
77         Assert.assertTrue(checkNoActivity(mi5.provideIntelligence(), CHECK_NO_ACTIVITY_FUNCTION));
78     }
79 }