2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.concepts;
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;
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;
23 import org.opendaylight.protocol.concepts.ASNumber;
25 public class CommunityTest {
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());
37 public void testOverflows() {
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());
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());
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());
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);
63 assertThat(c1, not(new Object()));
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());
74 public void testToString() {
75 final Community c = new Community(new ASNumber(0, 10), 222);
76 assertNotNull(c.toString());
80 public void testValueOf() {
81 final Community comm = Community.valueOf("12:50");
82 assertEquals(comm, new Community(new ASNumber(0, 12), 50));
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());