06ca1c8208e015503fbc90f84cc56d1dfec57501
[bgpcep.git] / bgp / parser-spi / src / test / java / org / opendaylight / protocol / bgp / parser / spi / pojo / UnrecognizedAttributesTest.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.bgp.parser.spi.pojo;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertTrue;
14
15 import io.netty.buffer.Unpooled;
16 import java.util.List;
17 import org.junit.Rule;
18 import org.junit.Test;
19 import org.junit.rules.ExpectedException;
20 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
21 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.UnrecognizedAttributes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.UnrecognizedAttributesKey;
25
26 public class UnrecognizedAttributesTest {
27
28     private static final int UNRECOGNIZED_ATTRIBUTE_COUNT = 1;
29     private static final int FIRST_ATTRIBUTE = 0;
30     private static final short NON_EXISTENT_TYPE = 0;
31     private static final int NON_VALUE_BYTES = 3;
32
33     private static final SimpleAttributeRegistry simpleAttrReg = new SimpleAttributeRegistry();
34
35     @Rule
36     public ExpectedException expException = ExpectedException.none();
37
38     @Test
39     public void testUnrecognizedAttributesWithoutOptionalFlag() throws BGPDocumentedException, BGPParsingException {
40         this.expException.expect(BGPDocumentedException.class);
41         this.expException.expectMessage("Well known attribute not recognized.");
42         simpleAttrReg.parseAttributes(
43             Unpooled.wrappedBuffer(new byte[] { 0x03, 0x00, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05 }), null);
44     }
45
46     @Test
47     public void testUnrecognizedAttributes() throws BGPDocumentedException, BGPParsingException {
48         final byte[] attributeBytes = { (byte)0xe0, 0x00, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05 };
49         final List<UnrecognizedAttributes> unrecogAttribs = simpleAttrReg
50             .parseAttributes(Unpooled.wrappedBuffer(attributeBytes), null).getUnrecognizedAttributes();
51         assertEquals(UNRECOGNIZED_ATTRIBUTE_COUNT, unrecogAttribs.size());
52         final UnrecognizedAttributes unrecogAttrib = unrecogAttribs.get(FIRST_ATTRIBUTE);
53         final UnrecognizedAttributesKey expectedAttribKey =
54             new UnrecognizedAttributesKey(unrecogAttrib.getType().shortValue());
55
56         assertTrue(unrecogAttrib.isPartial());
57         assertTrue(unrecogAttrib.isTransitive());
58         assertArrayEquals(ByteArray.cutBytes(attributeBytes, NON_VALUE_BYTES), unrecogAttrib.getValue());
59         assertEquals(NON_EXISTENT_TYPE, unrecogAttrib.getType().shortValue());
60         assertEquals(expectedAttribKey, unrecogAttrib.getKey());
61     }
62 }