blob: 4be32c4f2722439a2c7a8641675b37928e50b83b [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
Ray Milkey43a28222015-02-23 13:57:58 -080018import java.util.Collection;
19import java.util.Collections;
20import java.util.List;
21import java.util.Map;
22import java.util.Set;
23import java.util.concurrent.CountDownLatch;
24import java.util.concurrent.TimeUnit;
25
Brian O'Connor427a1762014-11-19 18:40:32 -080026import org.hamcrest.Description;
Brian O'Connor427a1762014-11-19 18:40:32 -080027import org.hamcrest.TypeSafeMatcher;
28import org.junit.After;
29import org.junit.Before;
Jonathan Hart4fd4ebb2015-02-04 17:38:48 -080030import org.junit.Ignore;
Brian O'Connor427a1762014-11-19 18:40:32 -080031import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.TestApplicationId;
33import org.onosproject.core.ApplicationId;
34import org.onosproject.core.impl.TestCoreManager;
35import org.onosproject.event.impl.TestEventDispatcher;
36import org.onosproject.net.NetworkResource;
37import org.onosproject.net.flow.FlowRule;
Ray Milkey71ade562015-02-18 15:08:07 -080038import org.onosproject.net.flow.FlowRuleOperation;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.intent.Intent;
40import org.onosproject.net.intent.IntentCompiler;
41import org.onosproject.net.intent.IntentEvent;
42import org.onosproject.net.intent.IntentEvent.Type;
43import org.onosproject.net.intent.IntentExtensionService;
44import org.onosproject.net.intent.IntentId;
45import org.onosproject.net.intent.IntentInstaller;
46import org.onosproject.net.intent.IntentListener;
47import org.onosproject.net.intent.IntentService;
48import org.onosproject.net.intent.IntentState;
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
Ray Milkey43a28222015-02-23 13:57:58 -080053import com.google.common.collect.HashMultimap;
54import com.google.common.collect.ImmutableSet;
55import com.google.common.collect.Lists;
56import com.google.common.collect.Maps;
57import com.google.common.collect.Multimap;
58import com.google.common.collect.Sets;
Brian O'Connor427a1762014-11-19 18:40:32 -080059
Ray Milkeye9a3e222014-12-03 16:46:06 -080060import static org.hamcrest.MatcherAssert.assertThat;
61import static org.hamcrest.Matchers.hasSize;
Ray Milkey43a28222015-02-23 13:57:58 -080062import static org.junit.Assert.assertEquals;
63import static org.junit.Assert.assertNotNull;
64import static org.junit.Assert.assertTrue;
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;
Ray Milkey43a28222015-02-23 13:57:58 -080067import static org.onosproject.net.intent.IntentState.FAILED;
68import static org.onosproject.net.intent.IntentState.INSTALLED;
69import static org.onosproject.net.intent.IntentState.WITHDRAWN;
70import static org.onosproject.net.intent.IntentTestsMocks.MockFlowRule;
71import static org.onosproject.net.intent.IntentTestsMocks.MockIntent;
Brian O'Connor427a1762014-11-19 18:40:32 -080072
73/**
74 * Test intent manager and transitions.
75 *
76 * TODO implement the following tests:
77 * - {submit, withdraw, update, replace} intent
Sho SHIMIZU34660962015-01-22 17:58:44 -080078 * - {submit, update, recompiling} intent with failed compilation
Brian O'Connor427a1762014-11-19 18:40:32 -080079 * - failed reservation
80 * - push timeout recovery
81 * - failed items recovery
82 *
83 * in general, verify intents store, flow store, and work queue
84 */
Jonathan Hart4fd4ebb2015-02-04 17:38:48 -080085@Ignore
Brian O'Connor427a1762014-11-19 18:40:32 -080086public class IntentManagerTest {
87
88 private static final ApplicationId APPID = new TestApplicationId("manager-test");
89
90 private IntentManager manager;
91 private MockFlowRuleService flowRuleService;
92
93 protected IntentService service;
94 protected IntentExtensionService extensionService;
95 protected TestListener listener = new TestListener();
96 protected TestIntentCompiler compiler = new TestIntentCompiler();
97 protected TestIntentInstaller installer = new TestIntentInstaller();
98
Ray Milkeye9a3e222014-12-03 16:46:06 -080099 private static class TestListener implements IntentListener {
100 final Multimap<IntentEvent.Type, IntentEvent> events = HashMultimap.create();
101 Map<IntentEvent.Type, CountDownLatch> latchMap = Maps.newHashMap();
102
103 @Override
104 public void event(IntentEvent event) {
105 events.put(event.type(), event);
106 if (latchMap.containsKey(event.type())) {
107 latchMap.get(event.type()).countDown();
108 }
109 }
110
111 public int getCounts(IntentEvent.Type type) {
112 return events.get(type).size();
113 }
114
115 public void setLatch(int count, IntentEvent.Type type) {
116 latchMap.put(type, new CountDownLatch(count));
117 }
118
119 public void await(IntentEvent.Type type) {
120 try {
121 assertTrue("Timed out waiting for: " + type,
122 latchMap.get(type).await(5, TimeUnit.SECONDS));
123 } catch (InterruptedException e) {
124 e.printStackTrace();
125 }
126 }
127 }
128
129 private static class TestIntentTracker implements ObjectiveTrackerService {
130 private TopologyChangeDelegate delegate;
131 @Override
132 public void setDelegate(TopologyChangeDelegate delegate) {
133 this.delegate = delegate;
134 }
135
136 @Override
137 public void unsetDelegate(TopologyChangeDelegate delegate) {
138 if (delegate.equals(this.delegate)) {
139 this.delegate = null;
140 }
141 }
142
143 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800144 public void addTrackedResources(Key key, Collection<NetworkResource> resources) {
Ray Milkeye9a3e222014-12-03 16:46:06 -0800145 //TODO
146 }
147
148 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800149 public void removeTrackedResources(Key key, Collection<NetworkResource> resources) {
Ray Milkeye9a3e222014-12-03 16:46:06 -0800150 //TODO
151 }
152 }
153
Ray Milkeye9a3e222014-12-03 16:46:06 -0800154 private static class MockInstallableIntent extends MockIntent {
155 public MockInstallableIntent(Long number) {
156 super(number);
157 }
158
159 @Override
160 public boolean isInstallable() {
161 return true;
162 }
163 }
164
165 private static class TestIntentCompiler implements IntentCompiler<MockIntent> {
166 @Override
167 public List<Intent> compile(MockIntent intent, List<Intent> installable,
168 Set<LinkResourceAllocations> resources) {
169 return Lists.newArrayList(new MockInstallableIntent(intent.number()));
170 }
171 }
172
173 private static class TestIntentCompilerError implements IntentCompiler<MockIntent> {
174 @Override
175 public List<Intent> compile(MockIntent intent, List<Intent> installable,
176 Set<LinkResourceAllocations> resources) {
177 throw new IntentCompilationException("Compilation always fails");
178 }
179 }
180
181 private static class TestIntentInstaller implements IntentInstaller<MockInstallableIntent> {
182 @Override
Brian O'Connor64a0369d2015-02-20 22:02:59 -0800183 public List<Collection<org.onosproject.net.flow.FlowRuleOperation>> install(MockInstallableIntent intent) {
Ray Milkey43a28222015-02-23 13:57:58 -0800184 FlowRule fr = new MockFlowRule(intent.number().intValue());
Ray Milkey71ade562015-02-18 15:08:07 -0800185 Set<FlowRuleOperation> rules = ImmutableSet.of(
186 new FlowRuleOperation(fr, FlowRuleOperation.Type.ADD));
187 return Lists.newArrayList(ImmutableSet.of(rules));
Ray Milkeye9a3e222014-12-03 16:46:06 -0800188 }
189
190 @Override
Brian O'Connor64a0369d2015-02-20 22:02:59 -0800191 public List<Collection<FlowRuleOperation>> uninstall(MockInstallableIntent intent) {
Ray Milkey43a28222015-02-23 13:57:58 -0800192 FlowRule fr = new MockFlowRule(intent.number().intValue());
Ray Milkey71ade562015-02-18 15:08:07 -0800193 Set<FlowRuleOperation> rules = ImmutableSet.of(
194 new FlowRuleOperation(fr, FlowRuleOperation.Type.REMOVE));
195 return Lists.newArrayList(ImmutableSet.of(rules));
Ray Milkeye9a3e222014-12-03 16:46:06 -0800196 }
197
198 @Override
Brian O'Connor64a0369d2015-02-20 22:02:59 -0800199 public List<Collection<FlowRuleOperation>> replace(MockInstallableIntent oldIntent,
200 MockInstallableIntent newIntent) {
Ray Milkey43a28222015-02-23 13:57:58 -0800201 FlowRule fr = new MockFlowRule(oldIntent.number().intValue());
202 FlowRule fr2 = new MockFlowRule(newIntent.number().intValue());
Ray Milkey71ade562015-02-18 15:08:07 -0800203 Set<FlowRuleOperation> rules = ImmutableSet.of(
204 new FlowRuleOperation(fr, FlowRuleOperation.Type.REMOVE),
205 new FlowRuleOperation(fr2, FlowRuleOperation.Type.ADD));
206 return Lists.newArrayList(ImmutableSet.of(rules));
Ray Milkeye9a3e222014-12-03 16:46:06 -0800207 }
208 }
209
210 private static class TestIntentErrorInstaller implements IntentInstaller<MockInstallableIntent> {
211 @Override
Brian O'Connor64a0369d2015-02-20 22:02:59 -0800212 public List<Collection<FlowRuleOperation>> install(MockInstallableIntent intent) {
Ray Milkeye9a3e222014-12-03 16:46:06 -0800213 throw new IntentInstallationException("install() always fails");
214 }
215
216 @Override
Brian O'Connor64a0369d2015-02-20 22:02:59 -0800217 public List<Collection<FlowRuleOperation>> uninstall(MockInstallableIntent intent) {
Ray Milkeye9a3e222014-12-03 16:46:06 -0800218 throw new IntentRemovalException("uninstall() always fails");
219 }
220
221 @Override
Brian O'Connor64a0369d2015-02-20 22:02:59 -0800222 public List<Collection<FlowRuleOperation>> replace(MockInstallableIntent oldIntent,
223 MockInstallableIntent newIntent) {
Ray Milkeye9a3e222014-12-03 16:46:06 -0800224 throw new IntentInstallationException("replace() always fails");
225 }
226 }
227
228 /**
229 * Hamcrest matcher to check that a conllection of Intents contains an
230 * Intent with the specified Intent Id.
231 */
232 public static class EntryForIntentMatcher extends TypeSafeMatcher<Collection<Intent>> {
233 private final IntentId id;
234
235 public EntryForIntentMatcher(IntentId idValue) {
236 id = idValue;
237 }
238
239 @Override
240 public boolean matchesSafely(Collection<Intent> intents) {
241 for (Intent intent : intents) {
242 if (intent.id().equals(id)) {
243 return true;
244 }
245 }
246 return false;
247 }
248
249 @Override
250 public void describeTo(Description description) {
251 description.appendText("an intent with id \" ").
252 appendText(id.toString()).
253 appendText("\"");
254 }
255 }
256
257 private static EntryForIntentMatcher hasIntentWithId(IntentId id) {
258 return new EntryForIntentMatcher(id);
259 }
260
Brian O'Connor427a1762014-11-19 18:40:32 -0800261 @Before
262 public void setUp() {
263 manager = new IntentManager();
264 flowRuleService = new MockFlowRuleService();
265 manager.store = new SimpleIntentStore();
Brian O'Connor427a1762014-11-19 18:40:32 -0800266 manager.eventDispatcher = new TestEventDispatcher();
267 manager.trackerService = new TestIntentTracker();
268 manager.flowRuleService = flowRuleService;
Brian O'Connor520c0522014-11-23 23:50:47 -0800269 manager.coreService = new TestCoreManager();
Brian O'Connor427a1762014-11-19 18:40:32 -0800270 service = manager;
271 extensionService = manager;
272
273 manager.activate();
274 service.addListener(listener);
275 extensionService.registerCompiler(MockIntent.class, compiler);
276 extensionService.registerInstaller(MockInstallableIntent.class, installer);
277
278 assertTrue("store should be empty",
279 Sets.newHashSet(service.getIntents()).isEmpty());
280 assertEquals(0L, flowRuleService.getFlowRuleCount());
281 }
282
Ray Milkey9f74c082015-02-11 15:40:16 -0800283 public void verifyState() {
Brian O'Connor427a1762014-11-19 18:40:32 -0800284 // verify that all intents are parked and the batch operation is unblocked
285 Set<IntentState> parked = Sets.newHashSet(INSTALLED, WITHDRAWN, FAILED);
286 for (Intent i : service.getIntents()) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800287 IntentState state = service.getIntentState(i.key());
Brian O'Connor427a1762014-11-19 18:40:32 -0800288 assertTrue("Intent " + i.id() + " is in invalid state " + state,
289 parked.contains(state));
290 }
291 //the batch has not yet been removed when we receive the last event
292 // FIXME: this doesn't guarantee to avoid the race
Brian O'Connorb499b352015-02-03 16:46:15 -0800293
294 //FIXME
295// for (int tries = 0; tries < 10; tries++) {
296// if (manager.batchService.getPendingOperations().isEmpty()) {
297// break;
298// }
299// delay(10);
300// }
301// assertTrue("There are still pending batch operations.",
302// manager.batchService.getPendingOperations().isEmpty());
Brian O'Connor427a1762014-11-19 18:40:32 -0800303
Ray Milkey9f74c082015-02-11 15:40:16 -0800304 }
305
306 @After
307 public void tearDown() {
Brian O'Connor427a1762014-11-19 18:40:32 -0800308 extensionService.unregisterCompiler(MockIntent.class);
309 extensionService.unregisterInstaller(MockInstallableIntent.class);
310 service.removeListener(listener);
311 manager.deactivate();
312 // TODO null the other refs?
313 }
314
315 @Test
316 public void submitIntent() {
317 flowRuleService.setFuture(true);
318
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800319 listener.setLatch(1, Type.INSTALL_REQ);
Brian O'Connor427a1762014-11-19 18:40:32 -0800320 listener.setLatch(1, Type.INSTALLED);
321 Intent intent = new MockIntent(MockIntent.nextId());
322 service.submit(intent);
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800323 listener.await(Type.INSTALL_REQ);
Brian O'Connor427a1762014-11-19 18:40:32 -0800324 listener.await(Type.INSTALLED);
325 assertEquals(1L, service.getIntentCount());
326 assertEquals(1L, flowRuleService.getFlowRuleCount());
Ray Milkey9f74c082015-02-11 15:40:16 -0800327 verifyState();
Brian O'Connor427a1762014-11-19 18:40:32 -0800328 }
329
330 @Test
331 public void withdrawIntent() {
332 flowRuleService.setFuture(true);
333
334 listener.setLatch(1, Type.INSTALLED);
335 Intent intent = new MockIntent(MockIntent.nextId());
336 service.submit(intent);
337 listener.await(Type.INSTALLED);
338 assertEquals(1L, service.getIntentCount());
339 assertEquals(1L, flowRuleService.getFlowRuleCount());
340
341 listener.setLatch(1, Type.WITHDRAWN);
342 service.withdraw(intent);
343 listener.await(Type.WITHDRAWN);
Brian O'Connor427a1762014-11-19 18:40:32 -0800344 assertEquals(0L, flowRuleService.getFlowRuleCount());
Ray Milkey9f74c082015-02-11 15:40:16 -0800345 verifyState();
Brian O'Connor427a1762014-11-19 18:40:32 -0800346 }
347
348 @Test
Ray Milkey9f74c082015-02-11 15:40:16 -0800349 public void stressSubmitWithdrawUnique() {
Brian O'Connor427a1762014-11-19 18:40:32 -0800350 flowRuleService.setFuture(true);
351
352 int count = 500;
Ray Milkey9f74c082015-02-11 15:40:16 -0800353 Intent[] intents = new Intent[count];
Brian O'Connor427a1762014-11-19 18:40:32 -0800354
Brian O'Connor427a1762014-11-19 18:40:32 -0800355 listener.setLatch(count, Type.WITHDRAWN);
356
Ray Milkey9f74c082015-02-11 15:40:16 -0800357 for (int i = 0; i < count; i++) {
358 intents[i] = new MockIntent(MockIntent.nextId());
359 service.submit(intents[i]);
360 }
361
362 for (int i = 0; i < count; i++) {
363 service.withdraw(intents[i]);
364 }
365
366 listener.await(Type.WITHDRAWN);
367 assertEquals(0L, flowRuleService.getFlowRuleCount());
368 verifyState();
369 }
370
371 @Test
372 public void stressSubmitWithdrawSame() {
373 flowRuleService.setFuture(true);
374
375 int count = 50;
376
Brian O'Connor427a1762014-11-19 18:40:32 -0800377 Intent intent = new MockIntent(MockIntent.nextId());
378 for (int i = 0; i < count; i++) {
379 service.submit(intent);
380 service.withdraw(intent);
381 }
382
Ray Milkey9f74c082015-02-11 15:40:16 -0800383 assertAfter(100, () -> {
384 assertEquals(1L, service.getIntentCount());
385 assertEquals(0L, flowRuleService.getFlowRuleCount());
386 });
387 verifyState();
Brian O'Connor427a1762014-11-19 18:40:32 -0800388 }
389
Ray Milkey9f74c082015-02-11 15:40:16 -0800390
Ray Milkey93508c22014-12-02 11:35:56 -0800391 /**
392 * Tests for proper behavior of installation of an intent that triggers
393 * a compilation error.
394 */
395 @Test
396 public void errorIntentCompile() {
397 final TestIntentCompilerError errorCompiler = new TestIntentCompilerError();
398 extensionService.registerCompiler(MockIntent.class, errorCompiler);
399 MockIntent intent = new MockIntent(MockIntent.nextId());
400 listener.setLatch(1, Type.INSTALL_REQ);
401 listener.setLatch(1, Type.FAILED);
402 service.submit(intent);
403 listener.await(Type.INSTALL_REQ);
404 listener.await(Type.FAILED);
Ray Milkey9f74c082015-02-11 15:40:16 -0800405 verifyState();
Ray Milkey93508c22014-12-02 11:35:56 -0800406 }
407
408 /**
409 * Tests handling a future that contains an error as a result of
410 * installing an intent.
411 */
Ray Milkey9f74c082015-02-11 15:40:16 -0800412 @Ignore("skipping until we fix update ordering problem")
Ray Milkey93508c22014-12-02 11:35:56 -0800413 @Test
414 public void errorIntentInstallFromFlows() {
415 final Long id = MockIntent.nextId();
Brian O'Connor5811ac22015-02-09 19:17:07 -0800416 flowRuleService.setFuture(false);
Ray Milkey93508c22014-12-02 11:35:56 -0800417 MockIntent intent = new MockIntent(id);
418 listener.setLatch(1, Type.FAILED);
419 listener.setLatch(1, Type.INSTALL_REQ);
420 service.submit(intent);
421 listener.await(Type.INSTALL_REQ);
Ray Milkey93508c22014-12-02 11:35:56 -0800422 listener.await(Type.FAILED);
Ray Milkey9f74c082015-02-11 15:40:16 -0800423 // FIXME the intent will be moved into INSTALLED immediately which overrides FAILED
424 // ... the updates come out of order
425 verifyState();
Ray Milkey93508c22014-12-02 11:35:56 -0800426 }
427
428 /**
429 * Tests handling of an error that is generated by the intent installer.
430 */
431 @Test
432 public void errorIntentInstallFromInstaller() {
433 final TestIntentErrorInstaller errorInstaller = new TestIntentErrorInstaller();
434 extensionService.registerInstaller(MockInstallableIntent.class, errorInstaller);
435 MockIntent intent = new MockIntent(MockIntent.nextId());
436 listener.setLatch(1, Type.INSTALL_REQ);
437 listener.setLatch(1, Type.FAILED);
438 service.submit(intent);
439 listener.await(Type.INSTALL_REQ);
440 listener.await(Type.FAILED);
Ray Milkey9f74c082015-02-11 15:40:16 -0800441 verifyState();
Ray Milkey93508c22014-12-02 11:35:56 -0800442 }
443
Brian O'Connor427a1762014-11-19 18:40:32 -0800444 /**
Ray Milkeye9a3e222014-12-03 16:46:06 -0800445 * Tests handling a future that contains an unresolvable error as a result of
446 * installing an intent.
Brian O'Connor427a1762014-11-19 18:40:32 -0800447 */
Ray Milkey9f74c082015-02-11 15:40:16 -0800448 @Ignore("test needs to be rewritten, so that the intent is resubmitted")
Ray Milkeye9a3e222014-12-03 16:46:06 -0800449 @Test
450 public void errorIntentInstallNeverTrue() {
451 final Long id = MockIntent.nextId();
Brian O'Connor5811ac22015-02-09 19:17:07 -0800452 flowRuleService.setFuture(false);
Ray Milkeye9a3e222014-12-03 16:46:06 -0800453 MockIntent intent = new MockIntent(id);
Ray Milkey9f74c082015-02-11 15:40:16 -0800454 listener.setLatch(1, Type.FAILED);
Ray Milkeye9a3e222014-12-03 16:46:06 -0800455 listener.setLatch(1, Type.INSTALL_REQ);
456 service.submit(intent);
457 listener.await(Type.INSTALL_REQ);
458 // The delay here forces the retry loop in the intent manager to time out
459 delay(100);
Brian O'Connor5811ac22015-02-09 19:17:07 -0800460 flowRuleService.setFuture(false);
Ray Milkeye9a3e222014-12-03 16:46:06 -0800461 service.withdraw(intent);
Ray Milkey9f74c082015-02-11 15:40:16 -0800462 listener.await(Type.FAILED);
463 verifyState();
Ray Milkeye9a3e222014-12-03 16:46:06 -0800464 }
Brian O'Connor427a1762014-11-19 18:40:32 -0800465
Ray Milkeye9a3e222014-12-03 16:46:06 -0800466 /**
467 * Tests that a compiler for a subclass of an intent that already has a
468 * compiler is automatically added.
469 */
470 @Test
471 public void intentSubclassCompile() {
472 class MockIntentSubclass extends MockIntent {
473 public MockIntentSubclass(Long number) {
474 super(number);
475 }
476 }
477 flowRuleService.setFuture(true);
478
479 listener.setLatch(1, Type.INSTALL_REQ);
480 listener.setLatch(1, Type.INSTALLED);
481 Intent intent = new MockIntentSubclass(MockIntent.nextId());
482 service.submit(intent);
483 listener.await(Type.INSTALL_REQ);
484 listener.await(Type.INSTALLED);
485 assertEquals(1L, service.getIntentCount());
486 assertEquals(1L, flowRuleService.getFlowRuleCount());
487
488 final Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> compilers =
489 extensionService.getCompilers();
490 assertEquals(2, compilers.size());
491 assertNotNull(compilers.get(MockIntentSubclass.class));
492 assertNotNull(compilers.get(MockIntent.class));
Ray Milkey9f74c082015-02-11 15:40:16 -0800493 verifyState();
Ray Milkeye9a3e222014-12-03 16:46:06 -0800494 }
495
496 /**
497 * Tests an intent with no compiler.
498 */
499 @Test
500 public void intentWithoutCompiler() {
501 class IntentNoCompiler extends Intent {
502 IntentNoCompiler() {
Sho SHIMIZUd7d18002015-01-21 14:37:14 -0800503 super(APPID, Collections.emptyList());
Ray Milkeye9a3e222014-12-03 16:46:06 -0800504 }
Brian O'Connor427a1762014-11-19 18:40:32 -0800505 }
506
Ray Milkeye9a3e222014-12-03 16:46:06 -0800507 Intent intent = new IntentNoCompiler();
508 listener.setLatch(1, Type.INSTALL_REQ);
509 listener.setLatch(1, Type.FAILED);
510 service.submit(intent);
511 listener.await(Type.INSTALL_REQ);
512 listener.await(Type.FAILED);
Ray Milkey9f74c082015-02-11 15:40:16 -0800513 verifyState();
Ray Milkeye9a3e222014-12-03 16:46:06 -0800514 }
Brian O'Connor427a1762014-11-19 18:40:32 -0800515
Ray Milkeye9a3e222014-12-03 16:46:06 -0800516 /**
517 * Tests an intent with no installer.
518 */
519 @Test
520 public void intentWithoutInstaller() {
521
522 extensionService.unregisterInstaller(MockInstallableIntent.class);
523
524 MockIntent intent = new MockIntent(MockIntent.nextId());
525 listener.setLatch(1, Type.INSTALL_REQ);
526 listener.setLatch(1, Type.FAILED);
527 service.submit(intent);
528 listener.await(Type.INSTALL_REQ);
529 listener.await(Type.FAILED);
Ray Milkey9f74c082015-02-11 15:40:16 -0800530 verifyState();
Ray Milkeye9a3e222014-12-03 16:46:06 -0800531 }
532
533 /**
534 * Tests that the intent fetching methods are correct.
535 */
536 @Test
537 public void testIntentFetching() {
538 List<Intent> intents;
539
540 flowRuleService.setFuture(true);
541
542 intents = Lists.newArrayList(service.getIntents());
543 assertThat(intents, hasSize(0));
544
545 final MockIntent intent1 = new MockIntent(MockIntent.nextId());
546 final MockIntent intent2 = new MockIntent(MockIntent.nextId());
547
548 listener.setLatch(2, Type.INSTALL_REQ);
549 listener.setLatch(2, Type.INSTALLED);
550 service.submit(intent1);
551 service.submit(intent2);
552 listener.await(Type.INSTALL_REQ);
553 listener.await(Type.INSTALL_REQ);
554 listener.await(Type.INSTALLED);
555 listener.await(Type.INSTALLED);
556
557 intents = Lists.newArrayList(service.getIntents());
558 assertThat(intents, hasSize(2));
559
560 assertThat(intents, hasIntentWithId(intent1.id()));
561 assertThat(intents, hasIntentWithId(intent2.id()));
Ray Milkey9f74c082015-02-11 15:40:16 -0800562 verifyState();
Brian O'Connor427a1762014-11-19 18:40:32 -0800563 }
564}