blob: f15c0a190451beaec2f5613ddcde449d2fe6a721 [file] [log] [blame]
Ray Milkey33d90232014-11-04 10:49:00 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onlab.onos.net;
17
18import org.junit.Test;
19
20import static org.hamcrest.CoreMatchers.allOf;
21import static org.hamcrest.CoreMatchers.containsString;
22import static org.hamcrest.CoreMatchers.equalTo;
23import static org.hamcrest.CoreMatchers.is;
24import static org.hamcrest.CoreMatchers.not;
25import static org.hamcrest.MatcherAssert.assertThat;
26import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
27import static org.onlab.onos.net.DeviceId.deviceId;
28import static org.onlab.onos.net.PortNumber.portNumber;
29
30/**
31 * Unit tests for the LinkKey class.
32 */
33public class LinkKeyTest {
34
35 static final DeviceId D1 = deviceId("1");
36 static final DeviceId D2 = deviceId("2");
37 static final PortNumber P1 = portNumber(1);
38 static final PortNumber P2 = portNumber(2);
39
40 static final ConnectPoint SRC1 = new ConnectPoint(D1, P1);
41 static final ConnectPoint DST1 = new ConnectPoint(D2, P1);
42 static final ConnectPoint DST2 = new ConnectPoint(D2, P2);
43
44
45 /**
46 * Checks that the LinkKey class is immutable.
47 */
48 @Test
49 public void testLinkKeyImmutability() {
50 assertThatClassIsImmutable(LinkKey.class);
51 }
52
53 /**
54 * Check null source connection.
55 */
56 @Test(expected = NullPointerException.class)
57 public void testNullSrc() {
58 LinkKey key = LinkKey.linkKey(null, DST1);
59 }
60
61 /**
62 * Check null destination connection.
63 */
64 @Test(expected = NullPointerException.class)
65 public void testNullDst() {
66 LinkKey key = LinkKey.linkKey(SRC1, null);
67 }
68
69 /**
70 * Check that two LinkKeys based on the same source/destination pair compare
71 * equal.
72 */
73 @Test
74 public void testCompareEquals() {
75 LinkKey k1 = LinkKey.linkKey(SRC1, DST2);
76 LinkKey k2 = LinkKey.linkKey(SRC1, DST2);
77
78 assertThat(k1, is(equalTo(k2)));
79 }
80
81 /**
82 * Check that two LinkKeys based on different source/destination pairs compare
83 * not equal.
84 */
85 @Test
86 public void testCompareNotEquals() {
87 LinkKey k1 = LinkKey.linkKey(SRC1, DST1);
88 LinkKey k2 = LinkKey.linkKey(SRC1, DST2);
89
90 assertThat(k1, is(not(equalTo(k2))));
91 assertThat(k1, is(not(equalTo(new Object()))));
92 }
93
94 /**
95 * Check that two LinkKeys based on the same source/destination pair compare
96 * equal.
97 */
98 @Test
99 public void testHashCodeEquals() {
100 LinkKey k1 = LinkKey.linkKey(SRC1, DST2);
101 LinkKey k2 = LinkKey.linkKey(SRC1, DST2);
102
103 assertThat(k1.hashCode(), is(equalTo(k2.hashCode())));
104 }
105
106 /**
107 * Check that two LinkKeys based on different source/destination pairs compare
108 * not equal.
109 */
110 @Test
111 public void testHashCodeNotEquals() {
112 LinkKey k1 = LinkKey.linkKey(SRC1, DST1);
113 LinkKey k2 = LinkKey.linkKey(SRC1, DST2);
114
115 assertThat(k1.hashCode(), is(not(equalTo(k2.hashCode()))));
116 }
117
118 /**
119 * Check the toString() method of LinkKey.
120 */
121 @Test
122 public void testToString() {
123 LinkKey k1 = LinkKey.linkKey(SRC1, DST1);
124 String k1String = k1.toString();
125 assertThat(k1String, allOf(containsString("LinkKey{"),
126 containsString("src=ConnectPoint{elementId=1, portNumber=1}"),
127 containsString("dst=ConnectPoint{elementId=2, portNumber=1}")));
128 }
129}