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