Bug 8326 - Split out OsgiBundleScanningSchemaService
[mdsal.git] / dom / mdsal-dom-schema-service-osgi / src / test / java / org / opendaylight / mdsal / dom / schema / service / osgi / OsgiBundleScanningSchemaServiceTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.schema.service.osgi;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Matchers.anyString;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.verify;
19 import org.junit.After;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
23 import org.opendaylight.mdsal.dom.schema.service.osgi.util.TestModel;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
26 import org.osgi.framework.Bundle;
27 import org.osgi.framework.BundleContext;
28 import org.osgi.framework.Filter;
29 import org.osgi.framework.ServiceReference;
30
31 public class OsgiBundleScanningSchemaServiceTest {
32
33     private OsgiBundleScanningSchemaService osgiService;
34     private final BundleContext bundleContext = mock(BundleContext.class, "bundleContext");
35
36     @Before
37     public void setUp() throws Exception {
38         destroyInstance();
39         doReturn(mock(Filter.class)).when(bundleContext).createFilter(any());
40         doNothing().when(bundleContext).addBundleListener(any());
41         doReturn(new Bundle[] {}).when(bundleContext).getBundles();
42         doNothing().when(bundleContext).addServiceListener(any(), any());
43         doReturn(new ServiceReference<?>[] {}).when(bundleContext).getServiceReferences(anyString(), any());
44         doNothing().when(bundleContext).removeBundleListener(any());
45         doNothing().when(bundleContext).removeServiceListener(any());
46         osgiService = OsgiBundleScanningSchemaService.createInstance(bundleContext);
47         assertEquals(osgiService, OsgiBundleScanningSchemaService.getInstance());
48         assertEquals(bundleContext, osgiService.getContext());
49     }
50
51     @SuppressWarnings("checkstyle:IllegalCatch")
52     @After
53     public void destroyInstance() throws Exception {
54         try {
55             OsgiBundleScanningSchemaService.getInstance();
56             OsgiBundleScanningSchemaService.destroyInstance();
57         } catch (final Exception e) {
58             assertTrue(e instanceof IllegalStateException);
59         }
60     }
61
62     @Test
63     public void basicTest() throws Exception {
64         assertTrue(osgiService instanceof DOMSchemaService);
65
66         final SchemaContext schemaContext = TestModel.createTestContext();
67
68         final SchemaContextListener schemaContextListener = mock(SchemaContextListener.class);
69         doNothing().when(schemaContextListener).onGlobalContextUpdated(schemaContext);
70         osgiService.registerSchemaContextListener(schemaContextListener);
71
72         osgiService.notifyListeners(schemaContext);
73
74         doReturn(schemaContextListener).when(bundleContext).getService(null);
75         assertEquals(schemaContextListener, osgiService.addingService(null));
76
77         osgiService.registerSchemaContextListener(schemaContextListener);
78         assertNull(osgiService.getSchemaContext());
79
80         doReturn(false).when(bundleContext).ungetService(null);
81         osgiService.removedService(null, null);
82         verify(bundleContext).ungetService(any());
83
84         osgiService.close();
85     }
86
87     @Test(expected = UnsupportedOperationException.class)
88     public void sessionContextTest() throws Exception {
89         osgiService.getSessionContext();
90     }
91 }