Fix checkstyle warnings for impl/protocol test package
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / protocol / serialization / instructions / AbstractInstructionSerializerTest.java
1 /*
2  * Copyright (c) 2016 Pantheon Technologies s.r.o. 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.openflowplugin.impl.protocol.serialization.instructions;
10
11 import static org.junit.Assert.assertEquals;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.UnpooledByteBufAllocator;
15 import java.util.function.Consumer;
16 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.openflowjava.protocol.impl.util.InstructionConstants;
19 import org.opendaylight.openflowplugin.impl.protocol.serialization.AbstractSerializerTest;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction;
21
22 public abstract class AbstractInstructionSerializerTest extends AbstractSerializerTest {
23     private AbstractInstructionSerializer serializer;
24
25     @Override
26     protected void init() {
27         serializer = getRegistry().getSerializer(new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, getClazz()));
28     }
29
30     protected void assertInstruction(final Instruction instruction, final Consumer<ByteBuf> assertBody) {
31         // Header serialization
32         final ByteBuf bufferHeader = UnpooledByteBufAllocator.DEFAULT.buffer();
33         serializer.serializeHeader(instruction, bufferHeader);
34         assertEquals(bufferHeader.readUnsignedShort(), getType());
35         assertEquals(bufferHeader.readUnsignedShort(), InstructionConstants.INSTRUCTION_IDS_LENGTH);
36         assertEquals(bufferHeader.readableBytes(), 0);
37
38         // Header and body serialization
39         final ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
40         serializer.serialize(instruction, buffer);
41         assertEquals(buffer.readUnsignedShort(), getType());
42         assertEquals(buffer.readUnsignedShort(), getLength());
43         assertBody.accept(buffer);
44         assertEquals(buffer.readableBytes(), 0);
45     }
46
47     protected AbstractInstructionSerializer getSerializer() {
48         return serializer;
49     }
50
51     protected abstract Class<? extends Instruction> getClazz();
52
53     protected abstract int getType();
54
55     protected abstract int getLength();
56 }