Fix mockito deprecation warnings
[mdsal.git] / dom / mdsal-dom-schema-service-osgi / src / test / java / org / opendaylight / mdsal / dom / schema / service / osgi / OsgiBundleScanningSchemaServiceTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.schema.service.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.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;
19
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;
32
33 public class OsgiBundleScanningSchemaServiceTest {
34
35     private OsgiBundleScanningSchemaService osgiService;
36     private final BundleContext bundleContext = mock(BundleContext.class, "bundleContext");
37
38     @Before
39     public void setUp() throws InvalidSyntaxException {
40         destroyInstance();
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());
51     }
52
53     @SuppressWarnings("checkstyle:IllegalCatch")
54     @After
55     public void destroyInstance() {
56         try {
57             OsgiBundleScanningSchemaService.getInstance();
58             OsgiBundleScanningSchemaService.destroyInstance();
59         } catch (final Exception e) {
60             assertTrue(e instanceof IllegalStateException);
61         }
62     }
63
64     @Test
65     public void basicTest() {
66         assertTrue(osgiService instanceof DOMSchemaService);
67
68         final SchemaContext schemaContext = TestModel.createTestContext();
69
70         final SchemaContextListener schemaContextListener = mock(SchemaContextListener.class);
71         doNothing().when(schemaContextListener).onGlobalContextUpdated(schemaContext);
72         osgiService.registerSchemaContextListener(schemaContextListener);
73
74         osgiService.notifyListeners(schemaContext);
75
76         doReturn(schemaContextListener).when(bundleContext).getService(null);
77         assertEquals(schemaContextListener, osgiService.addingService(null));
78
79         osgiService.registerSchemaContextListener(schemaContextListener);
80         assertNull(osgiService.getSchemaContext());
81
82         doReturn(false).when(bundleContext).ungetService(null);
83         osgiService.removedService(null, null);
84         verify(bundleContext).ungetService(any());
85
86         osgiService.close();
87     }
88
89     @Test(expected = UnsupportedOperationException.class)
90     public void sessionContextTest() {
91         osgiService.getSessionContext();
92     }
93 }