Remove trailing whitespace
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / OFDecoderStatisticsTest.java
index 9462de15e7441048034f1e3970fff94b068a880c..409e475d4603b6e07673e32dfa757c137fb14057 100644 (file)
@@ -27,84 +27,115 @@ import org.opendaylight.openflowjava.statistics.CounterEventTypes;
 import org.opendaylight.openflowjava.statistics.StatisticsCounters;
 import org.opendaylight.openflowjava.util.ByteBufUtils;
 import org.opendaylight.yangtools.yang.binding.DataObject;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
+ * Test to count decoder events (counters US_DECODE_SUCCESS, US_DECODE_FAIL and
+ * US_RECEIVED_IN_OFJAVA have to be enabled)
+ *
  * @author madamjak
  *
  */
 public class OFDecoderStatisticsTest {
 
-    private static final Logger LOGGER = LoggerFactory.getLogger(OFEncoderStatisticsTest.class);
-
-    @Mock ChannelHandlerContext mockChHndlrCtx ;
-    @Mock DeserializationFactory mockDeserializationFactory ;
-    @Mock DataObject mockDataObject ;
+    @Mock ChannelHandlerContext mockChHndlrCtx;
+    @Mock DeserializationFactory mockDeserializationFactory;
+    @Mock DataObject mockDataObject;
 
-    private OFDecoder ofDecoder ;
+    private OFDecoder ofDecoder;
     private ByteBuf writeObj;
     private VersionMessageWrapper inMsg;
     private List<Object> outList;
     private StatisticsCounters statCounters;
 
     /**
-     * Sets up test environment
-     * 
+     * Sets up test environment Start counting and reset counters before each
+     * test
      */
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
-        ofDecoder = new OFDecoder() ;
-        ofDecoder.setDeserializationFactory( mockDeserializationFactory ) ;
+        ofDecoder = new OFDecoder();
+        ofDecoder.setDeserializationFactory(mockDeserializationFactory);
         outList = new ArrayList<>();
         statCounters = StatisticsCounters.getInstance();
-        statCounters.resetCounters();
+        statCounters.startCounting(false, 0);
     }
 
+    /**
+     * Stop counting after each test
+     */
     @After
