Merge "Introduce forwarding DOMData classes"
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / osgi / ModuleFactoryBundleTrackerTest.java
1 package org.opendaylight.controller.config.manager.impl.osgi;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.fail;
6 import static org.mockito.Matchers.any;
7 import static org.mockito.Matchers.anyObject;
8 import static org.mockito.Matchers.anyString;
9 import static org.mockito.Mockito.doAnswer;
10 import static org.mockito.Mockito.doReturn;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.verifyZeroInteractions;
14 import java.util.Dictionary;
15 import java.util.Set;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mock;
19 import org.mockito.MockitoAnnotations;
20 import org.mockito.invocation.InvocationOnMock;
21 import org.mockito.stubbing.Answer;
22 import org.opendaylight.controller.config.api.DependencyResolver;
23 import org.opendaylight.controller.config.api.DependencyResolverFactory;
24 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
25 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
26 import org.opendaylight.controller.config.spi.Module;
27 import org.opendaylight.controller.config.spi.ModuleFactory;
28 import org.osgi.framework.Bundle;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.framework.BundleEvent;
31 import org.osgi.framework.ServiceRegistration;
32
33 public class ModuleFactoryBundleTrackerTest {
34
35     @Mock
36     private Bundle bundle;
37     @Mock
38     private BundleContext context;
39     @Mock
40     private ServiceRegistration<?> reg;
41
42     @Before
43     public void setUp() throws Exception {
44         MockitoAnnotations.initMocks(this);
45         doAnswer(new Answer<Object>() {
46             @Override
47             public Object answer(final InvocationOnMock invocation) throws Throwable {
48                 return getClass().getClassLoader().loadClass((String) invocation.getArguments()[0]);
49             }
50         }).when(bundle).loadClass(anyString());
51         doReturn("mockBundle").when(bundle).toString();
52         doReturn(context).when(bundle).getBundleContext();
53         doReturn(reg).when(context).registerService(anyString(), anyObject(), any(Dictionary.class));
54     }
55
56     @Test
57     public void testRegisterFactory() throws Exception {
58         ModuleFactoryBundleTracker.registerFactory(TestingFactory.class.getName(), bundle);
59         verify(context).registerService(ModuleFactory.class.getName(), TestingFactory.currentInstance, null);
60     }
61
62     @Test
63     public void testRegisterFactoryInstantiateEx() throws Exception {
64         try {
65             ModuleFactoryBundleTracker.registerFactory(WrongConstructorTestingFactory.class.getName(), bundle);
66         } catch (Exception e) {
67             verifyZeroInteractions(context);
68             assertNotNull(e.getCause());
69             assertEquals(InstantiationException.class, e.getCause().getClass());
70             return;
71         }
72
73         fail("Cannot register without proper constructor");
74     }
75
76     @Test
77     public void testRegisterFactoryInstantiateExAccess() throws Exception {
78         try {
79             ModuleFactoryBundleTracker.registerFactory(NoAccessConstructorTestingFactory.class.getName(), bundle);
80         } catch (Exception e) {
81             verifyZeroInteractions(context);
82             assertNotNull(e.getCause());
83             assertEquals(IllegalAccessException.class, e.getCause().getClass());
84             return;
85         }
86
87         fail("Cannot register without proper constructor");
88     }
89
90     @Test
91     public void testRegisterFactoryNotExtending() throws Exception {
92         try {
93             ModuleFactoryBundleTracker.registerFactory(NotExtendingTestingFactory.class.getName(), bundle);
94         } catch (Exception e) {
95             verifyZeroInteractions(context);
96             return;
97         }
98
99         fail("Cannot register without extend");
100     }
101
102     @Test
103     public void testRegisterFactoryNotExisting() throws Exception {
104         try {
105             ModuleFactoryBundleTracker.registerFactory("Unknown class", bundle);
106         } catch (Exception e) {
107             verifyZeroInteractions(context);
108             assertNotNull(e.getCause());
109             assertEquals(ClassNotFoundException.class, e.getCause().getClass());
110             return;
111         }
112
113         fail("Cannot register without extend");
114     }
115
116     @Mock
117     private BlankTransactionServiceTracker blankTxTracker;
118
119     @Test
120     public void testAddingBundle() throws Exception {
121         final ModuleFactoryBundleTracker tracker = new ModuleFactoryBundleTracker(blankTxTracker);
122         doReturn(getClass().getResource("/module-factories/module-factory-ok")).when(bundle).getEntry(anyString());
123         tracker.addingBundle(bundle, mock(BundleEvent.class));
124         verify(context).registerService(ModuleFactory.class.getName(), TestingFactory.currentInstance, null);
125     }
126
127     @Test
128     public void testAddingBundleError() throws Exception {
129         final ModuleFactoryBundleTracker tracker = new ModuleFactoryBundleTracker(blankTxTracker);
130         doReturn(getClass().getResource("/module-factories/module-factory-fail")).when(bundle).getEntry(anyString());
131         try {
132             tracker.addingBundle(bundle, mock(BundleEvent.class));
133         } catch (Exception e) {
134             verifyZeroInteractions(context);
135             return;
136         }
137
138         fail("Cannot register");
139     }
140
141     static class WrongConstructorTestingFactory extends TestingFactory {
142         WrongConstructorTestingFactory(final String randomParam) {
143         }
144     }
145
146     static class NotExtendingTestingFactory {}
147
148     static class NoAccessConstructorTestingFactory extends TestingFactory {
149         private NoAccessConstructorTestingFactory() {
150         }
151     }
152
153     static class TestingFactory implements ModuleFactory {
154
155         static TestingFactory currentInstance;
156
157         TestingFactory() {
158             currentInstance = this;
159         }
160
161         @Override
162         public String getImplementationName() {
163             return "Testing";
164         }
165
166         @Override
167         public Module createModule(final String instanceName, final DependencyResolver dependencyResolver, final BundleContext bundleContext) {
168             throw new UnsupportedOperationException();
169         }
170
171         @Override
172         public Module createModule(final String instanceName, final DependencyResolver dependencyResolver, final DynamicMBeanWithInstance old, final BundleContext bundleContext) throws Exception {
173             throw new UnsupportedOperationException();
174         }
175
176         @Override
177         public boolean isModuleImplementingServiceInterface(final Class<? extends AbstractServiceInterface> serviceInterface) {
178             throw new UnsupportedOperationException();
179         }
180
181         @Override
182         public Set<Class<? extends AbstractServiceInterface>> getImplementedServiceIntefaces() {
183             throw new UnsupportedOperationException();
184         }
185
186         @Override
187         public Set<? extends Module> getDefaultModules(final DependencyResolverFactory dependencyResolverFactory, final BundleContext bundleContext) {
188             throw new UnsupportedOperationException();
189         }
190     }
191 }