1bb549b6fedb5f9b1efbc478ae51ecb716aac7a9
[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(true,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.startLogReport();
105         Assert.assertTrue("Wrong - logRepoter is not running", statCounters.isRunLogReport());
106         Assert.assertEquals("Wrong - bad logReportPeriod", testDelay, statCounters.getLogReportPeriod());
107         statCounters.stopLogReport();
108         Assert.assertFalse("Wrong - logRepoter is running", statCounters.isRunLogReport());
109         statCounters.startLogReport(statCounters.MINIMAL_LOG_REPORT_PERIOD / 2);
110         Assert.assertTrue("Wrong - logRepoter is not running", statCounters.isRunLogReport());
111         Assert.assertEquals("Wrong - bad logReportPeriod", statCounters.MINIMAL_LOG_REPORT_PERIOD, statCounters.getLogReportPeriod());
112         statCounters.stopCounting();
113         Assert.assertFalse("Wrong - logRepoter is running", statCounters.isRunLogReport());
114     }
115
116     /**
117      * Test start log report with bad logReportDealy
118      */
119     @Test(expected = IllegalArgumentException.class)
120     public void testLogReportBadPeriod(){
121         statCounters.startLogReport(-1);
122     }
123
124     /**
125      * Test to get counter with null key
126      */
127     @Test(expected = IllegalArgumentException.class)
128     public void testGetCounterbyNull(){
129         statCounters.getCounter(null);
130     }
131
132     private void incrementCounter(CounterEventTypes cet, int count){
133         if(!statCounters.isCounterEnabled(cet)){
134             return;
135         }
136         for(int i = 0; i< count; i++){
137             statCounters.incrementCounter(cet);
138         }
139     }
140 }