blob: 0f373ff1c0fcbb5674d6563a7f25cd9e822b98ad [file] [log] [blame]
Ray Milkeyc8f481f2014-11-18 15:37:12 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Ray Milkeyc8f481f2014-11-18 15:37:12 -08003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080017
18import org.junit.Test;
HIGUCHI Yuta501c4652015-10-29 14:21:48 -070019import org.onlab.util.DataRateUnit;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.TestApplicationId;
21import org.onosproject.core.ApplicationId;
22import org.onosproject.net.HostId;
23import org.onosproject.net.flow.TrafficSelector;
HIGUCHI Yuta584107a2015-10-27 18:12:44 -070024import org.onosproject.net.intent.constraint.BandwidthConstraint;
HIGUCHI Yuta584107a2015-10-27 18:12:44 -070025import com.google.common.collect.ImmutableList;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080026import com.google.common.testing.EqualsTester;
27
Ray Milkey37f6a382014-11-25 14:54:42 -080028import static org.hamcrest.MatcherAssert.assertThat;
29import static org.hamcrest.Matchers.equalTo;
HIGUCHI Yuta584107a2015-10-27 18:12:44 -070030import static org.hamcrest.Matchers.hasItem;
Ray Milkey37f6a382014-11-25 14:54:42 -080031import static org.hamcrest.Matchers.is;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080032import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import static org.onosproject.net.NetTestTools.hid;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080034
35/**
36 * Unit tests for the HostToHostIntent class.
37 */
Brian O'Connor520c0522014-11-23 23:50:47 -080038public class HostToHostIntentTest extends IntentTest {
39 private final TrafficSelector selector = new IntentTestsMocks.MockSelector();
40 private final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment();
41 private final HostId id1 = hid("12:34:56:78:91:ab/1");
42 private final HostId id2 = hid("12:34:56:78:92:ab/1");
43 private final HostId id3 = hid("12:34:56:78:93:ab/1");
Ray Milkeyc8f481f2014-11-18 15:37:12 -080044
Ray Milkey37f6a382014-11-25 14:54:42 -080045 private static final ApplicationId APPID = new TestApplicationId("foo");
46
47 private HostToHostIntent makeHostToHost(HostId one, HostId two) {
Ray Milkeyebc5d222015-03-18 15:45:36 -070048 return HostToHostIntent.builder()
49 .appId(APPID)
50 .one(one)
51 .two(two)
52 .selector(selector)
53 .treatment(treatment)
54 .build();
Ray Milkey37f6a382014-11-25 14:54:42 -080055 }
56
57 /**
58 * Tests the equals() method where two HostToHostIntents have references
59 * to the same hosts. These should compare equal.
60 */
61 @Test
62 public void testSameEquals() {
63
64 HostId one = hid("00:00:00:00:00:01/-1");
65 HostId two = hid("00:00:00:00:00:02/-1");
66 HostToHostIntent i1 = makeHostToHost(one, two);
67 HostToHostIntent i2 = makeHostToHost(one, two);
68
69 assertThat(i1.one(), is(equalTo(i2.one())));
70 assertThat(i1.two(), is(equalTo(i2.two())));
71 }
72
Ray Milkeyc8f481f2014-11-18 15:37:12 -080073 /**
74 * Checks that the HostToHostIntent class is immutable.
75 */
76 @Test
77 public void testImmutability() {
78 assertThatClassIsImmutable(HostToHostIntent.class);
79 }
80
81 /**
82 * Tests equals(), hashCode() and toString() methods.
83 */
Ray Milkey37f6a382014-11-25 14:54:42 -080084 @Test
Ray Milkeyc8f481f2014-11-18 15:37:12 -080085 public void testEquals() {
Ray Milkeyebc5d222015-03-18 15:45:36 -070086 final HostToHostIntent intent1 = HostToHostIntent.builder()
87 .appId(APPID)
88 .one(id1)
89 .two(id2)
90 .selector(selector)
91 .treatment(treatment)
92 .build();
Ray Milkey37f6a382014-11-25 14:54:42 -080093
Ray Milkeyebc5d222015-03-18 15:45:36 -070094 final HostToHostIntent intent2 = HostToHostIntent.builder()
95 .appId(APPID)
96 .one(id2)
97 .two(id3)
98 .selector(selector)
99 .treatment(treatment)
100 .build();
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800101
102 new EqualsTester()
Ray Milkey37f6a382014-11-25 14:54:42 -0800103 .addEqualityGroup(intent1)
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800104 .addEqualityGroup(intent2)
105 .testEquals();
106 }
Brian O'Connor520c0522014-11-23 23:50:47 -0800107
HIGUCHI Yuta584107a2015-10-27 18:12:44 -0700108 @Test
109 public void testImplicitConstraintsAreAdded() {
HIGUCHI Yuta501c4652015-10-29 14:21:48 -0700110 final Constraint other = BandwidthConstraint.of(1, DataRateUnit.GBPS);
HIGUCHI Yuta584107a2015-10-27 18:12:44 -0700111 final HostToHostIntent intent = HostToHostIntent.builder()
112 .appId(APPID)
113 .one(id1)
114 .two(id2)
115 .selector(selector)
116 .treatment(treatment)
117 .constraints(ImmutableList.of(other))
118 .build();
119
120 assertThat(intent.constraints(), hasItem(HostToHostIntent.NOT_OPTICAL));
121 }
122
123 @Test
124 public void testImplicitConstraints() {
125 final HostToHostIntent implicit = HostToHostIntent.builder()
126 .appId(APPID)
127 .one(id1)
128 .two(id2)
129 .selector(selector)
130 .treatment(treatment)
131 .build();
132 final HostToHostIntent empty = HostToHostIntent.builder()
133 .appId(APPID)
134 .one(id1)
135 .two(id2)
136 .selector(selector)
137 .treatment(treatment)
138 .constraints(ImmutableList.of())
139 .build();
140 final HostToHostIntent exact = HostToHostIntent.builder()
141 .appId(APPID)
142 .one(id1)
143 .two(id2)
144 .selector(selector)
145 .treatment(treatment)
146 .constraints(ImmutableList.of(HostToHostIntent.NOT_OPTICAL))
147 .build();
148
149 new EqualsTester()
150 .addEqualityGroup(implicit.constraints(),
151 empty.constraints(),
152 exact.constraints())
153 .testEquals();
154
155 }
156
Brian O'Connor520c0522014-11-23 23:50:47 -0800157 @Override
158 protected Intent createOne() {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700159 return HostToHostIntent.builder()
160 .appId(APPID)
161 .one(id1)
162 .two(id2)
163 .selector(selector)
164 .treatment(treatment)
165 .build();
Brian O'Connor520c0522014-11-23 23:50:47 -0800166 }
167
168 @Override
169 protected Intent createAnother() {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700170 return HostToHostIntent.builder()
171 .appId(APPID)
172 .one(id1)
173 .two(id3)
174 .selector(selector)
175 .treatment(treatment)
176 .build();
Brian O'Connor520c0522014-11-23 23:50:47 -0800177 }
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800178}