Initial code drop
[bgpcep.git] / bgp / concepts / src / test / java / org / opendaylight / protocol / bgp / concepts / 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.concepts;
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.Community;
19 import org.opendaylight.protocol.bgp.concepts.ExtendedCommunity;
20 import org.opendaylight.protocol.bgp.concepts.OpaqueExtendedCommunity;
21 import org.opendaylight.protocol.bgp.concepts.RouteOriginCommunity;
22
23 import org.opendaylight.protocol.concepts.ASNumber;
24
25 public class CommunityTest {
26
27         @Test
28         public void testCommunity() {
29                 new Community(new ASNumber(0, 10), 222);
30                 final ASNumber as = new ASNumber(12);
31                 final Community c = new Community(as, 12);
32                 assertEquals(as, c.getAs());
33                 assertEquals(12, c.getSemantics());
34         }
35
36         @Test
37         public void testOverflows() {
38                 try {
39                         new Community(new ASNumber(0, 10), -2);
40                         fail("Semantics under range.");
41                 } catch (final IllegalArgumentException e) {
42                         assertEquals("Invalid semantics specified", e.getMessage());
43                 }
44                 try {
45                         new Community(new ASNumber(5, 10), 5);
46                         fail("AS high number cannot be null");
47                 } catch (final IllegalArgumentException e) {
48                         assertEquals("Invalid AS number specified", e.getMessage());
49                 }
50                 try {
51                         new Community(new ASNumber(0, 10), 65536);
52                         fail("Semantics above range.");
53                 } catch (final IllegalArgumentException e) {
54                         assertEquals("Invalid semantics specified", e.getMessage());
55                 }
56         }
57
58         @Test
59         public void testEquals() {
60                 final Community c1 = new Community(new ASNumber(0, 10), 222);
61                 final Community c2 = new Community(new ASNumber(0, 10), 222);
62                 assertEquals(c1, c2);
63                 assertThat(c1, not(new Object()));
64         }
65
66         @Test
67         public void testHashCode() {
68                 final Community c1 = new Community(new ASNumber(0, 10), 222);
69                 final Community c2 = new Community(new ASNumber(0, 10), 222);
70                 assertEquals(c1.hashCode(), c2.hashCode());
71         }
72
73         @Test
74         public void testToString() {
75                 final Community c = new Community(new ASNumber(0, 10), 222);
76                 assertNotNull(c.toString());
77         }
78
79         @Test
80         public void testValueOf() {
81                 final Community comm = Community.valueOf("12:50");
82                 assertEquals(comm, new Community(new ASNumber(0, 12), 50));
83         }
84
85         @Test
86         public void testExtendedCommunity() {
87                 final ExtendedCommunity ec = new OpaqueExtendedCommunity(false, 5, new byte[] { 1, 2, 3, 4, 5, 6 });
88                 final Object ec2 = new RouteOriginCommunity(new ASNumber(84), new byte[] { 1, 2, 3, 4 });
89                 assertNotSame(ec, ec2);
90                 assertEquals(ec, new OpaqueExtendedCommunity(false, 5, new byte[] { 1, 2, 3, 4, 5, 6 }));
91                 assertEquals(ec.hashCode(), (new OpaqueExtendedCommunity(false, 5, new byte[] { 1, 2, 3, 4, 5, 6 })).hashCode());
92         }
93 }