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