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