checkStyleViolationSeverity=error implemented for mdsal-dom-broker
[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 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;
30
31 public class OsgiBundleScanningSchemaServiceTest {
32
33     private OsgiBundleScanningSchemaService osgiService;
34
35     @Before
36     public void setUp() throws Exception {
37         destroyInstance();
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());
49     }
50
51     @SuppressWarnings("checkstyle:IllegalCatch")
52     @After
53     public void destroyInstance() throws Exception {
54         try {
55             OsgiBundleScanningSchemaService.getInstance();
56             OsgiBundleScanningSchemaService.destroyInstance();
57         } catch (Exception e) {
58             assertTrue(e instanceof IllegalStateException);
59         }
60     }
61
62     @Test
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);
68
69         final Method schemaContextUpdate =
70                 OsgiBundleScanningSchemaService.class.getDeclaredMethod("updateContext", SchemaContext.class);
71         schemaContextUpdate.setAccessible(true);
72         schemaContextUpdate.invoke(osgiService, schemaContext);
73
74         osgiService.registerSchemaContextListener(schemaContextListener);
75         assertNull(osgiService.getSchemaContext());
76         osgiService.close();
77     }
78
79     @Test(expected = UnsupportedOperationException.class)
80     public void sessionContextTest() throws Exception {
81         osgiService.getSessionContext();
82     }
83 }