-    public void tierDown(){
-        statCounters.resetCounters();
+    public void tierDown() {
+        statCounters.stopCounting();
     }
 
+    /**
+     * Test decode success counter
+     */
     @Test
-    public void testDecodeSuccesfullCounter() throws InterruptedException {
+    public void testDecodeSuccesfullCounter() {
+        if (!statCounters.isCounterEnabled(CounterEventTypes.US_DECODE_SUCCESS)) {
+            Assert.fail("Counter " + CounterEventTypes.US_DECODE_SUCCESS + " is not enable");
+        }
+        if (!statCounters.isCounterEnabled(CounterEventTypes.US_DECODE_FAIL)) {
+            Assert.fail("Counter " + CounterEventTypes.US_DECODE_FAIL + " is not enable");
+        }
+        if (!statCounters
+                .isCounterEnabled(CounterEventTypes.US_RECEIVED_IN_OFJAVA)) {
+            Assert.fail("Counter " + CounterEventTypes.US_RECEIVED_IN_OFJAVA + " is not enable");
+        }
         int count = 4;
-        when(mockDeserializationFactory.deserialize( any(ByteBuf.class), anyShort() )).thenReturn(mockDataObject);
+        when(mockDeserializationFactory.deserialize(any(ByteBuf.class),anyShort())).thenReturn(mockDataObject);
         try {
-            for(int i = 0; i<count; i++){
+            for (int i = 0; i < count; i++) {
                 writeObj = ByteBufUtils.hexStringToByteBuf("16 03 01 00");
-                inMsg = new VersionMessageWrapper( (short)8, writeObj );
+                inMsg = new VersionMessageWrapper((short) 8, writeObj);
                 ofDecoder.decode(mockChHndlrCtx, inMsg, outList);
             }
         } catch (Exception e) {
             Assert.fail();
         }
-        LOGGER.debug("Waiting to event queue process");
-        Assert.assertEquals("Wrong - bad counter value for OFEncoder encode succesfully ", count, statCounters.getCounter(CounterEventTypes.US_DECODE_SUCCESS).getCounterValue());
+        Assert.assertEquals("Wrong - bad counter value for OFEncoder encode succesfully ",
+                count,statCounters.getCounter(CounterEventTypes.US_DECODE_SUCCESS).getCounterValue());
+        Assert.assertEquals(
+                "Wrong - different between RECEIVED_IN_OFJAVA and (US_DECODE_SUCCESS + US_DECODE_FAIL)",
+                statCounters.getCounter(CounterEventTypes.US_RECEIVED_IN_OFJAVA).getCounterValue(),
+                statCounters.getCounter(CounterEventTypes.US_DECODE_SUCCESS).getCounterValue()
+                + statCounters.getCounter(CounterEventTypes.US_DECODE_FAIL).getCounterValue());
     }
 
     /**
-     * @throws InterruptedException 
-     * 
+     * Test fail decode counter
      */
     @Test
-    public void testDecodeFailCounter() throws InterruptedException {
+    public void testDecodeFailCounter() {
+        if (!statCounters.isCounterEnabled(CounterEventTypes.US_DECODE_SUCCESS)) {
+            Assert.fail("Counter " + CounterEventTypes.US_DECODE_SUCCESS + " is not enable");
+        }
+        if (!statCounters.isCounterEnabled(CounterEventTypes.US_DECODE_FAIL)) {
+            Assert.fail("Counter " + CounterEventTypes.US_DECODE_FAIL + " is not enable");
+        }
+        if (!statCounters.isCounterEnabled(CounterEventTypes.US_RECEIVED_IN_OFJAVA)) {
+            Assert.fail("Counter " + CounterEventTypes.US_RECEIVED_IN_OFJAVA + " is not enable");
+        }
         int count = 2;
-        when(mockDeserializationFactory.deserialize( any(ByteBuf.class), anyShort() ))
-        .thenThrow(new IllegalArgumentException()) ;
+        when( mockDeserializationFactory.deserialize(any(ByteBuf.class),anyShort())).thenThrow(new IllegalArgumentException());
         try {
-            for(int i = 0; i<count; i++){
+            for (int i = 0; i < count; i++) {
                 writeObj = ByteBufUtils.hexStringToByteBuf("16 03 01 00");
-                inMsg = new VersionMessageWrapper( (short)8, writeObj );
+                inMsg = new VersionMessageWrapper((short) 8, writeObj);
                 ofDecoder.decode(mockChHndlrCtx, inMsg, outList);
             }
         } catch (Exception e) {
-            System.out.println("a");
             Assert.fail();
         }
-        LOGGER.debug("Waiting to event queue process");
-        Assert.assertEquals("Wrong - bad counter value for OFEncoder encode succesfully ", count, statCounters.getCounter(CounterEventTypes.US_DECODE_FAIL).getCounterValue());
-
+        Assert.assertEquals(
+                "Wrong - bad counter value for OFEncoder encode succesfully ",
+                count, statCounters.getCounter(CounterEventTypes.US_DECODE_FAIL).getCounterValue());
+        Assert.assertEquals(
+                "Wrong - different between RECEIVED_IN_OFJAVA and (US_DECODE_SUCCESS + US_DECODE_FAIL)",
+                statCounters.getCounter(CounterEventTypes.US_RECEIVED_IN_OFJAVA).getCounterValue(),
+                statCounters.getCounter(CounterEventTypes.US_DECODE_SUCCESS).getCounterValue()
+                + statCounters.getCounter(CounterEventTypes.US_DECODE_FAIL).getCounterValue());
     }
 }