blob: 65aa71f401e0ea5221ca2925d6ee7a2e2879b47f [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;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070014import org.onlab.onos.ApplicationId;
15import org.onlab.onos.TestApplicationId;
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070016import org.onlab.onos.net.ConnectPoint;
17import org.onlab.onos.net.DeviceId;
18import org.onlab.onos.net.Link;
19import org.onlab.onos.net.PortNumber;
20import org.onlab.onos.net.flow.TrafficSelector;
21import org.onlab.onos.net.flow.TrafficTreatment;
22
Ray Milkeye6684082014-10-16 16:59:47 -070023/**
24 * Unit tests for the LinkCollectionIntent class.
25 */
Ray Milkey0742ec92014-10-13 08:39:55 -070026public class TestLinkCollectionIntent {
27
Thomas Vachuskab97cf282014-10-20 23:31:12 -070028 private static final ApplicationId APPID = new TestApplicationId("foo");
29
Ray Milkeye6684082014-10-16 16:59:47 -070030 private Link link1 = link("dev1", 1, "dev2", 2);
31 private Link link2 = link("dev1", 1, "dev3", 2);
32 private Link link3 = link("dev2", 1, "dev3", 2);
33
34 private Set<Link> links1;
35 private Set<Link> links2;
36
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070037 private ConnectPoint egress1 = new ConnectPoint(DeviceId.deviceId("dev1"),
38 PortNumber.portNumber(3));
39 private ConnectPoint egress2 = new ConnectPoint(DeviceId.deviceId("dev2"),
40 PortNumber.portNumber(3));
41
Ray Milkeye6684082014-10-16 16:59:47 -070042 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
43 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
44
Thomas Vachuskab97cf282014-10-20 23:31:12 -070045 private LinkCollectionIntent makeLinkCollection(Set<Link> links,
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070046 ConnectPoint egress) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070047 return new LinkCollectionIntent(APPID, selector, treatment, links, egress);
Ray Milkey0742ec92014-10-13 08:39:55 -070048 }
49
Ray Milkeye6684082014-10-16 16:59:47 -070050 @Before
51 public void setup() {
52 links1 = new HashSet<>();
53 links2 = new HashSet<>();
Ray Milkey0742ec92014-10-13 08:39:55 -070054 }
55
Ray Milkeye6684082014-10-16 16:59:47 -070056 /**
57 * Tests the equals() method where two LinkCollectionIntents have references
58 * to the same Links in different orders. These should compare equal.
59 */
Ray Milkey0742ec92014-10-13 08:39:55 -070060 @Test
Ray Milkeye6684082014-10-16 16:59:47 -070061 public void testSameEquals() {
62 links1.add(link1);
63 links1.add(link2);
64 links1.add(link3);
Ray Milkey0742ec92014-10-13 08:39:55 -070065
Ray Milkeye6684082014-10-16 16:59:47 -070066 links2.add(link3);
67 links2.add(link2);
68 links2.add(link1);
69
Thomas Vachuskab97cf282014-10-20 23:31:12 -070070 LinkCollectionIntent i1 = makeLinkCollection(links1, egress1);
71 LinkCollectionIntent i2 = makeLinkCollection(links2, egress1);
Ray Milkeye6684082014-10-16 16:59:47 -070072
73 assertThat(i1, is(equalTo(i2)));
Ray Milkey0742ec92014-10-13 08:39:55 -070074 }
75
Ray Milkeye6684082014-10-16 16:59:47 -070076 /**
77 * Tests the equals() method where two LinkCollectionIntents have references
78 * to different Links. These should compare not equal.
79 */
80 @Test
81 public void testLinksDifferentEquals() {
82 links1.add(link1);
83 links1.add(link2);
84
85 links2.add(link3);
86 links2.add(link1);
87
Thomas Vachuskab97cf282014-10-20 23:31:12 -070088 LinkCollectionIntent i1 = makeLinkCollection(links1, egress1);
89 LinkCollectionIntent i2 = makeLinkCollection(links2, egress1);
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070090
91 assertThat(i1, is(not(equalTo(i2))));
92 }
93
94 /**
95 * Tests the equals() method where two LinkCollectionIntents have references
96 * to the same Links but different egress points. These should compare not equal.
97 */
98 @Test
99 public void testEgressDifferentEquals() {
100 links1.add(link1);
101 links1.add(link2);
102 links1.add(link3);
103
104 links2.add(link3);
105 links2.add(link2);
106 links2.add(link1);
107
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700108 LinkCollectionIntent i1 = makeLinkCollection(links1, egress1);
109 LinkCollectionIntent i2 = makeLinkCollection(links2, egress2);
Ray Milkeye6684082014-10-16 16:59:47 -0700110
111 assertThat(i1, is(not(equalTo(i2))));
112 }
113
114 /**
115 * Tests that the hashCode() values for two equivalent LinkCollectionIntent
116 * objects are the same.
117 */
118 @Test
119 public void testHashCodeEquals() {
120 links1.add(link1);
121 links1.add(link2);
122 links1.add(link3);
123
124 links2.add(link3);
125 links2.add(link2);
126 links2.add(link1);
127
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700128 LinkCollectionIntent i1 = makeLinkCollection(links1, egress1);
129 LinkCollectionIntent i2 = makeLinkCollection(links2, egress1);
Ray Milkeye6684082014-10-16 16:59:47 -0700130
131 assertThat(i1.hashCode(), is(equalTo(i2.hashCode())));
132 }
133
134 /**
135 * Tests that the hashCode() values for two distinct LinkCollectionIntent
136 * objects are different.
137 */
138 @Test
139 public void testHashCodeDifferent() {
140 links1.add(link1);
141 links1.add(link2);
142
143 links2.add(link1);
144 links2.add(link3);
145
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700146 LinkCollectionIntent i1 = makeLinkCollection(links1, egress1);
147 LinkCollectionIntent i2 = makeLinkCollection(links2, egress2);
Ray Milkeye6684082014-10-16 16:59:47 -0700148
149 assertThat(i1.hashCode(), is(not(equalTo(i2.hashCode()))));
150 }
151
152 /**
153 * Checks that the HostToHostIntent class is immutable.
154 */
155 @Test
156 public void checkImmutability() {
157 ImmutableClassChecker.assertThatClassIsImmutable(LinkCollectionIntent.class);
158 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700159}