d922d590e99717b13bd5cfce46862d45b5870206
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / statistics / StatisticsCountersTest.java
1 /*
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. 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 package org.opendaylight.openflowjava.statistics;
9
10 import org.junit.After;
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /**
18  * General tests for StatisticsCounters class 
19  * @author madamjak
20  *
21  */
22 public class StatisticsCountersTest {
23
24     private static final Logger LOGGER = LoggerFactory.getLogger(StatisticsCounters.class);
25     private StatisticsCounters statCounters;
26
27     /**
28      * Initialize StatisticsCounters before each test, reset counters
29      */
30     @Before
31     public void initTest(){
32         statCounters = StatisticsCounters.getInstance();
33         statCounters.startCounting(false, 0);
34     }
35
36     /**
37      * Stop counting after each test
38      */
39     @After
40     public void tierDown(){
41         statCounters.stopCounting();
42     }
43
44     /**
45      * Basic test of increment and reset counters
46      */
47     @Test
48     public void testCounterAll() {
49         int testCount = 4;
50         for(CounterEventTypes cet : CounterEventTypes.values()){
51             if(statCounters.isCounterEnabled(cet)){
52                 incrementCounter(cet,testCount);
53                 Assert.assertEquals("Wrong - bad counter value " + cet, testCount, statCounters.getCounter(cet).getCounterValue());
54             } else {
55                 Assert.assertNull("Wrong - not enabled counter give not null value", statCounters.getCounter(cet));
56             }
57             
58         }
59         statCounters.resetCounters();
60         for(CounterEventTypes cet : CounterEventTypes.values()){
61             if(statCounters.isCounterEnabled(cet)){
62                 Assert.assertEquals("Wrong - bad counter value after reset " + cet, 0, statCounters.getCounter(cet).getCounterValue());
63             }
64         }
65     }
66
67     /**
68      * Test to store current and last read value of counter (at least one counter has to be enabled)
69      */
70     @Test
71     public void testCounterLastRead() {
72         int testCount = 4;
73         CounterEventTypes firstEnabledCET = null;
74         for(CounterEventTypes  cet : CounterEventTypes.values()){
75             if(statCounters.isCounterEnabled(cet)){
76                 firstEnabledCET = cet;
77                 break;
78             }
79         }
80         if(firstEnabledCET == null){
81             Assert.fail("No counter is enabled");
82         }
83         incrementCounter(firstEnabledCET,testCount);
84         LOGGER.debug("Waiting to process event queue");
85         Assert.assertEquals("Wrong - bad last read value.", 0,statCounters.getCounter(firstEnabledCET).getCounterLastReadValue());
86         Assert.assertEquals("Wrong - bad value", testCount,statCounters.getCounter(firstEnabledCET).getCounterValue(false));
87         Assert.assertEquals("Wrong - bad last read value.", 0,statCounters.getCounter(firstEnabledCET).getCounterLastReadValue());
88         Assert.assertEquals("Wrong - bad last read value.", testCount,statCounters.getCounter(firstEnabledCET).getCounterValue());
89         Assert.assertEquals("Wrong - bad last read value.", testCount,statCounters.getCounter(firstEnabledCET).getCounterLastReadValue());
90         incrementCounter(firstEnabledCET,testCount);
91         Assert.assertEquals("Wrong - bad last read value.", testCount,statCounters.getCounter(firstEnabledCET).getCounterLastReadValue());
92         Assert.assertEquals("Wrong - bad last read value.", 2*testCount,statCounters.getCounter(firstEnabledCET).getCounterValue());
93     }
94
95     /**
96      * Test start and stop log reporter
97      */
98     @Test
99     public void testStartStopLogReporter(){
100         int testDelay = 10000;
101         statCounters.startLogReport(testDelay);
102         Assert.assertTrue("Wrong - logRepoter is not running", statCounters.isRunLogReport());
103         Assert.assertEquals("Wrong - bad logReportPeriod", testDelay, statCounters.getLogReportPeriod());
104         statCounters.stopLogReport();
105         Assert.assertFalse("Wrong - logRepoter is running", statCounters.isRunLogReport());
106         statCounters.startLogReport(StatisticsCounters.MINIMAL_LOG_REPORT_PERIOD / 2);
107         Assert.assertTrue("Wrong - logRepoter is not running", statCounters.isRunLogReport());
108         Assert.assertEquals("Wrong - bad logReportPeriod", StatisticsCounters.MINIMAL_LOG_REPORT_PERIOD, statCounters.getLogReportPeriod());
109         statCounters.stopCounting();
110         Assert.assertFalse("Wrong - logRepoter is running", statCounters.isRunLogReport());
111     }
112
113     /**
114      * Test start log report with bad logReportDealy
115      */
116     @Test(expected = IllegalArgumentException.class)
117     public void testLogReportBadPeriod(){
118         statCounters.startLogReport(0);
119     }
120
121     /**
122      * Test to get counter with null key
123      */
124     @Test(expected = IllegalArgumentException.class)
125     public void testGetCounterbyNull(){
126         statCounters.getCounter(null);
127     }
128
129     private void incrementCounter(CounterEventTypes cet, int count){
130         if(!statCounters.isCounterEnabled(cet)){
131             return;
132         }
133         for(int i = 0; i< count; i++){
134             statCounters.incrementCounter(cet);
135         }
136     }
137 }