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