Simplify method isMutualExclusive in Subnet.
[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
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.controller.config.api.JmxAttribute;
22 import org.opendaylight.controller.config.api.ModuleIdentifier;
23 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
24 import org.opendaylight.controller.config.manager.impl.ModuleInternalTransactionalInfo;
25 import org.opendaylight.controller.config.manager.impl.TransactionStatus;
26 import org.opendaylight.controller.config.spi.Module;
27
28 public class DependencyResolverManagerTest {
29
30     final ModuleIdentifier apspName = new ModuleIdentifier("apsp", "apsp"); // depends
31                                                                             // on:
32     final ModuleIdentifier threadPoolName = new ModuleIdentifier("threadpool",
33             "threadpool"); // depends on:
34     final ModuleIdentifier threadFactoryName = new ModuleIdentifier(
35             "threadfactory", "threadfactory");
36
37     private DependencyResolverManager tested;
38     TransactionStatus transactionStatus;
39
40     @Before
41     public void setUp() {
42         transactionStatus = mock(TransactionStatus.class);
43         tested = new DependencyResolverManager("txName", transactionStatus);
44         doNothing().when(transactionStatus).checkCommitStarted();
45         doNothing().when(transactionStatus).checkNotCommitted();
46     }
47
48     @Test
49     public void testOrdering() {
50         DependencyResolverImpl apspDRI = tested.getOrCreate(apspName);
51         mockGetInstance(tested, apspName);
52         DependencyResolverImpl threadPoolDRI = tested
53                 .getOrCreate(threadPoolName);
54         mockGetInstance(tested, threadPoolName);
55         tested.getOrCreate(threadFactoryName);
56         mockGetInstance(tested, threadFactoryName);
57
58         // set threadfactory as dependency of threadpool
59         declareDependency(threadPoolDRI, threadFactoryName);
60         // set threadpool as dependency of apsp
61         declareDependency(apspDRI, threadPoolName);
62
63         // switch to second phase committed
64         reset(transactionStatus);
65         doNothing().when(transactionStatus).checkCommitted();
66         List<ModuleIdentifier> sortedModuleIdentifiers = tested
67                 .getSortedModuleIdentifiers();
68         assertEquals(
69                 Arrays.asList(threadFactoryName, threadPoolName, apspName),
70                 sortedModuleIdentifiers);
71
72     }
73
74     /**
75      * Simulate dependentResolver resolving its dependency identified by
76      * dependentName.
77      */
78     private void declareDependency(DependencyResolverImpl dependerResolver,
79             ModuleIdentifier dependentName) {
80         JmxAttribute dummyAttribute = new JmxAttribute("dummy");
81         dependerResolver.resolveInstance(Object.class,
82                 ObjectNameUtil.createReadOnlyModuleON(dependentName),
83                 dummyAttribute);
84     }
85
86     private static void mockGetInstance(DependencyResolverManager tested,
87             ModuleIdentifier moduleIdentifier) {
88         ModuleInternalTransactionalInfo mock = mock(ModuleInternalTransactionalInfo.class);
89         doReturn(moduleIdentifier).when(mock).getIdentifier();
90         doReturn(mockedModule()).when(mock).getModule();
91         tested.put(mock);
92     }
93
94     private static Module mockedModule() {
95         Module mockedModule = mock(Module.class);
96         doReturn(mock(AutoCloseable.class)).when(mockedModule).getInstance();
97         return mockedModule;
98     }
99
100 }