Fix checkstyle warnings in opendaylight/config
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / dependencyresolver / DependencyResolverManagerTest.java
1 /*
2  * Copyright (c) 2013 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",
38             "threadpool"); // depends on:
39     final ModuleIdentifier threadFactoryName = new ModuleIdentifier(
40             "threadfactory", "threadfactory");
41
42     private DependencyResolverManager tested;
43     TransactionStatus transactionStatus;
44
45     @Before
46     public void setUp() {
47         transactionStatus = mock(TransactionStatus.class);
48         ServiceReferenceReadableRegistry mockedRegistry = mock(ServiceReferenceReadableRegistry.class);
49         tested = new DependencyResolverManager(new TransactionIdentifier("txName"), transactionStatus, mockedRegistry,
50                 null, platformMBeanServer);
51         doNothing().when(transactionStatus).checkCommitStarted();
52         doNothing().when(transactionStatus).checkNotCommitted();
53     }
54
55     @Test
56     public void testOrdering() {
57         DependencyResolverImpl apspDRI = tested.getOrCreate(apspName);
58         mockGetInstance(tested, apspName);
59         DependencyResolverImpl threadPoolDRI = tested
60                 .getOrCreate(threadPoolName);
61         mockGetInstance(tested, threadPoolName);
62         tested.getOrCreate(threadFactoryName);
63         mockGetInstance(tested, threadFactoryName);
64
65         // set threadfactory as dependency of threadpool
66         declareDependency(threadPoolDRI, threadFactoryName);
67         // set threadpool as dependency of apsp
68         declareDependency(apspDRI, threadPoolName);
69
70         // switch to second phase committed
71         reset(transactionStatus);
72         doNothing().when(transactionStatus).checkCommitted();
73         List<ModuleIdentifier> sortedModuleIdentifiers = tested
74                 .getSortedModuleIdentifiers();
75         assertEquals(
76                 Arrays.asList(threadFactoryName, threadPoolName, apspName),
77                 sortedModuleIdentifiers);
78
79     }
80
81     /**
82      * Simulate dependentResolver resolving its dependency identified by
83      * dependentName.
84      */
85     private void declareDependency(DependencyResolverImpl dependerResolver,
86             ModuleIdentifier dependentName) {
87         JmxAttribute dummyAttribute = new JmxAttribute("dummy");
88         dependerResolver.resolveInstance(Object.class,
89                 ObjectNameUtil.createReadOnlyModuleON(dependentName),
90                 dummyAttribute);
91     }
92
93     private static void mockGetInstance(DependencyResolverManager tested,
94             ModuleIdentifier moduleIdentifier) {
95
96         ModuleFactory moduleFactory = mock(ModuleFactory.class);
97         ModuleInternalInfo maybeOldInternalInfo = null;
98         TransactionModuleJMXRegistration transactionModuleJMXRegistration = null;
99         boolean isDefaultBean = false;
100
101         tested.put(moduleIdentifier,
102         mockedModule(),
103         moduleFactory,
104         maybeOldInternalInfo,
105         transactionModuleJMXRegistration,
106         isDefaultBean, mock(BundleContext.class));
107     }
108
109     private static Module mockedModule() {
110         Module mockedModule = mock(Module.class);
111         doReturn(mock(AutoCloseable.class)).when(mockedModule).getInstance();
112         return mockedModule;
113     }
114
115 }