Bug 611 - BGP Update message serialization
[bgpcep.git] / bgp / parser-spi / src / test / java / org / opendaylight / protocol / bgp / parser / spi / AbstractMessageRegistryTest.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.protocol.bgp.parser.spi;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertTrue;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14
15 import org.junit.Test;
16 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
17 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
18 import org.opendaylight.protocol.util.ByteArray;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.KeepaliveBuilder;
21 import org.opendaylight.yangtools.yang.binding.Notification;
22
23 public class AbstractMessageRegistryTest {
24
25     public static final byte[] keepAliveBMsg = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
26         (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
27         (byte) 0xff, (byte) 0x00, (byte) 0x13, (byte) 0x04 };
28
29     private final AbstractMessageRegistry registry = new AbstractMessageRegistry() {
30
31         @Override
32         protected void serializeMessageImpl(final Notification message, final ByteBuf buffer) {
33             buffer.writeBytes(keepAliveBMsg);
34         }
35
36         @Override
37         protected Notification parseBody(int type, ByteBuf body, int messageLength) throws BGPDocumentedException {
38             return new KeepaliveBuilder().build();
39         }
40     };
41
42     @Test
43     public void testRegistry() throws BGPDocumentedException, BGPParsingException {
44         final Notification keepAlive = new KeepaliveBuilder().build();
45         final ByteBuf buffer = Unpooled.buffer();
46         this.registry.serializeMessage(keepAlive, buffer);
47         assertArrayEquals(keepAliveBMsg, ByteArray.getAllBytes(buffer));
48
49         final Notification not = this.registry.parseMessage(Unpooled.copiedBuffer(keepAliveBMsg));
50         assertTrue(not instanceof Keepalive);
51     }
52 }