Merge "Bug 5543 - Bo: Update JUnit tests part_5"
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / match / CtStateCodecTest.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.NxmNxCtState;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.ct.state.grouping.CtStateValuesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.CtStateCaseValue;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.CtStateCaseValueBuilder;
25
26 public class CtStateCodecTest {
27
28     CtStateCodec ctStateCodec;
29     ByteBuf buffer;
30     MatchEntry input;
31
32     private static final int VALUE_LENGTH = 8;
33     private static final int NXM_FIELD_CODE = 105;
34
35     @Before
36     public void setUp() {
37         ctStateCodec = new CtStateCodec();
38         buffer = ByteBufAllocator.DEFAULT.buffer();
39     }
40
41     @Test
42     public void serializeTest() {
43         input = createMatchEntry();
44         ctStateCodec.serialize(input, buffer);
45
46         assertEquals(OxmMatchConstants.NXM_1_CLASS, buffer.readUnsignedShort());
47         short fieldMask = buffer.readUnsignedByte();
48         assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
49         assertEquals(1, fieldMask & 1);
50         assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
51         assertEquals(1, buffer.readUnsignedInt());
52         assertEquals(2, buffer.readUnsignedInt());
53     }
54
55     @Test
56     public void deserializeTest() {
57         createBuffer(buffer);
58         input = ctStateCodec.deserialize(buffer);
59
60         CtStateCaseValue result = ((CtStateCaseValue) input.getMatchEntryValue());
61
62         assertEquals(Nxm1Class.class, input.getOxmClass());
63         assertEquals(NxmNxCtState.class, input.getOxmMatchField());
64         assertEquals(true, input.isHasMask());
65         assertEquals(1, result.getCtStateValues().getCtState().intValue());
66         assertEquals(2, result.getCtStateValues().getMask().intValue());
67     }
68
69     private MatchEntry createMatchEntry() {
70         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
71         CtStateCaseValueBuilder caseBuilder = new CtStateCaseValueBuilder();
72         CtStateValuesBuilder valuesBuilder = new CtStateValuesBuilder();
73
74         matchEntryBuilder.setOxmClass(Nxm1Class.class);
75         matchEntryBuilder.setOxmMatchField(NxmNxCtState.class);
76         matchEntryBuilder.setHasMask(true);
77
78         valuesBuilder.setCtState((long)1);
79         valuesBuilder.setMask((long)2);
80
81         caseBuilder.setCtStateValues(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         //CtState = 1
93         message.writeInt(1);
94         //Mask = 2
95         message.writeInt(2);
96     }
97
98 }