Merge "Bug 5543 - Bo: Update JUnit tests part_5"
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / match / CtZoneCodecTest.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.NxmNxCtZone;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.ct.zone.grouping.CtZoneValuesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.CtZoneCaseValue;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.CtZoneCaseValueBuilder;
25
26 public class CtZoneCodecTest {
27
28     CtZoneCodec ctZoneCodec;
29     ByteBuf buffer;
30     MatchEntry input;
31
32     private static final int VALUE_LENGTH = 2;
33     private static final int NXM_FIELD_CODE = 106;
34
35
36
37     @Before
38     public void setUp() {
39         ctZoneCodec = new CtZoneCodec();
40         buffer = ByteBufAllocator.DEFAULT.buffer();
41     }
42
43     @Test
44     public void serializeTest() {
45         input = createMatchEntry();
46         ctZoneCodec.serialize(input, buffer);
47
48         assertEquals(OxmMatchConstants.NXM_1_CLASS, buffer.readUnsignedShort());
49         short fieldMask = buffer.readUnsignedByte();
50         assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
51         assertEquals(0, fieldMask & 1);
52         assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
53         assertEquals(1, buffer.readUnsignedShort());
54     }
55
56     @Test
57     public void deserializeTest() {
58         createBuffer(buffer);
59         input = ctZoneCodec.deserialize(buffer);
60
61         CtZoneCaseValue result = ((CtZoneCaseValue) input.getMatchEntryValue());
62
63         assertEquals(Nxm1Class.class, input.getOxmClass());
64         assertEquals(NxmNxCtZone.class, input.getOxmMatchField());
65         assertEquals(false, input.isHasMask());
66         assertEquals(2, result.getCtZoneValues().getCtZone().shortValue());
67     }
68
69
70     private MatchEntry createMatchEntry() {
71         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
72         CtZoneCaseValueBuilder caseBuilder = new CtZoneCaseValueBuilder();
73         CtZoneValuesBuilder valuesBuilder = new CtZoneValuesBuilder();
74
75         matchEntryBuilder.setOxmClass(Nxm1Class.class);
76         matchEntryBuilder.setOxmMatchField(NxmNxCtZone.class);
77         matchEntryBuilder.setHasMask(false);
78
79         valuesBuilder.setCtZone(1);
80
81         caseBuilder.setCtZoneValues(valuesBuilder.build());
82         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
83         return matchEntryBuilder.build();
84     }
85
86     private void createBuffer(ByteBuf message) {
87         message.writeShort(OxmMatchConstants.NXM_1_CLASS);
88
89         int fieldMask = (NXM_FIELD_CODE << 1);
90         message.writeByte(fieldMask);
91         message.writeByte(VALUE_LENGTH);
92         message.writeShort(2);
93     }
94 }