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 org.junit.Before;
11 import org.junit.Test;
12 import org.opendaylight.controller.config.api.JmxAttribute;
13 import org.opendaylight.controller.config.api.ModuleIdentifier;
14 import org.opendaylight.controller.config.api.ServiceReferenceReadableRegistry;
15 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
16 import org.opendaylight.controller.config.manager.impl.ModuleInternalInfo;
17 import org.opendaylight.controller.config.manager.impl.TransactionIdentifier;
18 import org.opendaylight.controller.config.manager.impl.TransactionStatus;
19 import org.opendaylight.controller.config.manager.impl.jmx.TransactionModuleJMXRegistrator.TransactionModuleJMXRegistration;
20 import org.opendaylight.controller.config.spi.Module;
21 import org.opendaylight.controller.config.spi.ModuleFactory;
23 import java.util.Arrays;
24 import java.util.List;
26 import static org.junit.Assert.assertEquals;
27 import static org.mockito.Mockito.doNothing;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.reset;
32 public class DependencyResolverManagerTest {
34 final ModuleIdentifier apspName = new ModuleIdentifier("apsp", "apsp"); // depends
36 final ModuleIdentifier threadPoolName = new ModuleIdentifier("threadpool",
37 "threadpool"); // depends on:
38 final ModuleIdentifier threadFactoryName = new ModuleIdentifier(
39 "threadfactory", "threadfactory");
41 private DependencyResolverManager tested;
42 TransactionStatus transactionStatus;
46 transactionStatus = mock(TransactionStatus.class);
47 ServiceReferenceReadableRegistry mockedRegistry = mock(ServiceReferenceReadableRegistry.class);
48 tested = new DependencyResolverManager(new TransactionIdentifier("txName"), transactionStatus, mockedRegistry, null);
49 doNothing().when(transactionStatus).checkCommitStarted();
50 doNothing().when(transactionStatus).checkNotCommitted();
54 public void testOrdering() {
55 DependencyResolverImpl apspDRI = tested.getOrCreate(apspName);
56 mockGetInstance(tested, apspName);
57 DependencyResolverImpl threadPoolDRI = tested
58 .getOrCreate(threadPoolName);
59 mockGetInstance(tested, threadPoolName);
60 tested.getOrCreate(threadFactoryName);
61 mockGetInstance(tested, threadFactoryName);
63 // set threadfactory as dependency of threadpool
64 declareDependency(threadPoolDRI, threadFactoryName);
65 // set threadpool as dependency of apsp
66 declareDependency(apspDRI, threadPoolName);
68 // switch to second phase committed
69 reset(transactionStatus);
70 doNothing().when(transactionStatus).checkCommitted();
71 List<ModuleIdentifier> sortedModuleIdentifiers = tested
72 .getSortedModuleIdentifiers();
74 Arrays.asList(threadFactoryName, threadPoolName, apspName),
75 sortedModuleIdentifiers);
80 * Simulate dependentResolver resolving its dependency identified by
83 private void declareDependency(DependencyResolverImpl dependerResolver,
84 ModuleIdentifier dependentName) {
85 JmxAttribute dummyAttribute = new JmxAttribute("dummy");
86 dependerResolver.resolveInstance(Object.class,
87 ObjectNameUtil.createReadOnlyModuleON(dependentName),
91 private static void mockGetInstance(DependencyResolverManager tested,
92 ModuleIdentifier moduleIdentifier) {
94 ModuleFactory moduleFactory = mock(ModuleFactory.class);
95 ModuleInternalInfo maybeOldInternalInfo = null;
96 TransactionModuleJMXRegistration transactionModuleJMXRegistration = null;
97 boolean isDefaultBean = false;
99 tested.put(moduleIdentifier,
102 maybeOldInternalInfo,
103 transactionModuleJMXRegistration,
107 private static Module mockedModule() {
108 Module mockedModule = mock(Module.class);
109 doReturn(mock(AutoCloseable.class)).when(mockedModule).getInstance();