2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.openflowjava.protocol.impl.serialization.match;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.PooledByteBufAllocator;
15 import org.junit.Test;
16 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Icmpv4Code;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Icmpv4CodeCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.icmpv4.code._case.Icmpv4CodeBuilder;
24 * Unit tests for OxmIcmpv4CodeSerializer.
26 * @author michal.polkorab
28 public class OxmIcmpv4CodeSerializerTest {
30 OxmIcmpv4CodeSerializer serializer = new OxmIcmpv4CodeSerializer();
33 * Test correct serialization.
36 public void testSerialize() {
37 MatchEntryBuilder builder = prepareIcmpv4CodeMatchEntry((short) 200);
39 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
40 serializer.serialize(builder.build(), buffer);
42 checkHeader(buffer, false);
43 assertEquals("Wrong value", 200, buffer.readUnsignedByte());
44 assertTrue("Unexpected data", buffer.readableBytes() == 0);
48 * Test correct header serialization.
51 public void testSerializeHeader() {
52 MatchEntryBuilder builder = prepareIcmpv4CodeHeader(false);
54 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
55 serializer.serializeHeader(builder.build(), buffer);
57 checkHeader(buffer, false);
58 assertTrue("Unexpected data", buffer.readableBytes() == 0);
62 * Test correct oxm-class return value.
65 public void testGetOxmClassCode() {
66 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
70 * Test correct oxm-field return value.
73 public void getOxmFieldCode() {
74 assertEquals("Wrong oxm-class", OxmMatchConstants.ICMPV4_CODE, serializer.getOxmFieldCode());
78 * Test correct value length return value.
81 public void testGetValueLength() {
82 assertEquals("Wrong value length", Byte.BYTES, serializer.getValueLength());
85 private static MatchEntryBuilder prepareIcmpv4CodeMatchEntry(short value) {
86 MatchEntryBuilder builder = prepareIcmpv4CodeHeader(false);
87 Icmpv4CodeCaseBuilder casebuilder = new Icmpv4CodeCaseBuilder();
88 Icmpv4CodeBuilder valueBuilder = new Icmpv4CodeBuilder();
89 valueBuilder.setIcmpv4Code(value);
90 casebuilder.setIcmpv4Code(valueBuilder.build());
91 builder.setMatchEntryValue(casebuilder.build());
95 private static MatchEntryBuilder prepareIcmpv4CodeHeader(boolean hasMask) {
96 MatchEntryBuilder builder = new MatchEntryBuilder();
97 builder.setOxmClass(OpenflowBasicClass.class);
98 builder.setOxmMatchField(Icmpv4Code.class);
99 builder.setHasMask(hasMask);
103 private static void checkHeader(ByteBuf buffer, boolean hasMask) {
104 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
105 short fieldAndMask = buffer.readUnsignedByte();
106 assertEquals("Wrong oxm-field", OxmMatchConstants.ICMPV4_CODE, fieldAndMask >>> 1);
107 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
108 assertEquals("Wrong length", Byte.BYTES, buffer.readUnsignedByte());