Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / osgi / ModuleFactoryBundleTrackerTest.java
1 /*
2  * Copyright (c) 2014, 2017 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.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;
39
40 public class ModuleFactoryBundleTrackerTest {
41
42     @Mock
43     private Bundle bundle;
44     @Mock
45     private BundleContext context;
46     @Mock
47     private ServiceRegistration<?> reg;
48
49     @Before
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));
57     }
58
59     @Test
60     public void testRegisterFactory() throws Exception {
61         ModuleFactoryBundleTracker.registerFactory(TestingFactory.class.getName(), bundle);
62         verify(context).registerService(ModuleFactory.class.getName(), TestingFactory.currentInstance, null);
63     }
64
65     @Test
66     @SuppressWarnings("IllegalCatch")
67     public void testRegisterFactoryInstantiateEx() throws Exception {
68         try {
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());
74             return;
75         }
76
77         fail("Cannot register without proper constructor");
78     }
79
80     @Test
81     @SuppressWarnings("IllegalCatch")
82     public void testRegisterFactoryInstantiateExAccess() throws Exception {
83         try {
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());
89             return;
90         }
91
92         fail("Cannot register without proper constructor");
93     }
94
95     @Test
96     @SuppressWarnings("IllegalCatch")
97     public void testRegisterFactoryNotExtending() throws Exception {
98         try {
99             ModuleFactoryBundleTracker.registerFactory(NotExtendingTestingFactory.class.getName(), bundle);
100         } catch (final Exception e) {
101             verifyZeroInteractions(context);
102             return;
103         }
104
105         fail("Cannot register without extend");
106     }
107
108     @Test
109     @SuppressWarnings("IllegalCatch")
110     public void testRegisterFactoryNotExisting() throws Exception {
111         try {
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());
117             return;
118         }
119
120         fail("Cannot register without extend");
121     }
122
123     @Mock
124     private BlankTransactionServiceTracker blankTxTracker;
125
126     @Test
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);
132     }
133
134     @Test
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());
139         try {
140             tracker.addingBundle(bundle, mock(BundleEvent.class));
141         } catch (final 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
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,
177                 final BundleContext bundleContext) {
178             throw new UnsupportedOperationException();
179         }
180
181         @Override
182         public Module createModule(final String instanceName, final DependencyResolver dependencyResolver,
183                 final DynamicMBeanWithInstance old, final BundleContext bundleContext) throws Exception {
184             throw new UnsupportedOperationException();
185         }
186
187         @Override
188         public boolean isModuleImplementingServiceInterface(
189                 final Class<? extends AbstractServiceInterface> serviceInterface) {
190             throw new UnsupportedOperationException();
191         }
192
193         @Override
194         public Set<Class<? extends AbstractServiceInterface>> getImplementedServiceIntefaces() {
195             throw new UnsupportedOperationException();
196         }
197
198         @Override
199         public Set<? extends Module> getDefaultModules(final DependencyResolverFactory dependencyResolverFactory,
200                 final BundleContext bundleContext) {
201             throw new UnsupportedOperationException();
202         }
203     }
204 }