2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.config.manager.impl.dependencyresolver;
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Mockito.doNothing;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.reset;
16 import java.util.Arrays;
17 import java.util.List;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.controller.config.api.JmxAttribute;
22 import org.opendaylight.controller.config.api.ModuleIdentifier;
23 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
24 import org.opendaylight.controller.config.manager.impl.ModuleInternalTransactionalInfo;
25 import org.opendaylight.controller.config.manager.impl.TransactionStatus;
26 import org.opendaylight.controller.config.spi.Module;
28 public class DependencyResolverManagerTest {
30 final ModuleIdentifier apspName = new ModuleIdentifier("apsp", "apsp"); // depends
32 final ModuleIdentifier threadPoolName = new ModuleIdentifier("threadpool",
33 "threadpool"); // depends on:
34 final ModuleIdentifier threadFactoryName = new ModuleIdentifier(
35 "threadfactory", "threadfactory");
37 private DependencyResolverManager tested;
38 TransactionStatus transactionStatus;
42 transactionStatus = mock(TransactionStatus.class);
43 tested = new DependencyResolverManager("txName", transactionStatus);
44 doNothing().when(transactionStatus).checkCommitStarted();
45 doNothing().when(transactionStatus).checkNotCommitted();
49 public void testOrdering() {
50 DependencyResolverImpl apspDRI = tested.getOrCreate(apspName);
51 mockGetInstance(tested, apspName);
52 DependencyResolverImpl threadPoolDRI = tested
53 .getOrCreate(threadPoolName);
54 mockGetInstance(tested, threadPoolName);
55 tested.getOrCreate(threadFactoryName);
56 mockGetInstance(tested, threadFactoryName);
58 // set threadfactory as dependency of threadpool
59 declareDependency(threadPoolDRI, threadFactoryName);
60 // set threadpool as dependency of apsp
61 declareDependency(apspDRI, threadPoolName);
63 // switch to second phase committed
64 reset(transactionStatus);
65 doNothing().when(transactionStatus).checkCommitted();
66 List<ModuleIdentifier> sortedModuleIdentifiers = tested
67 .getSortedModuleIdentifiers();
69 Arrays.asList(threadFactoryName, threadPoolName, apspName),
70 sortedModuleIdentifiers);
75 * Simulate dependentResolver resolving its dependency identified by
78 private void declareDependency(DependencyResolverImpl dependerResolver,
79 ModuleIdentifier dependentName) {
80 JmxAttribute dummyAttribute = new JmxAttribute("dummy");
81 dependerResolver.resolveInstance(Object.class,
82 ObjectNameUtil.createReadOnlyModuleON(dependentName),
86 private static void mockGetInstance(DependencyResolverManager tested,
87 ModuleIdentifier moduleIdentifier) {
88 ModuleInternalTransactionalInfo mock = mock(ModuleInternalTransactionalInfo.class);
89 doReturn(moduleIdentifier).when(mock).getIdentifier();
90 doReturn(mockedModule()).when(mock).getModule();
94 private static Module mockedModule() {
95 Module mockedModule = mock(Module.class);
96 doReturn(mock(AutoCloseable.class)).when(mockedModule).getInstance();