blob: 42b5b285a416e5170f34e428a1a0073a6bd50983 [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;
Pavlin Radoslavovd26f57a2014-10-23 17:19:45 -07007import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
Ray Milkeye6684082014-10-16 16:59:47 -07008import static org.onlab.onos.net.NetTestTools.link;
Ray Milkey0742ec92014-10-13 08:39:55 -07009
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070010import java.util.HashSet;
11import java.util.Set;
12
13import org.junit.Before;
14import org.junit.Test;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070015import org.onlab.onos.ApplicationId;
16import org.onlab.onos.TestApplicationId;
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070017import org.onlab.onos.net.ConnectPoint;
18import org.onlab.onos.net.DeviceId;
19import org.onlab.onos.net.Link;
20import org.onlab.onos.net.PortNumber;
21import org.onlab.onos.net.flow.TrafficSelector;
22import org.onlab.onos.net.flow.TrafficTreatment;
23
Ray Milkeye6684082014-10-16 16:59:47 -070024/**
25 * Unit tests for the LinkCollectionIntent class.
26 */
Ray Milkey0742ec92014-10-13 08:39:55 -070027public class TestLinkCollectionIntent {
28
Thomas Vachuskab97cf282014-10-20 23:31:12 -070029 private static final ApplicationId APPID = new TestApplicationId("foo");
30
Ray Milkeye6684082014-10-16 16:59:47 -070031 private Link link1 = link("dev1", 1, "dev2", 2);
32 private Link link2 = link("dev1", 1, "dev3", 2);
33 private Link link3 = link("dev2", 1, "dev3", 2);
34
35 private Set<Link> links1;
36 private Set<Link> links2;
37
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070038 private ConnectPoint egress1 = new ConnectPoint(DeviceId.deviceId("dev1"),
39 PortNumber.portNumber(3));
40 private ConnectPoint egress2 = new ConnectPoint(DeviceId.deviceId("dev2"),
41 PortNumber.portNumber(3));
42
Ray Milkeye6684082014-10-16 16:59:47 -070043 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
44 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
45
Thomas Vachuskab97cf282014-10-20 23:31:12 -070046 private LinkCollectionIntent makeLinkCollection(Set<Link> links,
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070047 ConnectPoint egress) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070048 return new LinkCollectionIntent(APPID, selector, treatment, links, egress);
Ray Milkey0742ec92014-10-13 08:39:55 -070049 }
50
Ray Milkeye6684082014-10-16 16:59:47 -070051 @Before
52 public void setup() {
53 links1 = new HashSet<>();
54 links2 = new HashSet<>();
Ray Milkey0742ec92014-10-13 08:39:55 -070055 }
56
Ray Milkeye6684082014-10-16 16:59:47 -070057 /**
58 * Tests the equals() method where two LinkCollectionIntents have references
59 * to the same Links in different orders. These should compare equal.
60 */
Ray Milkey0742ec92014-10-13 08:39:55 -070061 @Test
Ray Milkeye6684082014-10-16 16:59:47 -070062 public void testSameEquals() {
63 links1.add(link1);
64 links1.add(link2);
65 links1.add(link3);
Ray Milkey0742ec92014-10-13 08:39:55 -070066
Ray Milkeye6684082014-10-16 16:59:47 -070067 links2.add(link3);
68 links2.add(link2);
69 links2.add(link1);
70
Thomas Vachuskab97cf282014-10-20 23:31:12 -070071 LinkCollectionIntent i1 = makeLinkCollection(links1, egress1);
72 LinkCollectionIntent i2 = makeLinkCollection(links2, egress1);
Ray Milkeye6684082014-10-16 16:59:47 -070073
74 assertThat(i1, is(equalTo(i2)));
Ray Milkey0742ec92014-10-13 08:39:55 -070075 }
76
Ray Milkeye6684082014-10-16 16:59:47 -070077 /**
78 * Tests the equals() method where two LinkCollectionIntents have references
79 * to different Links. These should compare not equal.
80 */
81 @Test
82 public void testLinksDifferentEquals() {
83 links1.add(link1);
84 links1.add(link2);
85
86 links2.add(link3);
87 links2.add(link1);
88
Thomas Vachuskab97cf282014-10-20 23:31:12 -070089 LinkCollectionIntent i1 = makeLinkCollection(links1, egress1);
90 LinkCollectionIntent i2 = makeLinkCollection(links2, egress1);
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070091
92 assertThat(i1, is(not(equalTo(i2))));
93 }
94
95 /**
96 * Tests the equals() method where two LinkCollectionIntents have references
97 * to the same Links but different egress points. These should compare not equal.
98 */
99 @Test
100 public void testEgressDifferentEquals() {
101 links1.add(link1);
102 links1.add(link2);
103 links1.add(link3);
104
105 links2.add(link3);
106 links2.add(link2);
107 links2.add(link1);
108
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700109 LinkCollectionIntent i1 = makeLinkCollection(links1, egress1);
110 LinkCollectionIntent i2 = makeLinkCollection(links2, egress2);
Ray Milkeye6684082014-10-16 16:59:47 -0700111
112 assertThat(i1, is(not(equalTo(i2))));
113 }
114
115 /**
116 * Tests that the hashCode() values for two equivalent LinkCollectionIntent
117 * objects are the same.
118 */
119 @Test
120 public void testHashCodeEquals() {
121 links1.add(link1);
122 links1.add(link2);
123 links1.add(link3);
124
125 links2.add(link3);
126 links2.add(link2);
127 links2.add(link1);
128
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700129 LinkCollectionIntent i1 = makeLinkCollection(links1, egress1);
130 LinkCollectionIntent i2 = makeLinkCollection(links2, egress1);
Ray Milkeye6684082014-10-16 16:59:47 -0700131
132 assertThat(i1.hashCode(), is(equalTo(i2.hashCode())));
133 }
134
135 /**
136 * Tests that the hashCode() values for two distinct LinkCollectionIntent
137 * objects are different.
138 */
139 @Test
140 public void testHashCodeDifferent() {
141 links1.add(link1);
142 links1.add(link2);
143
144 links2.add(link1);
145 links2.add(link3);
146
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700147 LinkCollectionIntent i1 = makeLinkCollection(links1, egress1);
148 LinkCollectionIntent i2 = makeLinkCollection(links2, egress2);
Ray Milkeye6684082014-10-16 16:59:47 -0700149
150 assertThat(i1.hashCode(), is(not(equalTo(i2.hashCode()))));
151 }
152
153 /**
154 * Checks that the HostToHostIntent class is immutable.
155 */
156 @Test
157 public void checkImmutability() {
Pavlin Radoslavovd26f57a2014-10-23 17:19:45 -0700158 assertThatClassIsImmutable(LinkCollectionIntent.class);
Ray Milkeye6684082014-10-16 16:59:47 -0700159 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700160}