blob: 7cc7398d181b4456f918b94271a4eca79e9bc980 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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 */
Ray Milkey0742ec92014-10-13 08:39:55 -070016package org.onlab.onos.net.intent;
17
Ray Milkeye6684082014-10-16 16:59:47 -070018import static org.hamcrest.CoreMatchers.not;
Ray Milkey0742ec92014-10-13 08:39:55 -070019import static org.hamcrest.MatcherAssert.assertThat;
Ray Milkeye6684082014-10-16 16:59:47 -070020import static org.hamcrest.Matchers.equalTo;
Ray Milkey0742ec92014-10-13 08:39:55 -070021import static org.hamcrest.Matchers.is;
Pavlin Radoslavovd26f57a2014-10-23 17:19:45 -070022import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
Ray Milkeye6684082014-10-16 16:59:47 -070023import static org.onlab.onos.net.NetTestTools.link;
Ray Milkey0742ec92014-10-13 08:39:55 -070024
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070025import java.util.HashSet;
26import java.util.Set;
27
28import org.junit.Before;
29import org.junit.Test;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070030import org.onlab.onos.core.ApplicationId;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070031import org.onlab.onos.TestApplicationId;
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070032import org.onlab.onos.net.ConnectPoint;
33import org.onlab.onos.net.DeviceId;
34import org.onlab.onos.net.Link;
35import org.onlab.onos.net.PortNumber;
36import org.onlab.onos.net.flow.TrafficSelector;
37import org.onlab.onos.net.flow.TrafficTreatment;
38
Ray Milkeye6684082014-10-16 16:59:47 -070039/**
40 * Unit tests for the LinkCollectionIntent class.
41 */
Ray Milkey0742ec92014-10-13 08:39:55 -070042public class TestLinkCollectionIntent {
43
Thomas Vachuskab97cf282014-10-20 23:31:12 -070044 private static final ApplicationId APPID = new TestApplicationId("foo");
45
Ray Milkeye6684082014-10-16 16:59:47 -070046 private Link link1 = link("dev1", 1, "dev2", 2);
47 private Link link2 = link("dev1", 1, "dev3", 2);
48 private Link link3 = link("dev2", 1, "dev3", 2);
49
50 private Set<Link> links1;
51 private Set<Link> links2;
52
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070053 private ConnectPoint egress1 = new ConnectPoint(DeviceId.deviceId("dev1"),
54 PortNumber.portNumber(3));
55 private ConnectPoint egress2 = new ConnectPoint(DeviceId.deviceId("dev2"),
56 PortNumber.portNumber(3));
57
Ray Milkeye6684082014-10-16 16:59:47 -070058 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
59 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
60
Thomas Vachuskab97cf282014-10-20 23:31:12 -070061 private LinkCollectionIntent makeLinkCollection(Set<Link> links,
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070062 ConnectPoint egress) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070063 return new LinkCollectionIntent(APPID, selector, treatment, links, egress);
Ray Milkey0742ec92014-10-13 08:39:55 -070064 }
65
Ray Milkeye6684082014-10-16 16:59:47 -070066 @Before
67 public void setup() {
68 links1 = new HashSet<>();
69 links2 = new HashSet<>();
Ray Milkey0742ec92014-10-13 08:39:55 -070070 }
71
Ray Milkeye6684082014-10-16 16:59:47 -070072 /**
73 * Tests the equals() method where two LinkCollectionIntents have references
74 * to the same Links in different orders. These should compare equal.
75 */
Ray Milkey0742ec92014-10-13 08:39:55 -070076 @Test
Ray Milkeye6684082014-10-16 16:59:47 -070077 public void testSameEquals() {
78 links1.add(link1);
79 links1.add(link2);
80 links1.add(link3);
Ray Milkey0742ec92014-10-13 08:39:55 -070081
Ray Milkeye6684082014-10-16 16:59:47 -070082 links2.add(link3);
83 links2.add(link2);
84 links2.add(link1);
85
Thomas Vachuskab97cf282014-10-20 23:31:12 -070086 LinkCollectionIntent i1 = makeLinkCollection(links1, egress1);
87 LinkCollectionIntent i2 = makeLinkCollection(links2, egress1);
Ray Milkeye6684082014-10-16 16:59:47 -070088
89 assertThat(i1, is(equalTo(i2)));
Ray Milkey0742ec92014-10-13 08:39:55 -070090 }
91
Ray Milkeye6684082014-10-16 16:59:47 -070092 /**
93 * Tests the equals() method where two LinkCollectionIntents have references
94 * to different Links. These should compare not equal.
95 */
96 @Test
97 public void testLinksDifferentEquals() {
98 links1.add(link1);
99 links1.add(link2);
100
101 links2.add(link3);
102 links2.add(link1);
103
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700104 LinkCollectionIntent i1 = makeLinkCollection(links1, egress1);
105 LinkCollectionIntent i2 = makeLinkCollection(links2, egress1);
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700106
107 assertThat(i1, is(not(equalTo(i2))));
108 }
109
110 /**
111 * Tests the equals() method where two LinkCollectionIntents have references
112 * to the same Links but different egress points. These should compare not equal.
113 */
114 @Test
115 public void testEgressDifferentEquals() {
116 links1.add(link1);
117 links1.add(link2);
118 links1.add(link3);
119
120 links2.add(link3);
121 links2.add(link2);
122 links2.add(link1);
123
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700124 LinkCollectionIntent i1 = makeLinkCollection(links1, egress1);
125 LinkCollectionIntent i2 = makeLinkCollection(links2, egress2);
Ray Milkeye6684082014-10-16 16:59:47 -0700126
127 assertThat(i1, is(not(equalTo(i2))));
128 }
129
130 /**
131 * Tests that the hashCode() values for two equivalent LinkCollectionIntent
132 * objects are the same.
133 */
134 @Test
135 public void testHashCodeEquals() {
136 links1.add(link1);
137 links1.add(link2);
138 links1.add(link3);
139
140 links2.add(link3);
141 links2.add(link2);
142 links2.add(link1);
143
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700144 LinkCollectionIntent i1 = makeLinkCollection(links1, egress1);
145 LinkCollectionIntent i2 = makeLinkCollection(links2, egress1);
Ray Milkeye6684082014-10-16 16:59:47 -0700146
147 assertThat(i1.hashCode(), is(equalTo(i2.hashCode())));
148 }
149
150 /**
151 * Tests that the hashCode() values for two distinct LinkCollectionIntent
152 * objects are different.
153 */
154 @Test
155 public void testHashCodeDifferent() {
156 links1.add(link1);
157 links1.add(link2);
158
159 links2.add(link1);
160 links2.add(link3);
161
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700162 LinkCollectionIntent i1 = makeLinkCollection(links1, egress1);
163 LinkCollectionIntent i2 = makeLinkCollection(links2, egress2);
Ray Milkeye6684082014-10-16 16:59:47 -0700164
165 assertThat(i1.hashCode(), is(not(equalTo(i2.hashCode()))));
166 }
167
168 /**
169 * Checks that the HostToHostIntent class is immutable.
170 */
171 @Test
172 public void checkImmutability() {
Pavlin Radoslavovd26f57a2014-10-23 17:19:45 -0700173 assertThatClassIsImmutable(LinkCollectionIntent.class);
Ray Milkeye6684082014-10-16 16:59:47 -0700174 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700175}