2 * Copyright (c) 2015 Cisco Systems, Inc. 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
9 package org.opendaylight.protocol.bmp.spi.registry;
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import org.junit.Test;
17 import org.opendaylight.protocol.bmp.spi.parser.AbstractBmpMessageParser;
18 import org.opendaylight.protocol.bmp.spi.parser.BmpDeserializationException;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.protocol.util.ByteBufWriteUtil;
21 import org.opendaylight.yangtools.yang.binding.Notification;
23 public class SimpleBmpMessageRegistryTest {
25 private static final int MSG_TYPE = 15;
26 private final BmpMessageRegistry registry = new SimpleBmpMessageRegistry();
28 private static final byte[] BMP_TEST_MESSAGE = {
30 0x00, 0x00, 0x00, 0x0A,//message length
32 0x00, 0x00, 0x01, 0x01//payload
35 private final byte[] exceptionMessage = {
37 0x00, 0x00, 0x00, 0x0A,//message length
39 0x00, 0x00, 0x01, 0x01
43 public void testBmpMessageRegistry() throws BmpDeserializationException {
44 final BmpTestParser bmpTestParser = new BmpTestParser();
45 this.registry.registerBmpMessageParser(MSG_TYPE, bmpTestParser);
46 this.registry.registerBmpMessageSerializer(BmpTestMessage.class, bmpTestParser);
47 final BmpTestMessage message = new BmpTestMessage(257);
49 assertEquals(message.toString(), this.registry
50 .parseMessage(Unpooled.copiedBuffer(BMP_TEST_MESSAGE)).toString());
52 final ByteBuf aggregator = Unpooled.buffer(BMP_TEST_MESSAGE.length);
53 this.registry.serializeMessage(message, aggregator);
54 assertArrayEquals(BMP_TEST_MESSAGE, ByteArray.getAllBytes(aggregator));
57 @Test(expected = BmpDeserializationException.class)
58 public void testNotBmpTypeException() throws BmpDeserializationException {
59 this.exceptionMessage[0] = 0x01;
60 this.registry.parseMessage(Unpooled.copiedBuffer(this.exceptionMessage));
63 @Test(expected = BmpDeserializationException.class)
64 public void testLengthException() throws BmpDeserializationException {
65 this.exceptionMessage[4] = 0x01;
66 this.registry.parseMessage(Unpooled.copiedBuffer(this.exceptionMessage));
69 @Test(expected = BmpDeserializationException.class)
70 public void testInvalidMessageContextException() throws BmpDeserializationException {
71 this.registry.parseMessage(Unpooled.copiedBuffer(this.exceptionMessage, 0,
72 this.exceptionMessage.length - 2));
75 private static final class BmpTestParser extends AbstractBmpMessageParser {
78 public void serializeMessageBody(final Notification message, final ByteBuf buffer) {
79 ByteBufWriteUtil.writeUnsignedInt(((BmpTestMessage) message).getValue(), buffer);
83 public Notification parseMessageBody(final ByteBuf bytes) throws BmpDeserializationException {
84 return new BmpTestMessage(bytes.readUnsignedInt());
88 public int getBmpMessageType() {
93 private static final class BmpTestMessage implements Notification {
95 private final long value;
97 BmpTestMessage(final long value) {
106 public Class<BmpTestMessage> implementedInterface() {
107 return BmpTestMessage.class;
111 public String toString() {
112 return "BmpTestMessage [value=" + this.value + "]";