Fix Eclipse warnings in config-manager
[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).checkCommitStarted();
73         doNothing().when(transactionStatus).checkCommitted();
74         doNothing().when(transactionStatus).checkNotCommitted();
75
76         List<ModuleIdentifier> sortedModuleIdentifiers = tested
77                 .getSortedModuleIdentifiers();
78         assertEquals(
79                 Arrays.asList(threadFactoryName, threadPoolName, apspName),
80                 sortedModuleIdentifiers);
81
82     }
83
84     /**
85      * Simulate dependentResolver resolving its dependency identified by
86      * dependentName.
87      */
88     private static void declareDependency(final DependencyResolverImpl dependerResolver,
89             final ModuleIdentifier dependentName) {
90         JmxAttribute dummyAttribute = new JmxAttribute("dummy");
91         dependerResolver.resolveInstance(Object.class,
92                 ObjectNameUtil.createReadOnlyModuleON(dependentName),
93                 dummyAttribute);
94     }
95
96     private static void mockGetInstance(final DependencyResolverManager tested,
97             final ModuleIdentifier moduleIdentifier) {
98
99         ModuleFactory moduleFactory = mock(ModuleFactory.class);
100         ModuleInternalInfo maybeOldInternalInfo = null;
101         TransactionModuleJMXRegistration transactionModuleJMXRegistration = null;
102         boolean isDefaultBean = false;
103
104         tested.put(moduleIdentifier,
105             mockedModule(),
106             moduleFactory,
107             maybeOldInternalInfo,
108             transactionModuleJMXRegistration,
109             isDefaultBean, mock(BundleContext.class));
110     }
111
112     private static Module mockedModule() {
113         Module mockedModule = mock(Module.class);
114         doReturn(mock(AutoCloseable.class)).when(mockedModule).getInstance();
115         doReturn(new ModuleIdentifier("fact", "instance")).when(mockedModule).getIdentifier();
116         return mockedModule;
117     }
118
119 }