2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.schema.service.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.ArgumentMatchers.any;
14 import static org.mockito.ArgumentMatchers.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.api.DOMSchemaService;
24 import org.opendaylight.mdsal.dom.schema.service.osgi.util.TestModel;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
27 import org.osgi.framework.Bundle;
28 import org.osgi.framework.BundleContext;
29 import org.osgi.framework.Filter;
30 import org.osgi.framework.InvalidSyntaxException;
31 import org.osgi.framework.ServiceReference;
33 public class OsgiBundleScanningSchemaServiceTest {
35 private OsgiBundleScanningSchemaService osgiService;
36 private final BundleContext bundleContext = mock(BundleContext.class, "bundleContext");
39 public void setUp() throws InvalidSyntaxException {
41 doReturn(mock(Filter.class)).when(bundleContext).createFilter(any());
42 doNothing().when(bundleContext).addBundleListener(any());
43 doReturn(new Bundle[] {}).when(bundleContext).getBundles();
44 doNothing().when(bundleContext).addServiceListener(any(), any());
45 doReturn(new ServiceReference<?>[] {}).when(bundleContext).getServiceReferences(anyString(), any());
46 doNothing().when(bundleContext).removeBundleListener(any());
47 doNothing().when(bundleContext).removeServiceListener(any());
48 osgiService = OsgiBundleScanningSchemaService.createInstance(bundleContext);
49 assertEquals(osgiService, OsgiBundleScanningSchemaService.getInstance());
50 assertEquals(bundleContext, osgiService.getContext());
53 @SuppressWarnings("checkstyle:IllegalCatch")
55 public void destroyInstance() {
57 OsgiBundleScanningSchemaService.getInstance();
58 OsgiBundleScanningSchemaService.destroyInstance();
59 } catch (final Exception e) {
60 assertTrue(e instanceof IllegalStateException);
65 public void basicTest() {
66 assertTrue(osgiService instanceof DOMSchemaService);
68 final SchemaContext schemaContext = TestModel.createTestContext();
70 final SchemaContextListener schemaContextListener = mock(SchemaContextListener.class);
71 doNothing().when(schemaContextListener).onGlobalContextUpdated(schemaContext);
72 osgiService.registerSchemaContextListener(schemaContextListener);
74 osgiService.notifyListeners(schemaContext);
76 doReturn(schemaContextListener).when(bundleContext).getService(null);
77 assertEquals(schemaContextListener, osgiService.addingService(null));
79 osgiService.registerSchemaContextListener(schemaContextListener);
80 assertNull(osgiService.getSchemaContext());
82 doReturn(false).when(bundleContext).ungetService(null);
83 osgiService.removedService(null, null);
84 verify(bundleContext).ungetService(any());
89 @Test(expected = UnsupportedOperationException.class)
90 public void sessionContextTest() {
91 osgiService.getSessionContext();