blob: c60c8740902afb0592817e08576d8b9ba7ad044c [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.impl;
Brian O'Connor427a1762014-11-19 18:40:32 -080017
Brian O'Connorb499b352015-02-03 16:46:15 -080018import com.google.common.collect.HashMultimap;
19import com.google.common.collect.Lists;
20import com.google.common.collect.Maps;
21import com.google.common.collect.Multimap;
22import com.google.common.collect.Sets;
Brian O'Connor427a1762014-11-19 18:40:32 -080023import org.hamcrest.Description;
Brian O'Connor427a1762014-11-19 18:40:32 -080024import org.hamcrest.TypeSafeMatcher;
25import org.junit.After;
26import org.junit.Before;
Jonathan Hart4fd4ebb2015-02-04 17:38:48 -080027import org.junit.Ignore;
Brian O'Connor427a1762014-11-19 18:40:32 -080028import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.TestApplicationId;
30import org.onosproject.core.ApplicationId;
31import org.onosproject.core.impl.TestCoreManager;
32import org.onosproject.event.impl.TestEventDispatcher;
33import org.onosproject.net.NetworkResource;
34import org.onosproject.net.flow.FlowRule;
35import org.onosproject.net.flow.FlowRuleBatchEntry;
36import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
37import org.onosproject.net.flow.FlowRuleBatchOperation;
38import org.onosproject.net.intent.Intent;
39import org.onosproject.net.intent.IntentCompiler;
40import org.onosproject.net.intent.IntentEvent;
41import org.onosproject.net.intent.IntentEvent.Type;
42import org.onosproject.net.intent.IntentExtensionService;
43import org.onosproject.net.intent.IntentId;
44import org.onosproject.net.intent.IntentInstaller;
45import org.onosproject.net.intent.IntentListener;
46import org.onosproject.net.intent.IntentService;
47import org.onosproject.net.intent.IntentState;
48import org.onosproject.net.intent.IntentTestsMocks;
Ray Milkeyf9af43c2015-02-09 16:45:48 -080049import org.onosproject.net.intent.Key;
Brian O'Connorabafb502014-12-02 22:26:20 -080050import org.onosproject.net.resource.LinkResourceAllocations;
Brian O'Connor47bc8552015-02-11 11:03:32 -080051import org.onosproject.store.intent.impl.SimpleIntentStore;
Brian O'Connor427a1762014-11-19 18:40:32 -080052
Brian O'Connorb499b352015-02-03 16:46:15 -080053import java.util.Collection;
54import java.util.Collections;
55import java.util.List;
56import java.util.Map;
57import java.util.Set;
58import java.util.concurrent.CountDownLatch;
59import java.util.concurrent.TimeUnit;
60import java.util.concurrent.atomic.AtomicLong;
Brian O'Connor427a1762014-11-19 18:40:32 -080061
Ray Milkeye9a3e222014-12-03 16:46:06 -080062import static org.hamcrest.MatcherAssert.assertThat;
63import static org.hamcrest.Matchers.hasSize;
Brian O'Connorb499b352015-02-03 16:46:15 -080064import static org.junit.Assert.*;
Ray Milkey9f74c082015-02-11 15:40:16 -080065import static org.onlab.junit.TestTools.assertAfter;
Brian O'Connor427a1762014-11-19 18:40:32 -080066import static org.onlab.util.Tools.delay;
Brian O'Connorb499b352015-02-03 16:46:15 -080067import static org.onosproject.net.intent.IntentState.*;
Brian O'Connor427a1762014-11-19 18:40:32 -080068
69/**
70 * Test intent manager and transitions.
71 *
72 * TODO implement the following tests:
73 * - {submit, withdraw, update, replace} intent
Sho SHIMIZU34660962015-01-22 17:58:44 -080074 * - {submit, update, recompiling} intent with failed compilation
Brian O'Connor427a1762014-11-19 18:40:32 -080075 * - failed reservation
76 * - push timeout recovery
77 * - failed items recovery
78 *
79 * in general, verify intents store, flow store, and work queue
80 */
Jonathan Hart4fd4ebb2015-02-04 17:38:48 -080081@Ignore
Brian O'Connor427a1762014-11-19 18:40:32 -080082public class IntentManagerTest {
83
84 private static final ApplicationId APPID = new TestApplicationId("manager-test");
85
86 private IntentManager manager;
87 private MockFlowRuleService flowRuleService;
88
89 protected IntentService service;
90 protected IntentExtensionService extensionService;
91 protected TestListener listener = new TestListener();
92 protected TestIntentCompiler compiler = new TestIntentCompiler();
93 protected TestIntentInstaller installer = new TestIntentInstaller();
94
Ray Milkeye9a3e222014-12-03 16:46:06 -080095 private static class TestListener implements IntentListener {
96 final Multimap<IntentEvent.Type, IntentEvent> events = HashMultimap.create();
97 Map<IntentEvent.Type, CountDownLatch> latchMap = Maps.newHashMap();
98
99 @Override
100 public void event(IntentEvent event) {
101 events.put(event.type(), event);
102 if (latchMap.containsKey(event.type())) {
103 latchMap.get(event.type()).countDown();
104 }
105 }
106
107 public int getCounts(IntentEvent.Type type) {
108 return events.get(type).size();
109 }
110
111 public void setLatch(int count, IntentEvent.Type type) {
112 latchMap.put(type, new CountDownLatch(count));
113 }
114
115 public void await(IntentEvent.Type type) {
116 try {
117 assertTrue("Timed out waiting for: " + type,
118 latchMap.get(type).await(5, TimeUnit.SECONDS));
119 } catch (InterruptedException e) {
120 e.printStackTrace();
121 }
122 }
123 }
124
125 private static class TestIntentTracker implements ObjectiveTrackerService {
126 private TopologyChangeDelegate delegate;
127 @Override
128 public void setDelegate(TopologyChangeDelegate delegate) {
129 this.delegate = delegate;
130 }
131
132 @Override
133 public void unsetDelegate(TopologyChangeDelegate delegate) {
134 if (delegate.equals(this.delegate)) {
135 this.delegate = null;
136 }
137 }
138
139 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800140 public void addTrackedResources(Key key, Collection<NetworkResource> resources) {
Ray Milkeye9a3e222014-12-03 16:46:06 -0800141 //TODO
142 }
143
144 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800145 public void removeTrackedResources(Key key, Collection<NetworkResource> resources) {
Ray Milkeye9a3e222014-12-03 16:46:06 -0800146 //TODO
147 }
148 }
149
150 private static class MockIntent extends Intent {
151 private static AtomicLong counter = new AtomicLong(0);
152
153 private final Long number;
154 // Nothing new here
155 public MockIntent(Long number) {
Sho SHIMIZUd7d18002015-01-21 14:37:14 -0800156 super(APPID, Collections.emptyList());
Ray Milkeye9a3e222014-12-03 16:46:06 -0800157 this.number = number;
158 }
159
160 public Long number() {
161 return number;
162 }
163
164 public static Long nextId() {
165 return counter.getAndIncrement();
166 }
167 }
168
169 private static class MockInstallableIntent extends MockIntent {
170 public MockInstallableIntent(Long number) {
171 super(number);
172 }
173
174 @Override
175 public boolean isInstallable() {
176 return true;
177 }
178 }
179
180 private static class TestIntentCompiler implements IntentCompiler<MockIntent> {
181 @Override
182 public List<Intent> compile(MockIntent intent, List<Intent> installable,
183 Set<LinkResourceAllocations> resources) {
184 return Lists.newArrayList(new MockInstallableIntent(intent.number()));
185 }
186 }
187
188 private static class TestIntentCompilerError implements IntentCompiler<MockIntent> {
189 @Override
190 public List<Intent> compile(MockIntent intent, List<Intent> installable,
191 Set<LinkResourceAllocations> resources) {
192 throw new IntentCompilationException("Compilation always fails");
193 }
194 }
195
196 private static class TestIntentInstaller implements IntentInstaller<MockInstallableIntent> {
197 @Override
198 public List<FlowRuleBatchOperation> install(MockInstallableIntent intent) {
199 FlowRule fr = new IntentTestsMocks.MockFlowRule(intent.number().intValue());
200 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
201 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, fr));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800202 return Lists.newArrayList(new FlowRuleBatchOperation(rules, fr.deviceId(), 0));
Ray Milkeye9a3e222014-12-03 16:46:06 -0800203 }
204
205 @Override
206 public List<FlowRuleBatchOperation> uninstall(MockInstallableIntent intent) {
207 FlowRule fr = new IntentTestsMocks.MockFlowRule(intent.number().intValue());
208 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
209 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, fr));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800210 return Lists.newArrayList(new FlowRuleBatchOperation(rules, fr.deviceId(), 0));
Ray Milkeye9a3e222014-12-03 16:46:06 -0800211 }
212
213 @Override
214 public List<FlowRuleBatchOperation> replace(MockInstallableIntent oldIntent, MockInstallableIntent newIntent) {
215 FlowRule fr = new IntentTestsMocks.MockFlowRule(oldIntent.number().intValue());
216 FlowRule fr2 = new IntentTestsMocks.MockFlowRule(newIntent.number().intValue());
217 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
218 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, fr));
219 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, fr2));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800220 return Lists.newArrayList(new FlowRuleBatchOperation(rules, fr.deviceId(), 0));
Ray Milkeye9a3e222014-12-03 16:46:06 -0800221 }
222 }
223
224 private static class TestIntentErrorInstaller implements IntentInstaller<MockInstallableIntent> {
225 @Override
226 public List<FlowRuleBatchOperation> install(MockInstallableIntent intent) {
227 throw new IntentInstallationException("install() always fails");
228 }
229
230 @Override
231 public List<FlowRuleBatchOperation> uninstall(MockInstallableIntent intent) {
232 throw new IntentRemovalException("uninstall() always fails");
233 }
234
235 @Override
236 public List<FlowRuleBatchOperation> replace(MockInstallableIntent oldIntent, MockInstallableIntent newIntent) {
237 throw new IntentInstallationException("replace() always fails");
238 }
239 }
240
241 /**
242 * Hamcrest matcher to check that a conllection of Intents contains an
243 * Intent with the specified Intent Id.
244 */
245 public static class EntryForIntentMatcher extends TypeSafeMatcher<Collection<Intent>> {
246 private final IntentId id;
247
248 public EntryForIntentMatcher(IntentId idValue) {
249 id = idValue;
250 }
251
252 @Override
253 public boolean matchesSafely(Collection<Intent> intents) {
254 for (Intent intent : intents) {
255 if (intent.id().equals(id)) {
256 return true;
257 }
258 }
259 return false;
260 }
261
262 @Override
263 public void describeTo(Description description) {
264 description.appendText("an intent with id \" ").
265 appendText(id.toString()).
266 appendText("\"");
267 }
268 }
269
270 private static EntryForIntentMatcher hasIntentWithId(IntentId id) {
271 return new EntryForIntentMatcher(id);
272 }
273
Brian O'Connor427a1762014-11-19 18:40:32 -0800274 @Before
275 public void setUp() {
276 manager = new IntentManager();
277 flowRuleService = new MockFlowRuleService();
278 manager.store = new SimpleIntentStore();
Brian O'Connor427a1762014-11-19 18:40:32 -0800279 manager.eventDispatcher = new TestEventDispatcher();
280 manager.trackerService = new TestIntentTracker();
281 manager.flowRuleService = flowRuleService;
Brian O'Connor520c0522014-11-23 23:50:47 -0800282 manager.coreService = new TestCoreManager();
Brian O'Connor427a1762014-11-19 18:40:32 -0800283 service = manager;
284 extensionService = manager;
285
286 manager.activate();
287 service.addListener(listener);
288 extensionService.registerCompiler(MockIntent.class, compiler);
289 extensionService.registerInstaller(MockInstallableIntent.class, installer);
290
291 assertTrue("store should be empty",
292 Sets.newHashSet(service.getIntents()).isEmpty());
293 assertEquals(0L, flowRuleService.getFlowRuleCount());
294 }
295
Ray Milkey9f74c082015-02-11 15:40:16 -0800296 public void verifyState() {
Brian O'Connor427a1762014-11-19 18:40:32 -0800297 // verify that all intents are parked and the batch operation is unblocked
298 Set<IntentState> parked = Sets.newHashSet(INSTALLED, WITHDRAWN, FAILED);
299 for (Intent i : service.getIntents()) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800300 IntentState state = service.getIntentState(i.key());
Brian O'Connor427a1762014-11-19 18:40:32 -0800301 assertTrue("Intent " + i.id() + " is in invalid state " + state,
302 parked.contains(state));
303 }
304 //the batch has not yet been removed when we receive the last event
305 // FIXME: this doesn't guarantee to avoid the race
Brian O'Connorb499b352015-02-03 16:46:15 -0800306
307 //FIXME
308// for (int tries = 0; tries < 10; tries++) {
309// if (manager.batchService.getPendingOperations().isEmpty()) {
310// break;
311// }
312// delay(10);
313// }
314// assertTrue("There are still pending batch operations.",
315// manager.batchService.getPendingOperations().isEmpty());
Brian O'Connor427a1762014-11-19 18:40:32 -0800316
Ray Milkey9f74c082015-02-11 15:40:16 -0800317 }
318
319 @After
320 public void tearDown() {
Brian O'Connor427a1762014-11-19 18:40:32 -0800321 extensionService.unregisterCompiler(MockIntent.class);
322 extensionService.unregisterInstaller(MockInstallableIntent.class);
323 service.removeListener(listener);
324 manager.deactivate();
325 // TODO null the other refs?
326 }
327
328 @Test
329 public void submitIntent() {
330 flowRuleService.setFuture(true);
331
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800332 listener.setLatch(1, Type.INSTALL_REQ);
Brian O'Connor427a1762014-11-19 18:40:32 -0800333 listener.setLatch(1, Type.INSTALLED);
334 Intent intent = new MockIntent(MockIntent.nextId());
335 service.submit(intent);
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800336 listener.await(Type.INSTALL_REQ);
Brian O'Connor427a1762014-11-19 18:40:32 -0800337 listener.await(Type.INSTALLED);
338 assertEquals(1L, service.getIntentCount());
339 assertEquals(1L, flowRuleService.getFlowRuleCount());
Ray Milkey9f74c082015-02-11 15:40:16 -0800340 verifyState();
Brian O'Connor427a1762014-11-19 18:40:32 -0800341 }
342
343 @Test
344 public void withdrawIntent() {
345 flowRuleService.setFuture(true);
346
347 listener.setLatch(1, Type.INSTALLED);
348 Intent intent = new MockIntent(MockIntent.nextId());
349 service.submit(intent);
350 listener.await(Type.INSTALLED);
351 assertEquals(1L, service.getIntentCount());
352 assertEquals(1L, flowRuleService.getFlowRuleCount());
353
354 listener.setLatch(1, Type.WITHDRAWN);
355 service.withdraw(intent);
356 listener.await(Type.WITHDRAWN);
Brian O'Connor427a1762014-11-19 18:40:32 -0800357 assertEquals(0L, flowRuleService.getFlowRuleCount());
Ray Milkey9f74c082015-02-11 15:40:16 -0800358 verifyState();
Brian O'Connor427a1762014-11-19 18:40:32 -0800359 }
360
361 @Test
Ray Milkey9f74c082015-02-11 15:40:16 -0800362 public void stressSubmitWithdrawUnique() {
Brian O'Connor427a1762014-11-19 18:40:32 -0800363 flowRuleService.setFuture(true);
364
365 int count = 500;
Ray Milkey9f74c082015-02-11 15:40:16 -0800366 Intent[] intents = new Intent[count];
Brian O'Connor427a1762014-11-19 18:40:32 -0800367
Brian O'Connor427a1762014-11-19 18:40:32 -0800368 listener.setLatch(count, Type.WITHDRAWN);
369
Ray Milkey9f74c082015-02-11 15:40:16 -0800370 for (int i = 0; i < count; i++) {
371 intents[i] = new MockIntent(MockIntent.nextId());
372 service.submit(intents[i]);
373 }
374
375 for (int i = 0; i < count; i++) {
376 service.withdraw(intents[i]);
377 }
378
379 listener.await(Type.WITHDRAWN);
380 assertEquals(0L, flowRuleService.getFlowRuleCount());
381 verifyState();
382 }
383
384 @Test
385 public void stressSubmitWithdrawSame() {
386 flowRuleService.setFuture(true);
387
388 int count = 50;
389
Brian O'Connor427a1762014-11-19 18:40:32 -0800390 Intent intent = new MockIntent(MockIntent.nextId());
391 for (int i = 0; i < count; i++) {
392 service.submit(intent);
393 service.withdraw(intent);
394 }
395
Ray Milkey9f74c082015-02-11 15:40:16 -0800396 assertAfter(100, () -> {
397 assertEquals(1L, service.getIntentCount());
398 assertEquals(0L, flowRuleService.getFlowRuleCount());
399 });
400 verifyState();
Brian O'Connor427a1762014-11-19 18:40:32 -0800401 }
402
Ray Milkey9f74c082015-02-11 15:40:16 -0800403
Ray Milkey93508c22014-12-02 11:35:56 -0800404 /**
405 * Tests for proper behavior of installation of an intent that triggers
406 * a compilation error.
407 */
408 @Test
409 public void errorIntentCompile() {
410 final TestIntentCompilerError errorCompiler = new TestIntentCompilerError();
411 extensionService.registerCompiler(MockIntent.class, errorCompiler);
412 MockIntent intent = new MockIntent(MockIntent.nextId());
413 listener.setLatch(1, Type.INSTALL_REQ);
414 listener.setLatch(1, Type.FAILED);
415 service.submit(intent);
416 listener.await(Type.INSTALL_REQ);
417 listener.await(Type.FAILED);
Ray Milkey9f74c082015-02-11 15:40:16 -0800418 verifyState();
Ray Milkey93508c22014-12-02 11:35:56 -0800419 }
420
421 /**
422 * Tests handling a future that contains an error as a result of
423 * installing an intent.
424 */
Ray Milkey9f74c082015-02-11 15:40:16 -0800425 @Ignore("skipping until we fix update ordering problem")
Ray Milkey93508c22014-12-02 11:35:56 -0800426 @Test
427 public void errorIntentInstallFromFlows() {
428 final Long id = MockIntent.nextId();
Brian O'Connor5811ac22015-02-09 19:17:07 -0800429 flowRuleService.setFuture(false);
Ray Milkey93508c22014-12-02 11:35:56 -0800430 MockIntent intent = new MockIntent(id);
431 listener.setLatch(1, Type.FAILED);
432 listener.setLatch(1, Type.INSTALL_REQ);
433 service.submit(intent);
434 listener.await(Type.INSTALL_REQ);
Ray Milkey93508c22014-12-02 11:35:56 -0800435 listener.await(Type.FAILED);
Ray Milkey9f74c082015-02-11 15:40:16 -0800436 // FIXME the intent will be moved into INSTALLED immediately which overrides FAILED
437 // ... the updates come out of order
438 verifyState();
Ray Milkey93508c22014-12-02 11:35:56 -0800439 }
440
441 /**
442 * Tests handling of an error that is generated by the intent installer.
443 */
444 @Test
445 public void errorIntentInstallFromInstaller() {
446 final TestIntentErrorInstaller errorInstaller = new TestIntentErrorInstaller();
447 extensionService.registerInstaller(MockInstallableIntent.class, errorInstaller);
448 MockIntent intent = new MockIntent(MockIntent.nextId());
449 listener.setLatch(1, Type.INSTALL_REQ);
450 listener.setLatch(1, Type.FAILED);
451 service.submit(intent);
452 listener.await(Type.INSTALL_REQ);
453 listener.await(Type.FAILED);
Ray Milkey9f74c082015-02-11 15:40:16 -0800454 verifyState();
Ray Milkey93508c22014-12-02 11:35:56 -0800455 }
456
Brian O'Connor427a1762014-11-19 18:40:32 -0800457 /**
Ray Milkeye9a3e222014-12-03 16:46:06 -0800458 * Tests handling a future that contains an unresolvable error as a result of
459 * installing an intent.
Brian O'Connor427a1762014-11-19 18:40:32 -0800460 */
Ray Milkey9f74c082015-02-11 15:40:16 -0800461 @Ignore("test needs to be rewritten, so that the intent is resubmitted")
Ray Milkeye9a3e222014-12-03 16:46:06 -0800462 @Test
463 public void errorIntentInstallNeverTrue() {
464 final Long id = MockIntent.nextId();
Brian O'Connor5811ac22015-02-09 19:17:07 -0800465 flowRuleService.setFuture(false);
Ray Milkeye9a3e222014-12-03 16:46:06 -0800466 MockIntent intent = new MockIntent(id);
Ray Milkey9f74c082015-02-11 15:40:16 -0800467 listener.setLatch(1, Type.FAILED);
Ray Milkeye9a3e222014-12-03 16:46:06 -0800468 listener.setLatch(1, Type.INSTALL_REQ);
469 service.submit(intent);
470 listener.await(Type.INSTALL_REQ);
471 // The delay here forces the retry loop in the intent manager to time out
472 delay(100);
Brian O'Connor5811ac22015-02-09 19:17:07 -0800473 flowRuleService.setFuture(false);
Ray Milkeye9a3e222014-12-03 16:46:06 -0800474 service.withdraw(intent);
Ray Milkey9f74c082015-02-11 15:40:16 -0800475 listener.await(Type.FAILED);
476 verifyState();
Ray Milkeye9a3e222014-12-03 16:46:06 -0800477 }
Brian O'Connor427a1762014-11-19 18:40:32 -0800478
Ray Milkeye9a3e222014-12-03 16:46:06 -0800479 /**
480 * Tests that a compiler for a subclass of an intent that already has a
481 * compiler is automatically added.
482 */
483 @Test
484 public void intentSubclassCompile() {
485 class MockIntentSubclass extends MockIntent {
486 public MockIntentSubclass(Long number) {
487 super(number);
488 }
489 }
490 flowRuleService.setFuture(true);
491
492 listener.setLatch(1, Type.INSTALL_REQ);
493 listener.setLatch(1, Type.INSTALLED);
494 Intent intent = new MockIntentSubclass(MockIntent.nextId());
495 service.submit(intent);
496 listener.await(Type.INSTALL_REQ);
497 listener.await(Type.INSTALLED);
498 assertEquals(1L, service.getIntentCount());
499 assertEquals(1L, flowRuleService.getFlowRuleCount());
500
501 final Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> compilers =
502 extensionService.getCompilers();
503 assertEquals(2, compilers.size());
504 assertNotNull(compilers.get(MockIntentSubclass.class));
505 assertNotNull(compilers.get(MockIntent.class));
Ray Milkey9f74c082015-02-11 15:40:16 -0800506 verifyState();
Ray Milkeye9a3e222014-12-03 16:46:06 -0800507 }
508
509 /**
510 * Tests an intent with no compiler.
511 */
512 @Test
513 public void intentWithoutCompiler() {
514 class IntentNoCompiler extends Intent {
515 IntentNoCompiler() {
Sho SHIMIZUd7d18002015-01-21 14:37:14 -0800516 super(APPID, Collections.emptyList());
Ray Milkeye9a3e222014-12-03 16:46:06 -0800517 }
Brian O'Connor427a1762014-11-19 18:40:32 -0800518 }
519
Ray Milkeye9a3e222014-12-03 16:46:06 -0800520 Intent intent = new IntentNoCompiler();
521 listener.setLatch(1, Type.INSTALL_REQ);
522 listener.setLatch(1, Type.FAILED);
523 service.submit(intent);
524 listener.await(Type.INSTALL_REQ);
525 listener.await(Type.FAILED);
Ray Milkey9f74c082015-02-11 15:40:16 -0800526 verifyState();
Ray Milkeye9a3e222014-12-03 16:46:06 -0800527 }
Brian O'Connor427a1762014-11-19 18:40:32 -0800528
Ray Milkeye9a3e222014-12-03 16:46:06 -0800529 /**
530 * Tests an intent with no installer.
531 */
532 @Test
533 public void intentWithoutInstaller() {
534
535 extensionService.unregisterInstaller(MockInstallableIntent.class);
536
537 MockIntent intent = new MockIntent(MockIntent.nextId());
538 listener.setLatch(1, Type.INSTALL_REQ);
539 listener.setLatch(1, Type.FAILED);
540 service.submit(intent);
541 listener.await(Type.INSTALL_REQ);
542 listener.await(Type.FAILED);
Ray Milkey9f74c082015-02-11 15:40:16 -0800543 verifyState();
Ray Milkeye9a3e222014-12-03 16:46:06 -0800544 }
545
546 /**
547 * Tests that the intent fetching methods are correct.
548 */
549 @Test
550 public void testIntentFetching() {
551 List<Intent> intents;
552
553 flowRuleService.setFuture(true);
554
555 intents = Lists.newArrayList(service.getIntents());
556 assertThat(intents, hasSize(0));
557
558 final MockIntent intent1 = new MockIntent(MockIntent.nextId());
559 final MockIntent intent2 = new MockIntent(MockIntent.nextId());
560
561 listener.setLatch(2, Type.INSTALL_REQ);
562 listener.setLatch(2, Type.INSTALLED);
563 service.submit(intent1);
564 service.submit(intent2);
565 listener.await(Type.INSTALL_REQ);
566 listener.await(Type.INSTALL_REQ);
567 listener.await(Type.INSTALLED);
568 listener.await(Type.INSTALLED);
569
570 intents = Lists.newArrayList(service.getIntents());
571 assertThat(intents, hasSize(2));
572
573 assertThat(intents, hasIntentWithId(intent1.id()));
574 assertThat(intents, hasIntentWithId(intent2.id()));
Ray Milkey9f74c082015-02-11 15:40:16 -0800575 verifyState();
Brian O'Connor427a1762014-11-19 18:40:32 -0800576 }
577}