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 java.lang.reflect.Method;
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;
36 public void setUp() throws Exception {
38 final BundleContext bundleContext = mock(BundleContext.class, "bundleContext");
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 final Method schemaContextUpdate =
70 OsgiBundleScanningSchemaService.class.getDeclaredMethod("updateContext", SchemaContext.class);
71 schemaContextUpdate.setAccessible(true);
72 schemaContextUpdate.invoke(osgiService, schemaContext);
74 osgiService.registerSchemaContextListener(schemaContextListener);
75 assertNull(osgiService.getSchemaContext());
79 @Test(expected = UnsupportedOperationException.class)
80 public void sessionContextTest() throws Exception {
81 osgiService.getSessionContext();