ad46f6eb99ed412e548524e79d13255f6de5dddb
[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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.KeepaliveBuilder;
20 import org.opendaylight.yangtools.yang.binding.Notification;
21
22 public class AbstractMessageRegistryTest {
23
24     public static final byte[] keepAliveBMsg = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
25         (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
26         (byte) 0x00, (byte) 0x13, (byte) 0x04 };
27
28     private final AbstractMessageRegistry registry = new AbstractMessageRegistry() {
29
30         @Override
31         protected byte[] serializeMessageImpl(Notification message) {
32             return keepAliveBMsg;
33         }
34
35         @Override
36         protected Notification parseBody(int type, ByteBuf body, int messageLength) throws BGPDocumentedException {
37             return new KeepaliveBuilder().build();
38         }
39     };
40
41     @Test
42     public void testRegistry() throws BGPDocumentedException, BGPParsingException {
43         final Notification keepAlive = new KeepaliveBuilder().build();
44         final byte[] serialized = this.registry.serializeMessage(keepAlive);
45         assertArrayEquals(keepAliveBMsg, serialized);
46
47         final Notification not = this.registry.parseMessage(Unpooled.copiedBuffer(keepAliveBMsg));
48         assertTrue(not instanceof Keepalive);
49     }
50 }