Sho SHIMIZU
Committed by Ray Milkey

Move unit tests to PointToPointIntentCompilerTest

The unit tests only depend on PointToPointIntentCompiler and it would be
obstacles when IntentCompiler subclasses are moved to a dedicated package

Change-Id: Ifbfe5ef2a4bae014d2b6cae599fe8860a54e01ca
Reference: ONOS-1066
...@@ -107,75 +107,6 @@ public class PathConstraintCalculationTest extends AbstractIntentTest { ...@@ -107,75 +107,6 @@ public class PathConstraintCalculationTest extends AbstractIntentTest {
107 } 107 }
108 108
109 /** 109 /**
110 - * Tests that requests with sufficient available bandwidth succeed.
111 - */
112 - @Test
113 - public void testBandwidthConstrainedIntentSuccess() {
114 -
115 - final LinkResourceService resourceService =
116 - IntentTestsMocks.MockResourceService.makeBandwidthResourceService(1000.0);
117 - final Constraint constraint = new BandwidthConstraint(Bandwidth.bps(100.0));
118 -
119 - final List<Intent> compiledIntents = compileIntent(constraint, resourceService);
120 - assertThat(compiledIntents, notNullValue());
121 - assertThat(compiledIntents, hasSize(1));
122 - }
123 -
124 - /**
125 - * Tests that requests with insufficient available bandwidth fail.
126 - */
127 - @Test
128 - public void testBandwidthConstrainedIntentFailure() {
129 -
130 - final LinkResourceService resourceService =
131 - IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
132 - final Constraint constraint = new BandwidthConstraint(Bandwidth.bps(100.0));
133 -
134 - try {
135 - compileIntent(constraint, resourceService);
136 - fail("Point to Point compilation with insufficient bandwidth does "
137 - + "not throw exception.");
138 - } catch (PathNotFoundException noPath) {
139 - assertThat(noPath.getMessage(), containsString("No path"));
140 - }
141 - }
142 -
143 - /**
144 - * Tests that requests for available lambdas are successful.
145 - */
146 - @Test
147 - public void testLambdaConstrainedIntentSuccess() {
148 -
149 - final Constraint constraint = new LambdaConstraint(Lambda.valueOf(1));
150 - final LinkResourceService resourceService =
151 - IntentTestsMocks.MockResourceService.makeLambdaResourceService(1);
152 -
153 - final List<Intent> compiledIntents =
154 - compileIntent(constraint, resourceService);
155 - assertThat(compiledIntents, notNullValue());
156 - assertThat(compiledIntents, hasSize(1));
157 - }
158 -
159 - /**
160 - * Tests that requests for lambdas when there are no available lambdas
161 - * fail.
162 - */
163 - @Test
164 - public void testLambdaConstrainedIntentFailure() {
165 -
166 - final Constraint constraint = new LambdaConstraint(Lambda.valueOf(1));
167 - final LinkResourceService resourceService =
168 - IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
169 - try {
170 - compileIntent(constraint, resourceService);
171 - fail("Point to Point compilation with no available lambda does "
172 - + "not throw exception.");
173 - } catch (PathNotFoundException noPath) {
174 - assertThat(noPath.getMessage(), containsString("No path"));
175 - }
176 - }
177 -
178 - /**
179 * Tests that installation of bandwidth constrained path intents are 110 * Tests that installation of bandwidth constrained path intents are
180 * successful. 111 * successful.
181 */ 112 */
......
...@@ -25,17 +25,26 @@ import org.onosproject.net.Path; ...@@ -25,17 +25,26 @@ import org.onosproject.net.Path;
25 import org.onosproject.net.flow.TrafficSelector; 25 import org.onosproject.net.flow.TrafficSelector;
26 import org.onosproject.net.flow.TrafficTreatment; 26 import org.onosproject.net.flow.TrafficTreatment;
27 import org.onosproject.net.intent.AbstractIntentTest; 27 import org.onosproject.net.intent.AbstractIntentTest;
28 +import org.onosproject.net.intent.Constraint;
28 import org.onosproject.net.intent.Intent; 29 import org.onosproject.net.intent.Intent;
29 import org.onosproject.net.intent.IntentTestsMocks; 30 import org.onosproject.net.intent.IntentTestsMocks;
30 import org.onosproject.net.intent.PathIntent; 31 import org.onosproject.net.intent.PathIntent;
31 import org.onosproject.net.intent.PointToPointIntent; 32 import org.onosproject.net.intent.PointToPointIntent;
33 +import org.onosproject.net.intent.constraint.BandwidthConstraint;
34 +import org.onosproject.net.intent.constraint.LambdaConstraint;
35 +import org.onosproject.net.resource.Bandwidth;
36 +import org.onosproject.net.resource.Lambda;
37 +import org.onosproject.net.resource.LinkResourceService;
32 38
39 +import java.util.LinkedList;
33 import java.util.List; 40 import java.util.List;
34 41
35 import static org.hamcrest.CoreMatchers.instanceOf; 42 import static org.hamcrest.CoreMatchers.instanceOf;
36 import static org.hamcrest.MatcherAssert.assertThat; 43 import static org.hamcrest.MatcherAssert.assertThat;
44 +import static org.hamcrest.Matchers.containsString;
37 import static org.hamcrest.Matchers.hasSize; 45 import static org.hamcrest.Matchers.hasSize;
38 import static org.hamcrest.Matchers.is; 46 import static org.hamcrest.Matchers.is;
47 +import static org.junit.Assert.fail;
39 import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; 48 import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
40 import static org.onosproject.net.DeviceId.deviceId; 49 import static org.onosproject.net.DeviceId.deviceId;
41 import static org.onosproject.net.NetTestTools.APP_ID; 50 import static org.onosproject.net.NetTestTools.APP_ID;
...@@ -80,6 +89,47 @@ public class PointToPointIntentCompilerTest extends AbstractIntentTest { ...@@ -80,6 +89,47 @@ public class PointToPointIntentCompilerTest extends AbstractIntentTest {
80 return compiler; 89 return compiler;
81 } 90 }
82 91
92 + /**
93 + * Creates a point to point intent compiler for a three switch linear
94 + * topology.
95 + *
96 + * @param resourceService service to use for resource allocation requests
97 + * @return point to point compiler
98 + */
99 + private PointToPointIntentCompiler makeCompiler(LinkResourceService resourceService) {
100 + final String[] hops = {"s1", "s2", "s3"};
101 + final PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
102 + compiler.resourceService = resourceService;
103 + compiler.pathService = new IntentTestsMocks.MockPathService(hops);
104 + return compiler;
105 + }
106 +
107 + /**
108 + * Creates an intent with a given constraint and compiles it. The compiler
109 + * will throw PathNotFoundException if the allocations cannot be satisfied.
110 + *
111 + * @param constraint constraint to apply to the created intent
112 + * @param resourceService service to use for resource allocation requests
113 + * @return List of compiled intents
114 + */
115 + private List<Intent> compileIntent(Constraint constraint,
116 + LinkResourceService resourceService) {
117 + final List<Constraint> constraints = new LinkedList<>();
118 + constraints.add(constraint);
119 + final TrafficSelector selector = new IntentTestsMocks.MockSelector();
120 + final TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
121 +
122 + final PointToPointIntent intent =
123 + new PointToPointIntent(APP_ID,
124 + selector,
125 + treatment,
126 + connectPoint("s1", 1),
127 + connectPoint("s3", 1),
128 + constraints);
129 + final PointToPointIntentCompiler compiler = makeCompiler(resourceService);
130 +
131 + return compiler.compile(intent, null, null);
132 + }
83 133
84 /** 134 /**
85 * Tests a pair of devices in an 8 hop path, forward direction. 135 * Tests a pair of devices in an 8 hop path, forward direction.
...@@ -166,4 +216,74 @@ public class PointToPointIntentCompilerTest extends AbstractIntentTest { ...@@ -166,4 +216,74 @@ public class PointToPointIntentCompilerTest extends AbstractIntentTest {
166 Link secondLink = path.links().get(1); 216 Link secondLink = path.links().get(1);
167 assertThat(secondLink, is(createEdgeLink(dst, false))); 217 assertThat(secondLink, is(createEdgeLink(dst, false)));
168 } 218 }
219 +
220 + /**
221 + * Tests that requests with sufficient available bandwidth succeed.
222 + */
223 + @Test
224 + public void testBandwidthConstrainedIntentSuccess() {
225 +
226 + final LinkResourceService resourceService =
227 + IntentTestsMocks.MockResourceService.makeBandwidthResourceService(1000.0);
228 + final Constraint constraint = new BandwidthConstraint(Bandwidth.bps(100.0));
229 +
230 + final List<Intent> compiledIntents = compileIntent(constraint, resourceService);
231 + assertThat(compiledIntents, Matchers.notNullValue());
232 + assertThat(compiledIntents, hasSize(1));
233 + }
234 +
235 + /**
236 + * Tests that requests with insufficient available bandwidth fail.
237 + */
238 + @Test
239 + public void testBandwidthConstrainedIntentFailure() {
240 +
241 + final LinkResourceService resourceService =
242 + IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
243 + final Constraint constraint = new BandwidthConstraint(Bandwidth.bps(100.0));
244 +
245 + try {
246 + compileIntent(constraint, resourceService);
247 + fail("Point to Point compilation with insufficient bandwidth does "
248 + + "not throw exception.");
249 + } catch (PathNotFoundException noPath) {
250 + assertThat(noPath.getMessage(), containsString("No path"));
251 + }
252 + }
253 +
254 + /**
255 + * Tests that requests for available lambdas are successful.
256 + */
257 + @Test
258 + public void testLambdaConstrainedIntentSuccess() {
259 +
260 + final Constraint constraint = new LambdaConstraint(Lambda.valueOf(1));
261 + final LinkResourceService resourceService =
262 + IntentTestsMocks.MockResourceService.makeLambdaResourceService(1);
263 +
264 + final List<Intent> compiledIntents =
265 + compileIntent(constraint, resourceService);
266 + assertThat(compiledIntents, Matchers.notNullValue());
267 + assertThat(compiledIntents, hasSize(1));
268 + }
269 +
270 + /**
271 + * Tests that requests for lambdas when there are no available lambdas
272 + * fail.
273 + */
274 + @Test
275 + public void testLambdaConstrainedIntentFailure() {
276 +
277 + final Constraint constraint = new LambdaConstraint(Lambda.valueOf(1));
278 + final LinkResourceService resourceService =
279 + IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
280 + try {
281 + compileIntent(constraint, resourceService);
282 + fail("Point to Point compilation with no available lambda does "
283 + + "not throw exception.");
284 + } catch (PathNotFoundException noPath) {
285 + assertThat(noPath.getMessage(), containsString("No path"));
286 + }
287 + }
288 +
169 } 289 }
......