BUG-9141: Fix Stateful07TopologySessionListener failing test
[bgpcep.git] / bgp / bmp-spi / src / test / java / org / opendaylight / protocol / bmp / spi / parser / PeerDistinguisherUtilTest.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.parser;
10
11 import static org.junit.Assert.assertEquals;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.lang.reflect.Constructor;
15 import java.lang.reflect.InvocationTargetException;
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.DistinguisherType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.peer.DistinguisherBuilder;
20
21 public class PeerDistinguisherUtilTest {
22
23     private static final byte[] DISTINGUISHER_TYPE0 = { 0x00, 0x00, 0x30, 0x39, 0x00, 0x09, (byte)0xfb, (byte)0xf1};
24     private static final byte[] DISTINGUISHER_TYPE1 = { 0x00, 0x01, 0x7f, (byte)0x9c, (byte)0x97, 0x0b, (byte)0xd4, 0x31};
25     private static final byte[] DISTINGUISHER_TYPE2 = { 0x00, 0x02, 0x00, 0x01, (byte)0xe2, 0x40, (byte)0xd4, 0x31};
26
27     private static final int ASN = 12345;
28     private static final int ASN_FOUR_BYTES = 123456;
29     private static final int ISP_NUMBER = 54321;
30     private static final int ISP_NUMBER_FOUR_BYTES = 654321;
31     private static final String IPV4_ADDRESS = "127.156.151.11";
32
33     @Test
34     public void testPeerDistingisherType0() {
35         final DistinguisherBuilder dBuilder = new DistinguisherBuilder();
36         dBuilder.setDistinguisherType(DistinguisherType.Type0);
37         dBuilder.setDistinguisher(ASN + ":" + ISP_NUMBER_FOUR_BYTES);
38
39         assertEquals(dBuilder.build(), PeerDistinguisherUtil.parsePeerDistingisher(Unpooled.wrappedBuffer(DISTINGUISHER_TYPE0)));
40
41         final ByteBuf buffer = Unpooled.buffer(DISTINGUISHER_TYPE0.length);
42         PeerDistinguisherUtil.serializePeerDistinguisher(dBuilder.build(), buffer);
43         Assert.assertArrayEquals(DISTINGUISHER_TYPE0, buffer.array());
44     }
45
46     @Test
47     public void testPeerDistingisherType1() {
48         final DistinguisherBuilder dBuilder = new DistinguisherBuilder();
49         dBuilder.setDistinguisherType(DistinguisherType.Type1);
50         dBuilder.setDistinguisher(IPV4_ADDRESS + ":" + ISP_NUMBER);
51
52         assertEquals(dBuilder.build(), PeerDistinguisherUtil.parsePeerDistingisher(Unpooled.wrappedBuffer(DISTINGUISHER_TYPE1)));
53
54         final ByteBuf buffer = Unpooled.buffer(DISTINGUISHER_TYPE1.length);
55         PeerDistinguisherUtil.serializePeerDistinguisher(dBuilder.build(), buffer);
56         Assert.assertArrayEquals(DISTINGUISHER_TYPE1, buffer.array());
57     }
58
59     @Test
60     public void testPeerDistingisherType2() {
61         final DistinguisherBuilder dBuilder = new DistinguisherBuilder();
62         dBuilder.setDistinguisherType(DistinguisherType.Type2);
63         dBuilder.setDistinguisher(ASN_FOUR_BYTES + ":" + ISP_NUMBER);
64
65         assertEquals(dBuilder.build(), PeerDistinguisherUtil.parsePeerDistingisher(Unpooled.wrappedBuffer(DISTINGUISHER_TYPE2)));
66
67         final ByteBuf buffer = Unpooled.buffer(DISTINGUISHER_TYPE2.length);
68         PeerDistinguisherUtil.serializePeerDistinguisher(dBuilder.build(), buffer);
69         Assert.assertArrayEquals(DISTINGUISHER_TYPE2, buffer.array());
70     }
71
72     @Test(expected=UnsupportedOperationException.class)
73     public void testPeerDistinguisherUtilPrivateConstructor() throws Throwable {
74         final Constructor<PeerDistinguisherUtil> c = PeerDistinguisherUtil.class.getDeclaredConstructor();
75         c.setAccessible(true);
76         try {
77             c.newInstance();
78         } catch (final InvocationTargetException e) {
79             throw e.getCause();
80         }
81     }
82 }