blob: 5325cfcc6d1cdf3d3123a21bc7e5e90ebd867726 [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 Milkeye6684082014-10-16 16:59:47 -070016package org.onlab.onos.net.intent;
17
Ray Milkeye6684082014-10-16 16:59:47 -070018import org.junit.Before;
19import org.junit.Test;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070020import org.onlab.onos.core.ApplicationId;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070021import org.onlab.onos.TestApplicationId;
Ray Milkeye6684082014-10-16 16:59:47 -070022import org.onlab.onos.net.ConnectPoint;
23import org.onlab.onos.net.flow.TrafficSelector;
24import org.onlab.onos.net.flow.TrafficTreatment;
25
Thomas Vachuskab97cf282014-10-20 23:31:12 -070026import java.util.HashSet;
27import java.util.Set;
28
Ray Milkeye6684082014-10-16 16:59:47 -070029import static org.hamcrest.CoreMatchers.not;
30import static org.hamcrest.MatcherAssert.assertThat;
31import static org.hamcrest.Matchers.equalTo;
32import static org.hamcrest.Matchers.is;
Pavlin Radoslavovd26f57a2014-10-23 17:19:45 -070033import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
Ray Milkeye6684082014-10-16 16:59:47 -070034import static org.onlab.onos.net.NetTestTools.connectPoint;
35
36/**
37 * Unit tests for the MultiPointToSinglePointIntent class.
38 */
39public class TestMultiPointToSinglePointIntent {
40
Thomas Vachuskab97cf282014-10-20 23:31:12 -070041 private static final ApplicationId APPID = new TestApplicationId("foo");
42
Ray Milkeye6684082014-10-16 16:59:47 -070043 private ConnectPoint point1 = connectPoint("dev1", 1);
44 private ConnectPoint point2 = connectPoint("dev2", 1);
45 private ConnectPoint point3 = connectPoint("dev3", 1);
46
47 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
48 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
49
50 Set<ConnectPoint> ingress1;
51 Set<ConnectPoint> ingress2;
52
53 /**
54 * Creates a MultiPointToSinglePointIntent object.
55 *
Ray Milkeye6684082014-10-16 16:59:47 -070056 * @param ingress set of ingress points
Thomas Vachuskab97cf282014-10-20 23:31:12 -070057 * @param egress egress point
Ray Milkeye6684082014-10-16 16:59:47 -070058 * @return MultiPointToSinglePoint intent
59 */
Thomas Vachuskab97cf282014-10-20 23:31:12 -070060 private MultiPointToSinglePointIntent makeIntent(Set<ConnectPoint> ingress,
61 ConnectPoint egress) {
62 return new MultiPointToSinglePointIntent(APPID, selector, treatment,
63 ingress, egress);
Ray Milkeye6684082014-10-16 16:59:47 -070064 }
65
66 /**
67 * Initializes the ingress sets.
68 */
69 @Before
70 public void setup() {
71 ingress1 = new HashSet<>();
72 ingress2 = new HashSet<>();
73 }
74
75 /**
76 * Tests the equals() method where two MultiPointToSinglePoint have references
77 * to the same Links in different orders. These should compare equal.
78 */
79 @Test
80 public void testSameEquals() {
81
82 Set<ConnectPoint> ingress1 = new HashSet<>();
83 ingress1.add(point2);
84 ingress1.add(point3);
85
86 Set<ConnectPoint> ingress2 = new HashSet<>();
87 ingress2.add(point3);
88 ingress2.add(point2);
89
Thomas Vachuskab97cf282014-10-20 23:31:12 -070090 Intent i1 = makeIntent(ingress1, point1);
91 Intent i2 = makeIntent(ingress2, point1);
Ray Milkeye6684082014-10-16 16:59:47 -070092
93 assertThat(i1, is(equalTo(i2)));
94 }
95
96 /**
97 * Tests the equals() method where two MultiPointToSinglePoint have references
98 * to different Links. These should compare not equal.
99 */
100 @Test
101 public void testLinksDifferentEquals() {
102 ingress1.add(point3);
103
104 ingress2.add(point3);
105 ingress2.add(point2);
106
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700107 Intent i1 = makeIntent(ingress1, point1);
108 Intent i2 = makeIntent(ingress2, point1);
Ray Milkeye6684082014-10-16 16:59:47 -0700109
110 assertThat(i1, is(not(equalTo(i2))));
111 }
112
113 /**
114 * Tests that the hashCode() values for two equivalent MultiPointToSinglePoint
115 * objects are the same.
116 */
117 @Test
118 public void testHashCodeEquals() {
119 ingress1.add(point2);
120 ingress1.add(point3);
121
122 ingress2.add(point3);
123 ingress2.add(point2);
124
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700125 Intent i1 = makeIntent(ingress1, point1);
126 Intent i2 = makeIntent(ingress2, point1);
Ray Milkeye6684082014-10-16 16:59:47 -0700127
128 assertThat(i1.hashCode(), is(equalTo(i2.hashCode())));
129 }
130
131 /**
132 * Tests that the hashCode() values for two distinct MultiPointToSinglePoint
133 * objects are different.
134 */
135 @Test
136 public void testHashCodeDifferent() {
137 ingress1.add(point2);
138
139 ingress2.add(point3);
140 ingress2.add(point2);
141
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700142 Intent i1 = makeIntent(ingress1, point1);
143 Intent i2 = makeIntent(ingress2, point1);
Ray Milkeye6684082014-10-16 16:59:47 -0700144
145
146 assertThat(i1.hashCode(), is(not(equalTo(i2.hashCode()))));
147 }
148
149 /**
150 * Checks that the MultiPointToSinglePointIntent class is immutable.
151 */
152 @Test
153 public void checkImmutability() {
Pavlin Radoslavovd26f57a2014-10-23 17:19:45 -0700154 assertThatClassIsImmutable(MultiPointToSinglePointIntent.class);
Ray Milkeye6684082014-10-16 16:59:47 -0700155 }
156}