Remove trailing whitespace
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / OFDecoderStatisticsTest.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.protocol.impl.core;
9
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Matchers.anyShort;
12 import static org.mockito.Mockito.when;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.channel.ChannelHandlerContext;
15
16 import java.util.ArrayList;
17 import java.util.List;
18
19 import org.junit.After;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializationFactory;
26 import org.opendaylight.openflowjava.statistics.CounterEventTypes;
27 import org.opendaylight.openflowjava.statistics.StatisticsCounters;
28 import org.opendaylight.openflowjava.util.ByteBufUtils;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30
31 /**
32  * Test to count decoder events (counters US_DECODE_SUCCESS, US_DECODE_FAIL and
33  * US_RECEIVED_IN_OFJAVA have to be enabled)
34  *
35  * @author madamjak
36  *
37  */
38 public class OFDecoderStatisticsTest {
39
40     @Mock ChannelHandlerContext mockChHndlrCtx;
41     @Mock DeserializationFactory mockDeserializationFactory;
42     @Mock DataObject mockDataObject;
43
44     private OFDecoder ofDecoder;
45     private ByteBuf writeObj;
46     private VersionMessageWrapper inMsg;
47     private List<Object> outList;
48     private StatisticsCounters statCounters;
49
50     /**
51      * Sets up test environment Start counting and reset counters before each
52      * test
53      */
54     @Before
55     public void setUp() {
56         MockitoAnnotations.initMocks(this);
57         ofDecoder = new OFDecoder();
58         ofDecoder.setDeserializationFactory(mockDeserializationFactory);
59         outList = new ArrayList<>();
60         statCounters = StatisticsCounters.getInstance();
61         statCounters.startCounting(false, 0);
62     }
63
64     /**
65      * Stop counting after each test
66      */
67     @After
68     public void tierDown() {
69         statCounters.stopCounting();
70     }
71
72     /**
73      * Test decode success counter
74      */
75     @Test
76     public void testDecodeSuccesfullCounter() {
77         if (!statCounters.isCounterEnabled(CounterEventTypes.US_DECODE_SUCCESS)) {
78             Assert.fail("Counter " + CounterEventTypes.US_DECODE_SUCCESS + " is not enable");
79         }
80         if (!statCounters.isCounterEnabled(CounterEventTypes.US_DECODE_FAIL)) {
81             Assert.fail("Counter " + CounterEventTypes.US_DECODE_FAIL + " is not enable");
82         }
83         if (!statCounters
84                 .isCounterEnabled(CounterEventTypes.US_RECEIVED_IN_OFJAVA)) {
85             Assert.fail("Counter " + CounterEventTypes.US_RECEIVED_IN_OFJAVA + " is not enable");
86         }
87         int count = 4;
88         when(mockDeserializationFactory.deserialize(any(ByteBuf.class),anyShort())).thenReturn(mockDataObject);
89         try {
90             for (int i = 0; i < count; i++) {
91                 writeObj = ByteBufUtils.hexStringToByteBuf("16 03 01 00");
92                 inMsg = new VersionMessageWrapper((short) 8, writeObj);
93                 ofDecoder.decode(mockChHndlrCtx, inMsg, outList);
94             }
95         } catch (Exception e) {
96             Assert.fail();
97         }
98         Assert.assertEquals("Wrong - bad counter value for OFEncoder encode succesfully ",
99                 count,statCounters.getCounter(CounterEventTypes.US_DECODE_SUCCESS).getCounterValue());
100         Assert.assertEquals(
101                 "Wrong - different between RECEIVED_IN_OFJAVA and (US_DECODE_SUCCESS + US_DECODE_FAIL)",
102                 statCounters.getCounter(CounterEventTypes.US_RECEIVED_IN_OFJAVA).getCounterValue(),
103                 statCounters.getCounter(CounterEventTypes.US_DECODE_SUCCESS).getCounterValue()
104                 + statCounters.getCounter(CounterEventTypes.US_DECODE_FAIL).getCounterValue());
105     }
106
107     /**
108      * Test fail decode counter
109      */
110     @Test
111     public void testDecodeFailCounter() {
112         if (!statCounters.isCounterEnabled(CounterEventTypes.US_DECODE_SUCCESS)) {
113             Assert.fail("Counter " + CounterEventTypes.US_DECODE_SUCCESS + " is not enable");
114         }
115         if (!statCounters.isCounterEnabled(CounterEventTypes.US_DECODE_FAIL)) {
116             Assert.fail("Counter " + CounterEventTypes.US_DECODE_FAIL + " is not enable");
117         }
118         if (!statCounters.isCounterEnabled(CounterEventTypes.US_RECEIVED_IN_OFJAVA)) {
119             Assert.fail("Counter " + CounterEventTypes.US_RECEIVED_IN_OFJAVA + " is not enable");
120         }
121         int count = 2;
122         when( mockDeserializationFactory.deserialize(any(ByteBuf.class),anyShort())).thenThrow(new IllegalArgumentException());
123         try {
124             for (int i = 0; i < count; i++) {
125                 writeObj = ByteBufUtils.hexStringToByteBuf("16 03 01 00");
126                 inMsg = new VersionMessageWrapper((short) 8, writeObj);
127                 ofDecoder.decode(mockChHndlrCtx, inMsg, outList);
128             }
129         } catch (Exception e) {
130             Assert.fail();
131         }
132         Assert.assertEquals(
133                 "Wrong - bad counter value for OFEncoder encode succesfully ",
134                 count, statCounters.getCounter(CounterEventTypes.US_DECODE_FAIL).getCounterValue());
135         Assert.assertEquals(
136                 "Wrong - different between RECEIVED_IN_OFJAVA and (US_DECODE_SUCCESS + US_DECODE_FAIL)",
137                 statCounters.getCounter(CounterEventTypes.US_RECEIVED_IN_OFJAVA).getCounterValue(),
138                 statCounters.getCounter(CounterEventTypes.US_DECODE_SUCCESS).getCounterValue()
139                 + statCounters.getCounter(CounterEventTypes.US_DECODE_FAIL).getCounterValue());
140     }
141 }