Fix checkstyle
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / match / NsiCodecTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.nx.codec.match;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufAllocator;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.nsi.grouping.NsiValues;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.nsi.grouping.NsiValuesBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.experimenter.id._case.NxExpMatchEntryValue;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.experimenter.id._case.nx.exp.match.entry.value.NsiCaseValue;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.experimenter.id._case.nx.exp.match.entry.value.NsiCaseValueBuilder;
22
23 public class NsiCodecTest {
24
25     NsiCodec nsiCodec;
26     ByteBuf buffer;
27
28     private static final Short NSI_VALUE = (short) 255;
29     private static final Short NSI_MASK = 0xF0;
30
31     @Before
32     public void setUp() {
33         nsiCodec = new NsiCodec();
34         buffer = ByteBufAllocator.DEFAULT.buffer();
35     }
36
37     @Test
38     public void serializeTestNoMask() {
39         NxExpMatchEntryValue matchEntryValue = createMatchEntryValue(NSI_VALUE, null);
40
41         nsiCodec.serializeValue(matchEntryValue, false, buffer);
42
43         assertEquals(NSI_VALUE.shortValue(), buffer.readUnsignedByte());
44         assertFalse(buffer.isReadable());
45     }
46
47     @Test
48     public void serializeTestMask() {
49         NxExpMatchEntryValue matchEntryValue = createMatchEntryValue(NSI_VALUE, NSI_MASK);
50
51         nsiCodec.serializeValue(matchEntryValue, true, buffer);
52
53         assertEquals(NSI_VALUE.shortValue(), buffer.readUnsignedByte());
54         assertEquals(NSI_MASK.shortValue(), buffer.readUnsignedByte());
55         assertFalse(buffer.isReadable());
56     }
57
58     @Test
59     public void deserializeTestNoMask() {
60         writeBuffer(buffer, NSI_VALUE, null);
61
62         NxExpMatchEntryValue value = nsiCodec.deserializeValue(buffer, false);
63
64         assertEquals(NSI_VALUE, ((NsiCaseValue) value).getNsiValues().getNsi());
65         assertFalse(buffer.isReadable());
66     }
67
68     @Test
69     public void deserializeTestMask() {
70         writeBuffer(buffer, NSI_VALUE, NSI_MASK);
71
72         NxExpMatchEntryValue value = nsiCodec.deserializeValue(buffer, true);
73
74         assertEquals(NSI_VALUE, ((NsiCaseValue) value).getNsiValues().getNsi());
75         assertEquals(NSI_MASK, ((NsiCaseValue) value).getNsiValues().getMask());
76         assertFalse(buffer.isReadable());
77     }
78
79     private NxExpMatchEntryValue createMatchEntryValue(Short value, Short mask) {
80         NsiValues nsiValues = new NsiValuesBuilder().setNsi(value).setMask(mask).build();
81         return new NsiCaseValueBuilder().setNsiValues(nsiValues).build();
82     }
83
84     private void writeBuffer(ByteBuf message, Short value, Short mask) {
85         message.writeByte(value.intValue());
86         if (mask != null) {
87             message.writeByte(mask.intValue());
88         }
89     }
90 }