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