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;
19 import org.junit.After;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.mdsal.dom.broker.util.TestModel;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
25 import org.osgi.framework.Bundle;
26 import org.osgi.framework.BundleContext;
27 import org.osgi.framework.Filter;
28 import org.osgi.framework.ServiceReference;
30 public class OsgiBundleScanningSchemaServiceTest {
32 private OsgiBundleScanningSchemaService osgiService;
35 public void setUp() throws Exception {
37 final BundleContext bundleContext = mock(BundleContext.class, "bundleContext");
38 doReturn(mock(Filter.class)).when(bundleContext).createFilter(any());
39 doNothing().when(bundleContext).addBundleListener(any());
40 doReturn(new Bundle[] {}).when(bundleContext).getBundles();
41 doNothing().when(bundleContext).addServiceListener(any(), any());
42 doReturn(new ServiceReference<?>[] {}).when(bundleContext).getServiceReferences(anyString(), any());
43 doNothing().when(bundleContext).removeBundleListener(any());
44 doNothing().when(bundleContext).removeServiceListener(any());
45 osgiService = OsgiBundleScanningSchemaService.createInstance(bundleContext);
46 assertEquals(osgiService, OsgiBundleScanningSchemaService.getInstance());
47 assertEquals(bundleContext, osgiService.getContext());
50 @SuppressWarnings("checkstyle:IllegalCatch")
52 public void destroyInstance() throws Exception {
54 OsgiBundleScanningSchemaService.getInstance();
55 OsgiBundleScanningSchemaService.destroyInstance();
56 } catch (Exception e) {
57 assertTrue(e instanceof IllegalStateException);
62 public void basicTest() throws Exception {
63 final SchemaContext schemaContext = TestModel.createTestContext();
64 final SchemaContextListener schemaContextListener = mock(SchemaContextListener.class);
65 doNothing().when(schemaContextListener).onGlobalContextUpdated(schemaContext);
66 osgiService.registerSchemaContextListener(schemaContextListener);
68 osgiService.notifyListeners(schemaContext);
70 osgiService.registerSchemaContextListener(schemaContextListener);
71 assertNull(osgiService.getSchemaContext());
75 @Test(expected = UnsupportedOperationException.class)
76 public void sessionContextTest() throws Exception {
77 osgiService.getSessionContext();