2 * Copyright (c) 2013, 2017 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;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.controller.config.api.JmxAttribute;
21 import org.opendaylight.controller.config.api.ModuleIdentifier;
22 import org.opendaylight.controller.config.api.ServiceReferenceReadableRegistry;
23 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
24 import org.opendaylight.controller.config.manager.impl.AbstractLockedPlatformMBeanServerTest;
25 import org.opendaylight.controller.config.manager.impl.ModuleInternalInfo;
26 import org.opendaylight.controller.config.manager.impl.TransactionIdentifier;
27 import org.opendaylight.controller.config.manager.impl.TransactionStatus;
28 import org.opendaylight.controller.config.manager.impl.jmx.TransactionModuleJMXRegistrator.TransactionModuleJMXRegistration;
29 import org.opendaylight.controller.config.spi.Module;
30 import org.opendaylight.controller.config.spi.ModuleFactory;
31 import org.osgi.framework.BundleContext;
33 public class DependencyResolverManagerTest extends AbstractLockedPlatformMBeanServerTest {
35 final ModuleIdentifier apspName = new ModuleIdentifier("apsp", "apsp"); // depends
37 final ModuleIdentifier threadPoolName = new ModuleIdentifier("threadpool", "threadpool"); // depends on:
38 final ModuleIdentifier threadFactoryName = new ModuleIdentifier("threadfactory", "threadfactory");
40 private DependencyResolverManager tested;
41 TransactionStatus transactionStatus;
45 transactionStatus = mock(TransactionStatus.class);
46 ServiceReferenceReadableRegistry mockedRegistry = mock(ServiceReferenceReadableRegistry.class);
47 tested = new DependencyResolverManager(new TransactionIdentifier("txName"), transactionStatus, mockedRegistry,
48 null, platformMBeanServer);
49 doNothing().when(transactionStatus).checkCommitStarted();
50 doNothing().when(transactionStatus).checkNotCommitted();
54 public void testOrdering() {
55 final DependencyResolverImpl apspDRI = tested.getOrCreate(apspName);
56 mockGetInstance(tested, apspName);
57 final DependencyResolverImpl threadPoolDRI = tested.getOrCreate(threadPoolName);
58 mockGetInstance(tested, threadPoolName);
59 tested.getOrCreate(threadFactoryName);
60 mockGetInstance(tested, threadFactoryName);
62 // set threadfactory as dependency of threadpool
63 declareDependency(threadPoolDRI, threadFactoryName);
64 // set threadpool as dependency of apsp
65 declareDependency(apspDRI, threadPoolName);
67 // switch to second phase committed
68 reset(transactionStatus);
69 doNothing().when(transactionStatus).checkCommitStarted();
70 doNothing().when(transactionStatus).checkCommitted();
71 doNothing().when(transactionStatus).checkNotCommitted();
73 List<ModuleIdentifier> sortedModuleIdentifiers = tested.getSortedModuleIdentifiers();
74 assertEquals(Arrays.asList(threadFactoryName, threadPoolName, apspName), sortedModuleIdentifiers);
78 * Simulate dependentResolver resolving its dependency identified by
81 private static void declareDependency(final DependencyResolverImpl dependerResolver,
82 final ModuleIdentifier dependentName) {
83 JmxAttribute dummyAttribute = new JmxAttribute("dummy");
84 dependerResolver.resolveInstance(Object.class, ObjectNameUtil.createReadOnlyModuleON(dependentName),
88 private static void mockGetInstance(final DependencyResolverManager tested,
89 final ModuleIdentifier moduleIdentifier) {
91 ModuleFactory moduleFactory = mock(ModuleFactory.class);
92 ModuleInternalInfo maybeOldInternalInfo = null;
93 TransactionModuleJMXRegistration transactionModuleJMXRegistration = null;
94 boolean isDefaultBean = false;
96 tested.put(moduleIdentifier, mockedModule(), moduleFactory, maybeOldInternalInfo,
97 transactionModuleJMXRegistration, isDefaultBean, mock(BundleContext.class));
100 private static Module mockedModule() {
101 Module mockedModule = mock(Module.class);
102 doReturn(mock(AutoCloseable.class)).when(mockedModule).getInstance();
103 doReturn(new ModuleIdentifier("fact", "instance")).when(mockedModule).getIdentifier();