Merge "Refactor nsh fields to new encoding"
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / match / NshMdtypeCodecTest.java
1 /*
2  * Copyright (c) 2018 SUSE LINUX GmbH.  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 import static org.junit.Assert.assertFalse;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.ByteBufAllocator;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.nsh.mdtype.grouping.NshMdtypeValues;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.nsh.mdtype.grouping.NshMdtypeValuesBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.experimenter.id._case.NxExpMatchEntryValue;
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.NshMdtypeCaseValue;
22 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.NshMdtypeCaseValueBuilder;
23
24 public class NshMdtypeCodecTest {
25
26     private NshMdtypeCodec nshMdtypeCodec;
27     private ByteBuf buffer;
28
29     private static final Short MDTYPE_VALUE = (short) 1;
30
31     @Before
32     public void setUp() {
33         nshMdtypeCodec = new NshMdtypeCodec();
34         buffer = ByteBufAllocator.DEFAULT.buffer();
35     }
36
37     @Test
38     public void serializeValueTest() {
39         NxExpMatchEntryValue matchEntryValue = createMatchEntryValue(MDTYPE_VALUE);
40
41         nshMdtypeCodec.serializeValue(matchEntryValue, false, buffer);
42
43         assertEquals(MDTYPE_VALUE.shortValue(), buffer.readUnsignedByte());
44         assertFalse(buffer.isReadable());
45     }
46
47     @Test
48     public void deserializeValueTest() {
49         writeBuffer(buffer, MDTYPE_VALUE);
50
51         NxExpMatchEntryValue value = nshMdtypeCodec.deserializeValue(buffer, false);
52
53         assertEquals(MDTYPE_VALUE, ((NshMdtypeCaseValue) value).getNshMdtypeValues().getValue());
54         assertFalse(buffer.isReadable());
55     }
56
57     private NxExpMatchEntryValue createMatchEntryValue(Short value) {
58         NshMdtypeValues nshMdtypeValues = new NshMdtypeValuesBuilder().setValue(value).build();
59         return new NshMdtypeCaseValueBuilder().setNshMdtypeValues(nshMdtypeValues).build();
60     }
61
62     private void writeBuffer(ByteBuf message, Short value) {
63         message.writeByte(value.intValue());
64     }
65 }