Code clean up
[bgpcep.git] / bgp / 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 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;
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.parseMessage(Unpooled.copiedBuffer(BMP_TEST_MESSAGE)).toString());
50
51         final ByteBuf aggregator = Unpooled.buffer(BMP_TEST_MESSAGE.length);
52         this.registry.serializeMessage(message, aggregator);
53         assertArrayEquals(BMP_TEST_MESSAGE, ByteArray.getAllBytes(aggregator));
54     }
55
56     @Test(expected=BmpDeserializationException.class)
57     public void testNotBmpTypeException() throws BmpDeserializationException {
58         this.exceptionMessage[0] = 0x01;
59         this.registry.parseMessage(Unpooled.copiedBuffer(this.exceptionMessage));
60     }
61
62     @Test(expected=BmpDeserializationException.class)
63     public void testLengthException() throws BmpDeserializationException {
64         this.exceptionMessage[4] = 0x01;
65         this.registry.parseMessage(Unpooled.copiedBuffer(this.exceptionMessage));
66     }
67
68     @Test(expected=BmpDeserializationException.class)
69     public void testInvalidMessageContextException() throws BmpDeserializationException {
70         this.registry.parseMessage(Unpooled.copiedBuffer(this.exceptionMessage, 0, this.exceptionMessage.length-2));
71     }
72
73     private static final class BmpTestParser extends AbstractBmpMessageParser {
74
75         @Override
76         public void serializeMessageBody(final Notification message, final ByteBuf buffer) {
77             ByteBufWriteUtil.writeUnsignedInt(((BmpTestMessage)message).getValue(), buffer);
78         }
79
80         @Override
81         public Notification parseMessageBody(final ByteBuf bytes) throws BmpDeserializationException {
82             return new BmpTestMessage(bytes.readUnsignedInt());
83         }
84
85         @Override
86         public int getBmpMessageType() {
87             return MSG_TYPE;
88         }
89     }
90
91     private static final class BmpTestMessage implements Notification {
92
93         private final long value;
94
95         public BmpTestMessage(final long value) {
96             this.value = value;
97         }
98
99         public long getValue() {
100             return this.value;
101         }
102
103         @Override
104         public Class<? extends DataContainer> getImplementedInterface() {
105             return BmpTestMessage.class;
106         }
107
108         @Override
109         public String toString() {
110             return "BmpTestMessage [value=" + this.value + "]";
111         }
112
113     }
114
115 }