46f689cdc00264079ec9d4e760896930dba22abd
[mdsal.git] / dom / mdsal-dom-broker / src / test / java / org / opendaylight / mdsal / dom / broker / osgi / OsgiBundleScanningSchemaServiceTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.broker.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
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;
29
30 public class OsgiBundleScanningSchemaServiceTest {
31
32     private OsgiBundleScanningSchemaService osgiService;
33
34     @Before
35     public void setUp() throws Exception {
36         destroyInstance();
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());
48     }
49
50     @SuppressWarnings("checkstyle:IllegalCatch")
51     @After
52     public void destroyInstance() throws Exception {
53         try {
54             OsgiBundleScanningSchemaService.getInstance();
55             OsgiBundleScanningSchemaService.destroyInstance();
56         } catch (Exception e) {
57             assertTrue(e instanceof IllegalStateException);
58         }
59     }
60
61     @Test
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);
67
68         osgiService.notifyListeners(schemaContext);
69
70         osgiService.registerSchemaContextListener(schemaContextListener);
71         assertNull(osgiService.getSchemaContext());
72         osgiService.close();
73     }
74
75     @Test(expected = UnsupportedOperationException.class)
76     public void sessionContextTest() throws Exception {
77         osgiService.getSessionContext();
78     }
79 }