Migrate openflow-protocol-impl tests to Uint types
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / OFEncoderStatisticsTest.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.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.anyShort;
12 import static org.mockito.Mockito.doThrow;
13 import static org.mockito.Mockito.when;
14
15 import io.netty.buffer.ByteBuf;
16 import io.netty.channel.ChannelHandlerContext;
17 import io.netty.util.concurrent.Future;
18 import io.netty.util.concurrent.GenericFutureListener;
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.api.util.EncodeConstants;
26 import org.opendaylight.openflowjava.protocol.impl.core.connection.MessageListenerWrapper;
27 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;
28 import org.opendaylight.openflowjava.statistics.CounterEventTypes;
29 import org.opendaylight.openflowjava.statistics.StatisticsCounters;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.opendaylight.yangtools.yang.common.Uint8;
34
35 /**
36  * Test counters for encoding (at least DS_ENCODE_SUCCESS, DS_ENCODE_FAIL and DS_FLOW_MODS_SENT counters have to
37  * be enabled).
38  *
39  * @author madamjak
40  */
41 public class OFEncoderStatisticsTest {
42
43     @Mock ChannelHandlerContext mockChHndlrCtx ;
44     @Mock SerializationFactory mockSerializationFactory ;
45     @Mock MessageListenerWrapper wrapper;
46     @Mock OfHeader mockMsg ;
47     @Mock ByteBuf mockOut ;
48     @Mock Future<Void> future;
49     @Mock GenericFutureListener<Future<Void>> listener;
50     @Mock FlowModInput mockFlowModInput;
51
52     private StatisticsCounters statCounters;
53     private OFEncoder ofEncoder;
54
55     /**
56      * Initialize tests, start and reset counters before each test.
57      */
58     @Before
59     public void initTlest() {
60         MockitoAnnotations.initMocks(this);
61         ofEncoder = new OFEncoder() ;
62         ofEncoder.setSerializationFactory(mockSerializationFactory) ;
63         statCounters = StatisticsCounters.getInstance();
64         statCounters.startCounting(false, 0);
65     }
66
67     /**
68      * Stop counting after each test.
69      */
70     @After
71     public void tierDown() {
72         statCounters.stopCounting();
73     }
74
75     /**
76      * Test counting of success encode (counter DS_ENCODE_SUCCESS has to be enabled).
77      */
78     @Test
79     public void testEncodeSuccessCounter() throws Exception {
80         CounterEventTypes cet = CounterEventTypes.DS_ENCODE_SUCCESS;
81         if (!statCounters.isCounterEnabled(cet)) {
82             Assert.fail("Counter " + cet + " is not enabled.");
83         }
84
85         when(mockOut.readableBytes()).thenReturn(1);
86         when(wrapper.getMsg()).thenReturn(mockMsg);
87         when(wrapper.getMsg().getVersion()).thenReturn(Uint8.valueOf(EncodeConstants.OF13_VERSION_ID));
88
89         int count = 4;
90         for (int i = 0; i < count; i++) {
91             ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
92         }
93
94         Assert.assertEquals("Wrong - bad counter value for OFEncoder encode succesfully ", count,
95                 statCounters.getCounter(cet).getCounterValue());
96     }
97
98     /**
99      * Test counting of flow-mod sent (counter DS_FLOW_MODS_SENT has to be enabled).
100      */
101     @Test
102     public void testFlowModSentCounter() throws Exception {
103         CounterEventTypes cet = CounterEventTypes.DS_FLOW_MODS_SENT;
104         if (!statCounters.isCounterEnabled(cet)) {
105             Assert.fail("Counter " + cet + " is not enabled.");
106         }
107         when(mockOut.readableBytes()).thenReturn(1);
108         when(wrapper.getMsg()).thenReturn(mockFlowModInput);
109         when(wrapper.getMsg().getVersion()).thenReturn(Uint8.valueOf(EncodeConstants.OF13_VERSION_ID));
110
111         int count = 4;
112         for (int i = 0; i < count; i++) {
113             ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
114         }
115
116         Assert.assertEquals("Wrong - bad counter value for OFEncoder flow-mod sent", count,
117                 statCounters.getCounter(cet).getCounterValue());
118     }
119
120     /**
121      * Test counting of encode fail (counter DS_ENCODE_FAIL has to be enabled).
122      */
123     @Test
124     public void testEncodeEncodeFailCounter() throws Exception {
125         CounterEventTypes cet = CounterEventTypes.DS_ENCODE_FAIL;
126         if (!statCounters.isCounterEnabled(cet)) {
127             Assert.fail("Counter " + cet + " is not enabled.");
128         }
129
130         when(wrapper.getMsg()).thenReturn(mockMsg);
131         when(wrapper.getListener()).thenReturn(listener);
132         when(wrapper.getMsg().getVersion()).thenReturn(Uint8.valueOf(EncodeConstants.OF13_VERSION_ID));
133         doThrow(new IllegalArgumentException()).when(mockSerializationFactory).messageToBuffer(anyShort(),
134                 any(ByteBuf.class), any(DataObject.class));
135
136         int count = 2;
137         for (int i = 0; i < count; i++) {
138             ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
139         }
140
141         Assert.assertEquals("Wrong - bad counter value for OFEncoder fail encode", count,
142                 statCounters.getCounter(cet).getCounterValue());
143     }
144 }