fe76838ab05cf574ff38f2ea68e30df38dd1dc8b
[bgpcep.git] / bmp / bmp-spi / src / test / java / org / opendaylight / protocol / bmp / spi / registry / SimpleBmpMessageRegistryTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.protocol.bmp.spi.registry;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13
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;
23
24 public class SimpleBmpMessageRegistryTest {
25
26     private static final int MSG_TYPE = 15;
27     private final BmpMessageRegistry registry = new SimpleBmpMessageRegistry();
28
29     private static final byte[] BMP_TEST_MESSAGE = {
30         0x03,//version
31         0x00, 0x00, 0x00, 0x0A,//message length
32         0x0F,//msg type
33         0x00, 0x00, 0x01, 0x01//payload
34     };
35
36     private final byte[] exceptionMessage = {
37         0x03,//version
38         0x00, 0x00, 0x00, 0x0A,//message length
39         0x0F,//msg type
40         0x00, 0x00, 0x01, 0x01
41     };
42
43     @Test
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);
49
50         assertEquals(message.toString(), this.registry
51                 .parseMessage(Unpooled.copiedBuffer(BMP_TEST_MESSAGE)).toString());
52
53         final ByteBuf aggregator = Unpooled.buffer(BMP_TEST_MESSAGE.length);
54         this.registry.serializeMessage(message, aggregator);
55         assertArrayEquals(BMP_TEST_MESSAGE, ByteArray.getAllBytes(aggregator));
56     }
57
58     @Test(expected = BmpDeserializationException.class)
59     public void testNotBmpTypeException() throws BmpDeserializationException {
60         this.exceptionMessage[0] = 0x01;
61         this.registry.parseMessage(Unpooled.copiedBuffer(this.exceptionMessage));
62     }
63
64     @Test(expected = BmpDeserializationException.class)
65     public void testLengthException() throws BmpDeserializationException {
66         this.exceptionMessage[4] = 0x01;
67         this.registry.parseMessage(Unpooled.copiedBuffer(this.exceptionMessage));
68     }
69
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));
74     }
75
76     private static final class BmpTestParser extends AbstractBmpMessageParser {
77
78         @Override
79         public void serializeMessageBody(final Notification message, final ByteBuf buffer) {
80             ByteBufWriteUtil.writeUnsignedInt(((BmpTestMessage) message).getValue(), buffer);
81         }
82
83         @Override
84         public Notification parseMessageBody(final ByteBuf bytes) throws BmpDeserializationException {
85             return new BmpTestMessage(bytes.readUnsignedInt());
86         }
87
88         @Override
89         public int getBmpMessageType() {
90             return MSG_TYPE;
91         }
92     }
93
94     private static final class BmpTestMessage implements Notification {
95
96         private final long value;
97
98         BmpTestMessage(final long value) {
99             this.value = value;
100         }
101
102         long getValue() {
103             return this.value;
104         }
105
106         @Override
107         public Class<? extends DataContainer> getImplementedInterface() {
108             return BmpTestMessage.class;
109         }
110
111         @Override
112         public String toString() {
113             return "BmpTestMessage [value=" + this.value + "]";
114         }
115
116     }
117
118 }