2 * Copyright (c) 2014, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.config.manager.impl.osgi;
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;
23 import java.util.Dictionary;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.opendaylight.controller.config.api.DependencyResolver;
30 import org.opendaylight.controller.config.api.DependencyResolverFactory;
31 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
32 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
33 import org.opendaylight.controller.config.spi.Module;
34 import org.opendaylight.controller.config.spi.ModuleFactory;
35 import org.osgi.framework.Bundle;
36 import org.osgi.framework.BundleContext;
37 import org.osgi.framework.BundleEvent;
38 import org.osgi.framework.ServiceRegistration;
40 public class ModuleFactoryBundleTrackerTest {
43 private Bundle bundle;
45 private BundleContext context;
47 private ServiceRegistration<?> reg;
50 public void setUp() throws Exception {
51 MockitoAnnotations.initMocks(this);
52 doAnswer(invocation -> getClass().getClassLoader().loadClass((String) invocation.getArguments()[0]))
53 .when(bundle).loadClass(anyString());
54 doReturn("mockBundle").when(bundle).toString();
55 doReturn(context).when(bundle).getBundleContext();
56 doReturn(reg).when(context).registerService(anyString(), anyObject(), any(Dictionary.class));
60 public void testRegisterFactory() throws Exception {
61 ModuleFactoryBundleTracker.registerFactory(TestingFactory.class.getName(), bundle);
62 verify(context).registerService(ModuleFactory.class.getName(), TestingFactory.currentInstance, null);
66 @SuppressWarnings("IllegalCatch")
67 public void testRegisterFactoryInstantiateEx() throws Exception {
69 ModuleFactoryBundleTracker.registerFactory(WrongConstructorTestingFactory.class.getName(), bundle);
70 } catch (final Exception e) {
71 verifyZeroInteractions(context);
72 assertNotNull(e.getCause());
73 assertEquals(InstantiationException.class, e.getCause().getClass());
77 fail("Cannot register without proper constructor");
81 @SuppressWarnings("IllegalCatch")
82 public void testRegisterFactoryInstantiateExAccess() throws Exception {
84 ModuleFactoryBundleTracker.registerFactory(NoAccessConstructorTestingFactory.class.getName(), bundle);
85 } catch (final Exception e) {
86 verifyZeroInteractions(context);
87 assertNotNull(e.getCause());
88 assertEquals(IllegalAccessException.class, e.getCause().getClass());
92 fail("Cannot register without proper constructor");
96 @SuppressWarnings("IllegalCatch")
97 public void testRegisterFactoryNotExtending() throws Exception {
99 ModuleFactoryBundleTracker.registerFactory(NotExtendingTestingFactory.class.getName(), bundle);
100 } catch (final Exception e) {
101 verifyZeroInteractions(context);
105 fail("Cannot register without extend");
109 @SuppressWarnings("IllegalCatch")
110 public void testRegisterFactoryNotExisting() throws Exception {
112 ModuleFactoryBundleTracker.registerFactory("Unknown class", bundle);
113 } catch (final Exception e) {
114 verifyZeroInteractions(context);
115 assertNotNull(e.getCause());
116 assertEquals(ClassNotFoundException.class, e.getCause().getClass());
120 fail("Cannot register without extend");
124 private BlankTransactionServiceTracker blankTxTracker;
127 public void testAddingBundle() throws Exception {
128 final ModuleFactoryBundleTracker tracker = new ModuleFactoryBundleTracker(blankTxTracker);
129 doReturn(getClass().getResource("/module-factories/module-factory-ok")).when(bundle).getEntry(anyString());
130 tracker.addingBundle(bundle, mock(BundleEvent.class));
131 verify(context).registerService(ModuleFactory.class.getName(), TestingFactory.currentInstance, null);
135 @SuppressWarnings("IllegalCatch")
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());
140 tracker.addingBundle(bundle, mock(BundleEvent.class));
141 } catch (final Exception e) {
142 verifyZeroInteractions(context);
146 fail("Cannot register");
149 static class WrongConstructorTestingFactory extends TestingFactory {
150 WrongConstructorTestingFactory(final String randomParam) {
154 static class NotExtendingTestingFactory {
157 static class NoAccessConstructorTestingFactory extends TestingFactory {
158 private NoAccessConstructorTestingFactory() {
162 static class TestingFactory implements ModuleFactory {
164 static TestingFactory currentInstance;
167 currentInstance = this;
171 public String getImplementationName() {
176 public Module createModule(final String instanceName, final DependencyResolver dependencyResolver,
177 final BundleContext bundleContext) {
178 throw new UnsupportedOperationException();
182 public Module createModule(final String instanceName, final DependencyResolver dependencyResolver,
183 final DynamicMBeanWithInstance old, final BundleContext bundleContext) throws Exception {
184 throw new UnsupportedOperationException();
188 public boolean isModuleImplementingServiceInterface(
189 final Class<? extends AbstractServiceInterface> serviceInterface) {
190 throw new UnsupportedOperationException();
194 public Set<Class<? extends AbstractServiceInterface>> getImplementedServiceIntefaces() {
195 throw new UnsupportedOperationException();
199 public Set<? extends Module> getDefaultModules(final DependencyResolverFactory dependencyResolverFactory,
200 final BundleContext bundleContext) {
201 throw new UnsupportedOperationException();