2 * Copyright (c) 2016 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
8 package org.opendaylight.mdsal.dom.broker.osgi;
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;
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.mdsal.dom.broker.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;
31 public class OsgiBundleScanningSchemaServiceTest {
33 private OsgiBundleScanningSchemaService osgiService;
34 private final BundleContext bundleContext = mock(BundleContext.class, "bundleContext");
37 public void setUp() throws Exception {
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());
51 @SuppressWarnings("checkstyle:IllegalCatch")
53 public void destroyInstance() throws Exception {
55 OsgiBundleScanningSchemaService.getInstance();
56 OsgiBundleScanningSchemaService.destroyInstance();
57 } catch (Exception e) {
58 assertTrue(e instanceof IllegalStateException);
63 public void basicTest() throws Exception {
64 final SchemaContext schemaContext = TestModel.createTestContext();
65 final SchemaContextListener schemaContextListener = mock(SchemaContextListener.class);
66 doNothing().when(schemaContextListener).onGlobalContextUpdated(schemaContext);
67 osgiService.registerSchemaContextListener(schemaContextListener);
69 osgiService.notifyListeners(schemaContext);
71 doReturn(schemaContextListener).when(bundleContext).getService(null);
72 assertEquals(schemaContextListener, osgiService.addingService(null));
74 osgiService.registerSchemaContextListener(schemaContextListener);
75 assertNull(osgiService.getSchemaContext());
77 doReturn(false).when(bundleContext).ungetService(null);
78 osgiService.removedService(null, null);
79 verify(bundleContext).ungetService(any());
84 @Test(expected = UnsupportedOperationException.class)
85 public void sessionContextTest() throws Exception {
86 osgiService.getSessionContext();