Bug 5543 - Bo: Update JUnit tests part_9
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / match / Reg2CodecTest.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
9 package org.opendaylight.openflowjava.nx.codec.match;
10
11 import static org.junit.Assert.assertEquals;
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.openflowjava.protocol.api.util.OxmMatchConstants;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm1Class;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.reg.grouping.RegValuesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.RegCaseValue;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.RegCaseValueBuilder;
25
26 public class Reg2CodecTest {
27     Reg2Codec reg2Codec;
28     ByteBuf buffer;
29     MatchEntry input;
30
31     private static final int NXM_FIELD_CODE = 2;
32     private static final int VALUE_LENGTH = 4;
33
34     @Before
35     public void setUp() {
36         reg2Codec = new Reg2Codec();
37         buffer = ByteBufAllocator.DEFAULT.buffer();
38     }
39
40     @Test
41     public void serializeTest() {
42         input = createMatchEntry();
43         reg2Codec.serialize(input, buffer);
44
45         assertEquals(OxmMatchConstants.NXM_1_CLASS, buffer.readUnsignedShort());
46         short fieldMask = buffer.readUnsignedByte();
47         assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
48         assertEquals(0, fieldMask & 1);
49         assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
50         assertEquals(1, buffer.readUnsignedInt());
51     }
52
53     @Test
54     public void deserializeTest() {
55         createBuffer(buffer);
56
57         input = reg2Codec.deserialize(buffer);
58
59         RegCaseValue result = ((RegCaseValue) input.getMatchEntryValue());
60
61         assertEquals(Nxm1Class.class, input.getOxmClass());
62         assertEquals(NxmNxReg2.class, input.getOxmMatchField());
63         assertEquals(false, input.isHasMask());
64         assertEquals(1, result.getRegValues().getValue().intValue());
65     }
66
67     private MatchEntry createMatchEntry() {
68         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
69         RegCaseValueBuilder caseBuilder = new RegCaseValueBuilder();
70         RegValuesBuilder valuesBuilder = new RegValuesBuilder();
71
72         matchEntryBuilder.setOxmClass(Nxm1Class.class);
73         matchEntryBuilder.setOxmMatchField(NxmNxReg2.class);
74         matchEntryBuilder.setHasMask(false);
75
76         valuesBuilder.setValue((long)1);
77
78         caseBuilder.setRegValues(valuesBuilder.build());
79         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
80         return matchEntryBuilder.build();
81     }
82
83     private void createBuffer(ByteBuf message) {
84         message.writeShort(OxmMatchConstants.NXM_1_CLASS);
85
86         int fieldMask = (NXM_FIELD_CODE << 1);
87         message.writeByte(fieldMask);
88         message.writeByte(VALUE_LENGTH);
89         message.writeInt(1);
90     }
91 }