blob: a7082b4379cb76f2596ce469c613ecb903187798 [file] [log] [blame]
Ray Milkey0742ec92014-10-13 08:39:55 -07001package org.onlab.onos.net.intent;
2
Ray Milkeye6684082014-10-16 16:59:47 -07003import static org.hamcrest.CoreMatchers.not;
Ray Milkey0742ec92014-10-13 08:39:55 -07004import static org.hamcrest.MatcherAssert.assertThat;
Ray Milkeye6684082014-10-16 16:59:47 -07005import static org.hamcrest.Matchers.equalTo;
Ray Milkey0742ec92014-10-13 08:39:55 -07006import static org.hamcrest.Matchers.is;
Ray Milkeye6684082014-10-16 16:59:47 -07007import static org.onlab.onos.net.NetTestTools.link;
Ray Milkey0742ec92014-10-13 08:39:55 -07008
Jonathan Hart6b2ffc32014-10-18 02:09:22 -07009import java.util.HashSet;
10import java.util.Set;
11
12import org.junit.Before;
13import org.junit.Test;
14import org.onlab.onos.net.ConnectPoint;
15import org.onlab.onos.net.DeviceId;
16import org.onlab.onos.net.Link;
17import org.onlab.onos.net.PortNumber;
18import org.onlab.onos.net.flow.TrafficSelector;
19import org.onlab.onos.net.flow.TrafficTreatment;
20
Ray Milkeye6684082014-10-16 16:59:47 -070021/**
22 * Unit tests for the LinkCollectionIntent class.
23 */
Ray Milkey0742ec92014-10-13 08:39:55 -070024public class TestLinkCollectionIntent {
25
Ray Milkeye6684082014-10-16 16:59:47 -070026 private Link link1 = link("dev1", 1, "dev2", 2);
27 private Link link2 = link("dev1", 1, "dev3", 2);
28 private Link link3 = link("dev2", 1, "dev3", 2);
29
30 private Set<Link> links1;
31 private Set<Link> links2;
32
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070033 private ConnectPoint egress1 = new ConnectPoint(DeviceId.deviceId("dev1"),
34 PortNumber.portNumber(3));
35 private ConnectPoint egress2 = new ConnectPoint(DeviceId.deviceId("dev2"),
36 PortNumber.portNumber(3));
37
Ray Milkeye6684082014-10-16 16:59:47 -070038 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
39 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
40
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070041 private LinkCollectionIntent makeLinkCollection(long id, Set<Link> links,
42 ConnectPoint egress) {
Ray Milkeye6684082014-10-16 16:59:47 -070043 return new LinkCollectionIntent(new IntentId(id),
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070044 selector, treatment, links, egress);
Ray Milkey0742ec92014-10-13 08:39:55 -070045 }
46
Ray Milkeye6684082014-10-16 16:59:47 -070047 @Before
48 public void setup() {
49 links1 = new HashSet<>();
50 links2 = new HashSet<>();
Ray Milkey0742ec92014-10-13 08:39:55 -070051 }
52
Ray Milkeye6684082014-10-16 16:59:47 -070053 /**
54 * Tests the equals() method where two LinkCollectionIntents have references
55 * to the same Links in different orders. These should compare equal.
56 */
Ray Milkey0742ec92014-10-13 08:39:55 -070057 @Test
Ray Milkeye6684082014-10-16 16:59:47 -070058 public void testSameEquals() {
59 links1.add(link1);
60 links1.add(link2);
61 links1.add(link3);
Ray Milkey0742ec92014-10-13 08:39:55 -070062
Ray Milkeye6684082014-10-16 16:59:47 -070063 links2.add(link3);
64 links2.add(link2);
65 links2.add(link1);
66
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070067 LinkCollectionIntent i1 = makeLinkCollection(12, links1, egress1);
68 LinkCollectionIntent i2 = makeLinkCollection(12, links2, egress1);
Ray Milkeye6684082014-10-16 16:59:47 -070069
70 assertThat(i1, is(equalTo(i2)));
Ray Milkey0742ec92014-10-13 08:39:55 -070071 }
72
Ray Milkeye6684082014-10-16 16:59:47 -070073 /**
74 * Tests the equals() method where two LinkCollectionIntents have references
75 * to different Links. These should compare not equal.
76 */
77 @Test
78 public void testLinksDifferentEquals() {
79 links1.add(link1);
80 links1.add(link2);
81
82 links2.add(link3);
83 links2.add(link1);
84
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070085 LinkCollectionIntent i1 = makeLinkCollection(12, links1, egress1);
86 LinkCollectionIntent i2 = makeLinkCollection(12, links2, egress1);
87
88 assertThat(i1, is(not(equalTo(i2))));
89 }
90
91 /**
92 * Tests the equals() method where two LinkCollectionIntents have references
93 * to the same Links but different egress points. These should compare not equal.
94 */
95 @Test
96 public void testEgressDifferentEquals() {
97 links1.add(link1);
98 links1.add(link2);
99 links1.add(link3);
100
101 links2.add(link3);
102 links2.add(link2);
103 links2.add(link1);
104
105 LinkCollectionIntent i1 = makeLinkCollection(12, links1, egress1);
106 LinkCollectionIntent i2 = makeLinkCollection(12, links2, egress2);
Ray Milkeye6684082014-10-16 16:59:47 -0700107
108 assertThat(i1, is(not(equalTo(i2))));
109 }
110
111 /**
112 * Tests the equals() method where two LinkCollectionIntents have different
113 * ids. These should compare not equal.
114 */
115 @Test
116 public void testBaseDifferentEquals() {
117 links1.add(link1);
118 links1.add(link2);
119
120 links2.add(link2);
121 links2.add(link1);
122
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700123 LinkCollectionIntent i1 = makeLinkCollection(1, links1, egress1);
124 LinkCollectionIntent i2 = makeLinkCollection(2, links2, egress1);
Ray Milkeye6684082014-10-16 16:59:47 -0700125
126 assertThat(i1, is(not(equalTo(i2))));
127 }
128
129 /**
130 * Tests that the hashCode() values for two equivalent LinkCollectionIntent
131 * objects are the same.
132 */
133 @Test
134 public void testHashCodeEquals() {
135 links1.add(link1);
136 links1.add(link2);
137 links1.add(link3);
138
139 links2.add(link3);
140 links2.add(link2);
141 links2.add(link1);
142
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700143 LinkCollectionIntent i1 = makeLinkCollection(1, links1, egress1);
144 LinkCollectionIntent i2 = makeLinkCollection(1, links2, egress1);
Ray Milkeye6684082014-10-16 16:59:47 -0700145
146 assertThat(i1.hashCode(), is(equalTo(i2.hashCode())));
147 }
148
149 /**
150 * Tests that the hashCode() values for two distinct LinkCollectionIntent
151 * objects are different.
152 */
153 @Test
154 public void testHashCodeDifferent() {
155 links1.add(link1);
156 links1.add(link2);
157
158 links2.add(link1);
159 links2.add(link3);
160
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700161 LinkCollectionIntent i1 = makeLinkCollection(1, links1, egress1);
162 LinkCollectionIntent i2 = makeLinkCollection(1, links2, egress2);
Ray Milkeye6684082014-10-16 16:59:47 -0700163
164 assertThat(i1.hashCode(), is(not(equalTo(i2.hashCode()))));
165 }
166
167 /**
168 * Checks that the HostToHostIntent class is immutable.
169 */
170 @Test
171 public void checkImmutability() {
172 ImmutableClassChecker.assertThatClassIsImmutable(LinkCollectionIntent.class);
173 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700174}