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;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.junit.Test;
16 import org.opendaylight.protocol.bmp.spi.parser.AbstractBmpMessageParser;
17 import org.opendaylight.protocol.bmp.spi.parser.BmpDeserializationException;
18 import org.opendaylight.protocol.util.ByteArray;
19 import org.opendaylight.protocol.util.ByteBufWriteUtil;
20 import org.opendaylight.yangtools.yang.binding.DataContainer;
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(), ((BmpTestMessage)this.registry.parseMessage(Unpooled.copiedBuffer(BMP_TEST_MESSAGE))).toString());
51 final ByteBuf aggregator = Unpooled.buffer(BMP_TEST_MESSAGE.length);
52 this.registry.serializeMessage(message, aggregator);
53 assertArrayEquals(BMP_TEST_MESSAGE, ByteArray.getAllBytes(aggregator));
56 @Test(expected=BmpDeserializationException.class)
57 public void testNotBmpTypeException() throws BmpDeserializationException {
58 this.exceptionMessage[0] = 0x01;
59 this.registry.parseMessage(Unpooled.copiedBuffer(this.exceptionMessage));
62 @Test(expected=BmpDeserializationException.class)
63 public void testLengthException() throws BmpDeserializationException {
64 this.exceptionMessage[4] = 0x01;
65 this.registry.parseMessage(Unpooled.copiedBuffer(this.exceptionMessage));
68 @Test(expected=BmpDeserializationException.class)
69 public void testInvalidMessageContextException() throws BmpDeserializationException {
70 this.registry.parseMessage(Unpooled.copiedBuffer(this.exceptionMessage, 0, this.exceptionMessage.length-2));
73 private static final class BmpTestParser extends AbstractBmpMessageParser {
76 public void serializeMessageBody(final Notification message, final ByteBuf buffer) {
77 ByteBufWriteUtil.writeUnsignedInt(((BmpTestMessage)message).getValue(), buffer);
81 public Notification parseMessageBody(final ByteBuf bytes) throws BmpDeserializationException {
82 return new BmpTestMessage(bytes.readUnsignedInt());
86 public int getBmpMessageType() {
91 private static final class BmpTestMessage implements Notification {
93 private final long value;
95 public BmpTestMessage(final long value) {
99 public long getValue() {
104 public Class<? extends DataContainer> getImplementedInterface() {
105 return BmpTestMessage.class;
109 public String toString() {
110 return "BmpTestMessage [value=" + this.value + "]";