a0751b60bccc83990f50802b1763c15e90b54773
[mdsal.git] / dom / mdsal-dom-broker / src / test / java / org / opendaylight / mdsal / dom / broker / osgi / SchemaServiceActivatorTest.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.mdsal.dom.broker.osgi;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyString;
13 import static org.mockito.Mockito.CALLS_REAL_METHODS;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.verify;
18
19 import java.lang.reflect.Field;
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.osgi.framework.Bundle;
24 import org.osgi.framework.BundleContext;
25 import org.osgi.framework.Filter;
26 import org.osgi.framework.ServiceReference;
27 import org.osgi.framework.ServiceRegistration;
28
29 public class SchemaServiceActivatorTest {
30
31     @Test
32     public void basicTest() throws Exception {
33         final BundleContext bundleContext = mock(BundleContext.class);
34         doReturn(mock(Filter.class)).when(bundleContext).createFilter(any());
35         doNothing().when(bundleContext).addBundleListener(any());
36         doReturn(new Bundle[] {}).when(bundleContext).getBundles();
37         doNothing().when(bundleContext).addServiceListener(any(), any());
38         doReturn(new ServiceReference<?>[] {}).when(bundleContext).getServiceReferences(anyString(), any());
39         doReturn(mock(ServiceRegistration.class)).when(bundleContext).registerService(any(Class.class), any(), any());
40         doNothing().when(bundleContext).removeBundleListener(any());
41         doNothing().when(bundleContext).removeServiceListener(any());
42         final SchemaServiceActivator schemaServiceActivator = new SchemaServiceActivator();
43         schemaServiceActivator.start(bundleContext);
44
45         final ServiceRegistration registration = mock(ServiceRegistration.class);
46         final OsgiBundleScanningSchemaService osgiBundle =
47                 mock(OsgiBundleScanningSchemaService.class, CALLS_REAL_METHODS);
48
49         final Field schemaServiceRegField = SchemaServiceActivator.class.getDeclaredField("schemaServiceReg");
50         schemaServiceRegField.setAccessible(true);
51         schemaServiceRegField.set(schemaServiceActivator, registration);
52
53         final Field schemaServiceField = SchemaServiceActivator.class.getDeclaredField("schemaService");
54         schemaServiceField.setAccessible(true);
55         schemaServiceField.set(schemaServiceActivator, osgiBundle);
56
57         doNothing().when(registration).unregister();
58         doNothing().when(osgiBundle).close();
59
60         schemaServiceActivator.stop(bundleContext);
61         verify(registration).unregister();
62         verify(osgiBundle).close();
63     }
64
65     @After
66     @Before
67     public void destroyInstance() throws Exception {
68         try {
69             OsgiBundleScanningSchemaService.getInstance();
70             OsgiBundleScanningSchemaService.destroyInstance();
71         } catch (Exception e) {
72             assertTrue(e instanceof IllegalStateException);
73         }
74     }
75 }