blob: d1a8a8d20f5e943a744b1defc775b71f120203aa [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;
Brian O'Connor520c0522014-11-23 23:50:47 -080029import org.junit.Ignore;
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070030import org.junit.Test;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070031import org.onlab.onos.core.ApplicationId;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070032import org.onlab.onos.TestApplicationId;
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070033import org.onlab.onos.net.ConnectPoint;
34import org.onlab.onos.net.DeviceId;
35import org.onlab.onos.net.Link;
36import org.onlab.onos.net.PortNumber;
37import org.onlab.onos.net.flow.TrafficSelector;
38import org.onlab.onos.net.flow.TrafficTreatment;
39
Ray Milkeye6684082014-10-16 16:59:47 -070040/**
41 * Unit tests for the LinkCollectionIntent class.
42 */
Ray Milkey0742ec92014-10-13 08:39:55 -070043public class TestLinkCollectionIntent {
44
Thomas Vachuskab97cf282014-10-20 23:31:12 -070045 private static final ApplicationId APPID = new TestApplicationId("foo");
46
Ray Milkeye6684082014-10-16 16:59:47 -070047 private Link link1 = link("dev1", 1, "dev2", 2);
48 private Link link2 = link("dev1", 1, "dev3", 2);
49 private Link link3 = link("dev2", 1, "dev3", 2);
50
51 private Set<Link> links1;
52 private Set<Link> links2;
53
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070054 private ConnectPoint egress1 = new ConnectPoint(DeviceId.deviceId("dev1"),
55 PortNumber.portNumber(3));
56 private ConnectPoint egress2 = new ConnectPoint(DeviceId.deviceId("dev2"),
57 PortNumber.portNumber(3));
58
Ray Milkeye6684082014-10-16 16:59:47 -070059 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
60 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
61
Thomas Vachuskab97cf282014-10-20 23:31:12 -070062 private LinkCollectionIntent makeLinkCollection(Set<Link> links,
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070063 ConnectPoint egress) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070064 return new LinkCollectionIntent(APPID, selector, treatment, links, egress);
Ray Milkey0742ec92014-10-13 08:39:55 -070065 }
66
Ray Milkeye6684082014-10-16 16:59:47 -070067 @Before
68 public void setup() {
69 links1 = new HashSet<>();
70 links2 = new HashSet<>();
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 the same Links in different orders. These should compare equal.
76 */
Brian O'Connor520c0522014-11-23 23:50:47 -080077 @Test @Ignore("Needs to be merged with other API test")
Ray Milkeye6684082014-10-16 16:59:47 -070078 public void testSameEquals() {
79 links1.add(link1);
80 links1.add(link2);
81 links1.add(link3);
Ray Milkey0742ec92014-10-13 08:39:55 -070082
Ray Milkeye6684082014-10-16 16:59:47 -070083 links2.add(link3);
84 links2.add(link2);
85 links2.add(link1);
86
Thomas Vachuskab97cf282014-10-20 23:31:12 -070087 LinkCollectionIntent i1 = makeLinkCollection(links1, egress1);
88 LinkCollectionIntent i2 = makeLinkCollection(links2, egress1);
Ray Milkeye6684082014-10-16 16:59:47 -070089
90 assertThat(i1, is(equalTo(i2)));
Ray Milkey0742ec92014-10-13 08:39:55 -070091 }
92
Ray Milkeye6684082014-10-16 16:59:47 -070093 /**
94 * Tests the equals() method where two LinkCollectionIntents have references
95 * to different Links. These should compare not equal.
96 */
Brian O'Connor520c0522014-11-23 23:50:47 -080097 @Test @Ignore("Needs to be merged with other API test")
Ray Milkeye6684082014-10-16 16:59:47 -070098 public void testLinksDifferentEquals() {
99 links1.add(link1);
100 links1.add(link2);
101
102 links2.add(link3);
103 links2.add(link1);
104
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700105 LinkCollectionIntent i1 = makeLinkCollection(links1, egress1);
106 LinkCollectionIntent i2 = makeLinkCollection(links2, egress1);
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700107
108 assertThat(i1, is(not(equalTo(i2))));
109 }
110
111 /**
112 * Tests the equals() method where two LinkCollectionIntents have references
113 * to the same Links but different egress points. These should compare not equal.
114 */
Brian O'Connor520c0522014-11-23 23:50:47 -0800115 @Test @Ignore("Needs to be merged with other API test")
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700116 public void testEgressDifferentEquals() {
117 links1.add(link1);
118 links1.add(link2);
119 links1.add(link3);
120
121 links2.add(link3);
122 links2.add(link2);
123 links2.add(link1);
124
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700125 LinkCollectionIntent i1 = makeLinkCollection(links1, egress1);
126 LinkCollectionIntent i2 = makeLinkCollection(links2, egress2);
Ray Milkeye6684082014-10-16 16:59:47 -0700127
128 assertThat(i1, is(not(equalTo(i2))));
129 }
130
131 /**
132 * Tests that the hashCode() values for two equivalent LinkCollectionIntent
133 * objects are the same.
134 */
Brian O'Connor520c0522014-11-23 23:50:47 -0800135 @Test @Ignore("Needs to be merged with other API test")
Ray Milkeye6684082014-10-16 16:59:47 -0700136 public void testHashCodeEquals() {
137 links1.add(link1);
138 links1.add(link2);
139 links1.add(link3);
140
141 links2.add(link3);
142 links2.add(link2);
143 links2.add(link1);
144
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700145 LinkCollectionIntent i1 = makeLinkCollection(links1, egress1);
146 LinkCollectionIntent i2 = makeLinkCollection(links2, egress1);
Ray Milkeye6684082014-10-16 16:59:47 -0700147
148 assertThat(i1.hashCode(), is(equalTo(i2.hashCode())));
149 }
150
151 /**
152 * Tests that the hashCode() values for two distinct LinkCollectionIntent
153 * objects are different.
154 */
Brian O'Connor520c0522014-11-23 23:50:47 -0800155 @Test @Ignore("Needs to be merged with other API test")
Ray Milkeye6684082014-10-16 16:59:47 -0700156 public void testHashCodeDifferent() {
157 links1.add(link1);
158 links1.add(link2);
159
160 links2.add(link1);
161 links2.add(link3);
162
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700163 LinkCollectionIntent i1 = makeLinkCollection(links1, egress1);
164 LinkCollectionIntent i2 = makeLinkCollection(links2, egress2);
Ray Milkeye6684082014-10-16 16:59:47 -0700165
166 assertThat(i1.hashCode(), is(not(equalTo(i2.hashCode()))));
167 }
168
169 /**
170 * Checks that the HostToHostIntent class is immutable.
171 */
172 @Test
173 public void checkImmutability() {
Pavlin Radoslavovd26f57a2014-10-23 17:19:45 -0700174 assertThatClassIsImmutable(LinkCollectionIntent.class);
Ray Milkeye6684082014-10-16 16:59:47 -0700175 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700176}