Remove yang-test
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / dependencyresolver / DependencyResolverManagerTest.java
1 /*
2  * Copyright (c) 2013, 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.manager.impl.dependencyresolver;
9
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;
15
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;
32
33 public class DependencyResolverManagerTest extends AbstractLockedPlatformMBeanServerTest {
34
35     final ModuleIdentifier apspName = new ModuleIdentifier("apsp", "apsp"); // depends
36                                                                             // on:
37     final ModuleIdentifier threadPoolName = new ModuleIdentifier("threadpool", "threadpool"); // depends on:
38     final ModuleIdentifier threadFactoryName = new ModuleIdentifier("threadfactory", "threadfactory");
39
40     private DependencyResolverManager tested;
41     TransactionStatus transactionStatus;
42
43     @Before
44     public void setUp() {
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();
51     }
52
53     @Test
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);
61
62         // set threadfactory as dependency of threadpool
63         declareDependency(threadPoolDRI, threadFactoryName);
64         // set threadpool as dependency of apsp
65         declareDependency(apspDRI, threadPoolName);
66
67         // switch to second phase committed
68         reset(transactionStatus);
69         doNothing().when(transactionStatus).checkCommitStarted();
70         doNothing().when(transactionStatus).checkCommitted();
71         doNothing().when(transactionStatus).checkNotCommitted();
72
73         List<ModuleIdentifier> sortedModuleIdentifiers = tested.getSortedModuleIdentifiers();
74         assertEquals(Arrays.asList(threadFactoryName, threadPoolName, apspName), sortedModuleIdentifiers);
75     }
76
77     /**
78      * Simulate dependentResolver resolving its dependency identified by
79      * dependentName.
80      */
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),
85                 dummyAttribute);
86     }
87
88     private static void mockGetInstance(final DependencyResolverManager tested,
89             final ModuleIdentifier moduleIdentifier) {
90
91         ModuleFactory moduleFactory = mock(ModuleFactory.class);
92         ModuleInternalInfo maybeOldInternalInfo = null;
93         TransactionModuleJMXRegistration transactionModuleJMXRegistration = null;
94         boolean isDefaultBean = false;
95
96         tested.put(moduleIdentifier, mockedModule(), moduleFactory, maybeOldInternalInfo,
97                 transactionModuleJMXRegistration, isDefaultBean, mock(BundleContext.class));
98     }
99
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();
104         return mockedModule;
105     }
106 }