Merge "Exception for URI /restconf/operations/module_name:rpc ended with slash"
[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 org.junit.Before;
11 import org.junit.Test;
12 import org.opendaylight.controller.config.api.JmxAttribute;
13 import org.opendaylight.controller.config.api.ModuleIdentifier;
14 import org.opendaylight.controller.config.api.ServiceReferenceReadableRegistry;
15 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
16 import org.opendaylight.controller.config.manager.impl.ModuleInternalInfo;
17 import org.opendaylight.controller.config.manager.impl.TransactionIdentifier;
18 import org.opendaylight.controller.config.manager.impl.TransactionStatus;
19 import org.opendaylight.controller.config.manager.impl.jmx.TransactionModuleJMXRegistrator.TransactionModuleJMXRegistration;
20 import org.opendaylight.controller.config.spi.Module;
21 import org.opendaylight.controller.config.spi.ModuleFactory;
22
23 import java.util.Arrays;
24 import java.util.List;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.mockito.Mockito.doNothing;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.reset;
31
32 public class DependencyResolverManagerTest {
33
34     final ModuleIdentifier apspName = new ModuleIdentifier("apsp", "apsp"); // depends
35                                                                             // on:
36     final ModuleIdentifier threadPoolName = new ModuleIdentifier("threadpool",
37             "threadpool"); // depends on:
38     final ModuleIdentifier threadFactoryName = new ModuleIdentifier(
39             "threadfactory", "threadfactory");
40
41     private DependencyResolverManager tested;
42     TransactionStatus transactionStatus;
43
44     @Before
45     public void setUp() {
46         transactionStatus = mock(TransactionStatus.class);
47         ServiceReferenceReadableRegistry mockedRegistry = mock(ServiceReferenceReadableRegistry.class);
48         tested = new DependencyResolverManager(new TransactionIdentifier("txName"), transactionStatus, mockedRegistry, null);
49         doNothing().when(transactionStatus).checkCommitStarted();
50         doNothing().when(transactionStatus).checkNotCommitted();
51     }
52
53     @Test
54     public void testOrdering() {
55         DependencyResolverImpl apspDRI = tested.getOrCreate(apspName);
56         mockGetInstance(tested, apspName);
57         DependencyResolverImpl threadPoolDRI = tested
58                 .getOrCreate(threadPoolName);
59         mockGetInstance(tested, threadPoolName);
60         tested.getOrCreate(threadFactoryName);
61         mockGetInstance(tested, threadFactoryName);
62
63         // set threadfactory as dependency of threadpool
64         declareDependency(threadPoolDRI, threadFactoryName);
65         // set threadpool as dependency of apsp
66         declareDependency(apspDRI, threadPoolName);
67
68         // switch to second phase committed
69         reset(transactionStatus);
70         doNothing().when(transactionStatus).checkCommitted();
71         List<ModuleIdentifier> sortedModuleIdentifiers = tested
72                 .getSortedModuleIdentifiers();
73         assertEquals(
74                 Arrays.asList(threadFactoryName, threadPoolName, apspName),
75                 sortedModuleIdentifiers);
76
77     }
78
79     /**
80      * Simulate dependentResolver resolving its dependency identified by
81      * dependentName.
82      */
83     private void declareDependency(DependencyResolverImpl dependerResolver,
84             ModuleIdentifier dependentName) {
85         JmxAttribute dummyAttribute = new JmxAttribute("dummy");
86         dependerResolver.resolveInstance(Object.class,
87                 ObjectNameUtil.createReadOnlyModuleON(dependentName),
88                 dummyAttribute);
89     }
90
91     private static void mockGetInstance(DependencyResolverManager tested,
92             ModuleIdentifier moduleIdentifier) {
93
94         ModuleFactory moduleFactory = mock(ModuleFactory.class);
95         ModuleInternalInfo maybeOldInternalInfo = null;
96         TransactionModuleJMXRegistration transactionModuleJMXRegistration = null;
97         boolean isDefaultBean = false;
98
99         tested.put(moduleIdentifier,
100         mockedModule(),
101          moduleFactory,
102          maybeOldInternalInfo,
103          transactionModuleJMXRegistration,
104          isDefaultBean);
105     }
106
107     private static Module mockedModule() {
108         Module mockedModule = mock(Module.class);
109         doReturn(mock(AutoCloseable.class)).when(mockedModule).getInstance();
110         return mockedModule;
111     }
112
113 }