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.DataContainer;
22 import org.opendaylight.yangtools.yang.binding.Notification;
24 public class SimpleBmpMessageRegistryTest {
26 private static final int MSG_TYPE = 15;
27 private final BmpMessageRegistry registry = new SimpleBmpMessageRegistry();
29 private static final byte[] BMP_TEST_MESSAGE = {
31 0x00, 0x00, 0x00, 0x0A,//message length
33 0x00, 0x00, 0x01, 0x01//payload
36 private final byte[] exceptionMessage = {
38 0x00, 0x00, 0x00, 0x0A,//message length
40 0x00, 0x00, 0x01, 0x01
44 public void testBmpMessageRegistry() throws BmpDeserializationException {
45 final BmpTestParser bmpTestParser = new BmpTestParser();
46 this.registry.registerBmpMessageParser(MSG_TYPE, bmpTestParser);
47 this.registry.registerBmpMessageSerializer(BmpTestMessage.class, bmpTestParser);
48 final BmpTestMessage message = new BmpTestMessage(257);
50 assertEquals(message.toString(), this.registry
51 .parseMessage(Unpooled.copiedBuffer(BMP_TEST_MESSAGE)).toString());
53 final ByteBuf aggregator = Unpooled.buffer(BMP_TEST_MESSAGE.length);
54 this.registry.serializeMessage(message, aggregator);
55 assertArrayEquals(BMP_TEST_MESSAGE, ByteArray.getAllBytes(aggregator));
58 @Test(expected = BmpDeserializationException.class)
59 public void testNotBmpTypeException() throws BmpDeserializationException {
60 this.exceptionMessage[0] = 0x01;
61 this.registry.parseMessage(Unpooled.copiedBuffer(this.exceptionMessage));
64 @Test(expected = BmpDeserializationException.class)
65 public void testLengthException() throws BmpDeserializationException {
66 this.exceptionMessage[4] = 0x01;
67 this.registry.parseMessage(Unpooled.copiedBuffer(this.exceptionMessage));
70 @Test(expected = BmpDeserializationException.class)
71 public void testInvalidMessageContextException() throws BmpDeserializationException {
72 this.registry.parseMessage(Unpooled.copiedBuffer(this.exceptionMessage, 0,
73 this.exceptionMessage.length - 2));
76 private static final class BmpTestParser extends AbstractBmpMessageParser {
79 public void serializeMessageBody(final Notification message, final ByteBuf buffer) {
80 ByteBufWriteUtil.writeUnsignedInt(((BmpTestMessage) message).getValue(), buffer);
84 public Notification parseMessageBody(final ByteBuf bytes) throws BmpDeserializationException {
85 return new BmpTestMessage(bytes.readUnsignedInt());
89 public int getBmpMessageType() {
94 private static final class BmpTestMessage implements Notification {
96 private final long value;
98 BmpTestMessage(final long value) {
107 public Class<? extends DataContainer> getImplementedInterface() {
108 return BmpTestMessage.class;
112 public String toString() {
113 return "BmpTestMessage [value=" + this.value + "]";