BUG-45 : migrated Community path attribute to generated code.
[bgpcep.git] / bgp / parser-impl / src / test / java / org / opendaylight / protocol / bgp / parser / impl / CommunityTest.java
1 /*
2  * Copyright (c) 2013 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.impl;
9
10 import static org.hamcrest.core.IsNot.not;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNotSame;
14 import static org.junit.Assert.assertThat;
15 import static org.junit.Assert.fail;
16
17 import org.junit.Test;
18 import org.opendaylight.protocol.bgp.concepts.ExtendedCommunity;
19 import org.opendaylight.protocol.bgp.concepts.OpaqueExtendedCommunity;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Community;
22
23 public class CommunityTest {
24
25         @Test
26         public void testCommunity() {
27                 CommunityUtil.create(10, 222);
28                 final Community c = CommunityUtil.create(12, 12);
29                 assertEquals(12, c.getAsNumber().getValue().intValue());
30                 assertEquals(12, c.getSemantics().intValue());
31         }
32
33         @Test
34         public void testOverflows() {
35                 try {
36                         CommunityUtil.create(10, -2);
37                         fail("Semantics under range.");
38                 } catch (final IllegalArgumentException e) {
39                         assertEquals("Invalid semantics specified", e.getMessage());
40                 }
41                 try {
42                         CommunityUtil.create(10, 65536);
43                         fail("Semantics above range.");
44                 } catch (final IllegalArgumentException e) {
45                         assertEquals("Invalid semantics specified", e.getMessage());
46                 }
47         }
48
49         @Test
50         public void testEquals() {
51                 final Community c1 = CommunityUtil.create(10, 222);
52                 final Community c2 = CommunityUtil.create(10, 222);
53                 assertEquals(c1, c2);
54                 assertThat(c1, not(new Object()));
55         }
56
57         @Test
58         public void testHashCode() {
59                 final Community c1 = CommunityUtil.create(10, 222);
60                 final Community c2 = CommunityUtil.create(10, 222);
61                 assertEquals(c1.hashCode(), c2.hashCode());
62         }
63
64         @Test
65         public void testToString() {
66                 final Community c = CommunityUtil.create(10, 222);
67                 assertNotNull(c.toString());
68         }
69
70         @Test
71         public void testValueOf() {
72                 final Community comm = CommunityUtil.valueOf("12:50");
73                 assertEquals(comm, CommunityUtil.create(12, 50));
74         }
75
76         @Test
77         public void testExtendedCommunity() {
78                 final ExtendedCommunity ec = new OpaqueExtendedCommunity(false, 5, new byte[] { 1, 2, 3, 4, 5, 6 });
79                 final Object ec2 = new RouteOriginCommunity(new AsNumber((long) 84), new byte[] { 1, 2, 3, 4 });
80                 assertNotSame(ec, ec2);
81                 assertEquals(ec, new OpaqueExtendedCommunity(false, 5, new byte[] { 1, 2, 3, 4, 5, 6 }));
82                 assertEquals(ec.hashCode(), (new OpaqueExtendedCommunity(false, 5, new byte[] { 1, 2, 3, 4, 5, 6 })).hashCode());
83         }
84 }