Fix most bgp-parser-spi checkstyle violations
[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.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
20 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
21 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Keepalive;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.KeepaliveBuilder;
25 import org.opendaylight.yangtools.yang.binding.Notification;
26
27 public class AbstractMessageRegistryTest {
28
29     public static final byte[] KEEPALIVE_BMSG = new byte[] {
30         (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
31         (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
32         (byte) 0x00, (byte) 0x13, (byte) 0x04
33     };
34
35     private final AbstractMessageRegistry registry = new AbstractMessageRegistry() {
36
37         @Override
38         protected void serializeMessageImpl(final Notification message, final ByteBuf buffer) {
39             buffer.writeBytes(KEEPALIVE_BMSG);
40         }
41
42         @Override
43         protected Notification parseBody(final int type, final ByteBuf body, final int messageLength,
44                 final PeerSpecificParserConstraint constraint) throws BGPDocumentedException {
45             return new KeepaliveBuilder().build();
46         }
47     };
48
49     @Test
50     public void testRegistry() throws BGPDocumentedException, BGPParsingException {
51         final Notification keepAlive = new KeepaliveBuilder().build();
52         final ByteBuf buffer = Unpooled.buffer();
53         this.registry.serializeMessage(keepAlive, buffer);
54         assertArrayEquals(KEEPALIVE_BMSG, ByteArray.getAllBytes(buffer));
55
56         final Notification not = this.registry.parseMessage(Unpooled.copiedBuffer(KEEPALIVE_BMSG), null);
57         assertTrue(not instanceof Keepalive);
58     }
59
60     @Test
61     public void testIncompleteMarker() {
62         final byte[] testBytes = new byte[] {
63             (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
64             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
65             (byte) 0x00, (byte) 0x13, (byte) 0x04
66         };
67         try {
68             this.registry.parseMessage(Unpooled.copiedBuffer(testBytes), null);
69             Assert.fail();
70         } catch (BGPDocumentedException | BGPParsingException e) {
71             assertTrue(e instanceof BGPDocumentedException);
72             Assert.assertEquals("Marker not set to ones.", e.getMessage());
73         }
74     }
75
76     @Test
77     public void testInvalidLength() {
78         final byte[] testBytes = new byte[] {
79             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
80             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
81             (byte) 0x00, (byte) 0x12, (byte) 0x04
82         };
83         try {
84             this.registry.parseMessage(Unpooled.copiedBuffer(testBytes), null);
85             Assert.fail();
86         } catch (BGPDocumentedException | BGPParsingException e) {
87             assertTrue(e instanceof BGPDocumentedException);
88             Assert.assertEquals("Message length field not within valid range.", e.getMessage());
89         }
90     }
91
92     @Test
93     public void testInvalidSpecifiedSize() {
94         final byte[] testBytes = new byte[] {
95             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
96             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
97             (byte) 0x00, (byte) 0x13, (byte) 0x04, (byte) 0x04
98         };
99         try {
100             this.registry.parseMessage(Unpooled.copiedBuffer(testBytes), null);
101             Assert.fail();
102         } catch (BGPDocumentedException | BGPParsingException e) {
103             assertTrue(e instanceof BGPParsingException);
104             Assert.assertTrue(e.getMessage().startsWith("Size doesn't match size specified in header."));
105         }
106     }
107
108     @Test
109     public void testBGPHeaderParser() throws Exception {
110         final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
111                 .getMessageRegistry();
112         try {
113             msgReg.parseMessage(Unpooled.copiedBuffer(new byte[] { (byte) 0, (byte) 0 }), null);
114             fail("Exception should have occured.");
115         } catch (final IllegalArgumentException e) {
116             assertEquals("Too few bytes in passed array. Passed: 2. Expected: >= 19.", e.getMessage());
117         }
118     }
119
120     @Test
121     public void testMessageParser() throws Exception {
122         final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
123                 .getMessageRegistry();
124         String ex = "";
125         try {
126             msgReg.serializeMessage(null, Unpooled.EMPTY_BUFFER);
127         } catch (final NullPointerException e) {
128             ex = e.getMessage();
129         }
130         assertEquals("BGPMessage is mandatory.", ex);
131     }
132 }