blob: f781cf35be8fb916709304ef9facb83d8b760f52 [file] [log] [blame]
Ray Milkey0742ec92014-10-13 08:39:55 -07001package org.onlab.onos.net.intent;
2
Ray Milkey0742ec92014-10-13 08:39:55 -07003import java.util.HashSet;
Ray Milkey0742ec92014-10-13 08:39:55 -07004import java.util.Set;
5
Ray Milkeye6684082014-10-16 16:59:47 -07006import org.junit.Before;
Ray Milkey0742ec92014-10-13 08:39:55 -07007import org.junit.Test;
8import org.onlab.onos.net.Link;
9import org.onlab.onos.net.flow.TrafficSelector;
10import org.onlab.onos.net.flow.TrafficTreatment;
Ray Milkey0742ec92014-10-13 08:39:55 -070011
Ray Milkeye6684082014-10-16 16:59:47 -070012import static org.hamcrest.CoreMatchers.not;
Ray Milkey0742ec92014-10-13 08:39:55 -070013import static org.hamcrest.MatcherAssert.assertThat;
Ray Milkeye6684082014-10-16 16:59:47 -070014import static org.hamcrest.Matchers.equalTo;
Ray Milkey0742ec92014-10-13 08:39:55 -070015import static org.hamcrest.Matchers.is;
Ray Milkeye6684082014-10-16 16:59:47 -070016import static org.onlab.onos.net.NetTestTools.link;
Ray Milkey0742ec92014-10-13 08:39:55 -070017
Ray Milkeye6684082014-10-16 16:59:47 -070018/**
19 * Unit tests for the LinkCollectionIntent class.
20 */
Ray Milkey0742ec92014-10-13 08:39:55 -070021public class TestLinkCollectionIntent {
22
Ray Milkeye6684082014-10-16 16:59:47 -070023 private Link link1 = link("dev1", 1, "dev2", 2);
24 private Link link2 = link("dev1", 1, "dev3", 2);
25 private Link link3 = link("dev2", 1, "dev3", 2);
26
27 private Set<Link> links1;
28 private Set<Link> links2;
29
30 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
31 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
32
33 private LinkCollectionIntent makeLinkCollection(long id, Set<Link> links) {
34 return new LinkCollectionIntent(new IntentId(id),
35 selector, treatment, links);
Ray Milkey0742ec92014-10-13 08:39:55 -070036 }
37
Ray Milkeye6684082014-10-16 16:59:47 -070038 @Before
39 public void setup() {
40 links1 = new HashSet<>();
41 links2 = new HashSet<>();
Ray Milkey0742ec92014-10-13 08:39:55 -070042 }
43
Ray Milkeye6684082014-10-16 16:59:47 -070044 /**
45 * Tests the equals() method where two LinkCollectionIntents have references
46 * to the same Links in different orders. These should compare equal.
47 */
Ray Milkey0742ec92014-10-13 08:39:55 -070048 @Test
Ray Milkeye6684082014-10-16 16:59:47 -070049 public void testSameEquals() {
50 links1.add(link1);
51 links1.add(link2);
52 links1.add(link3);
Ray Milkey0742ec92014-10-13 08:39:55 -070053
Ray Milkeye6684082014-10-16 16:59:47 -070054 links2.add(link3);
55 links2.add(link2);
56 links2.add(link1);
57
58 LinkCollectionIntent i1 = makeLinkCollection(12, links1);
59 LinkCollectionIntent i2 = makeLinkCollection(12, links2);
60
61 assertThat(i1, is(equalTo(i2)));
Ray Milkey0742ec92014-10-13 08:39:55 -070062 }
63
Ray Milkeye6684082014-10-16 16:59:47 -070064 /**
65 * Tests the equals() method where two LinkCollectionIntents have references
66 * to different Links. These should compare not equal.
67 */
68 @Test
69 public void testLinksDifferentEquals() {
70 links1.add(link1);
71 links1.add(link2);
72
73 links2.add(link3);
74 links2.add(link1);
75
76 LinkCollectionIntent i1 = makeLinkCollection(12, links1);
77 LinkCollectionIntent i2 = makeLinkCollection(12, links2);
78
79 assertThat(i1, is(not(equalTo(i2))));
80 }
81
82 /**
83 * Tests the equals() method where two LinkCollectionIntents have different
84 * ids. These should compare not equal.
85 */
86 @Test
87 public void testBaseDifferentEquals() {
88 links1.add(link1);
89 links1.add(link2);
90
91 links2.add(link2);
92 links2.add(link1);
93
94 LinkCollectionIntent i1 = makeLinkCollection(1, links1);
95 LinkCollectionIntent i2 = makeLinkCollection(2, links2);
96
97 assertThat(i1, is(not(equalTo(i2))));
98 }
99
100 /**
101 * Tests that the hashCode() values for two equivalent LinkCollectionIntent
102 * objects are the same.
103 */
104 @Test
105 public void testHashCodeEquals() {
106 links1.add(link1);
107 links1.add(link2);
108 links1.add(link3);
109
110 links2.add(link3);
111 links2.add(link2);
112 links2.add(link1);
113
114 LinkCollectionIntent i1 = makeLinkCollection(1, links1);
115 LinkCollectionIntent i2 = makeLinkCollection(1, links2);
116
117 assertThat(i1.hashCode(), is(equalTo(i2.hashCode())));
118 }
119
120 /**
121 * Tests that the hashCode() values for two distinct LinkCollectionIntent
122 * objects are different.
123 */
124 @Test
125 public void testHashCodeDifferent() {
126 links1.add(link1);
127 links1.add(link2);
128
129 links2.add(link1);
130 links2.add(link3);
131
132 LinkCollectionIntent i1 = makeLinkCollection(1, links1);
133 LinkCollectionIntent i2 = makeLinkCollection(1, links2);
134
135 assertThat(i1.hashCode(), is(not(equalTo(i2.hashCode()))));
136 }
137
138 /**
139 * Checks that the HostToHostIntent class is immutable.
140 */
141 @Test
142 public void checkImmutability() {
143 ImmutableClassChecker.assertThatClassIsImmutable(LinkCollectionIntent.class);
144 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700145}