Add KarafFeaturesSupportTest
[mdsal.git] / dom / mdsal-dom-schema-osgi / src / test / java / org / opendaylight / mdsal / dom / schema / osgi / impl / KarafFeaturesSupportTest.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, 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.osgi.impl;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertSame;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17
18 import org.apache.karaf.features.DeploymentListener;
19 import org.apache.karaf.features.FeaturesService;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
26 import org.osgi.framework.BundleContext;
27 import org.osgi.framework.ServiceReference;
28 import org.osgi.service.component.ComponentFactory;
29
30 @RunWith(MockitoJUnitRunner.StrictStubs.class)
31 public class KarafFeaturesSupportTest {
32     @Mock
33     private YangParserFactory parserFactory;
34     @Mock
35     private ComponentFactory contextFactory;
36     @Mock
37     private BundleContext bundleContext;
38     @Mock
39     private ServiceReference<FeaturesService> mockRef;
40
41     private RegularYangModuleInfoRegistry infoRegistry;
42
43     @Before
44     public void before() {
45         infoRegistry = new RegularYangModuleInfoRegistry(contextFactory, parserFactory);
46     }
47
48     @Test
49     public void testWrapperWithKaraf() {
50         final FeaturesService mockFeatures = mock(FeaturesService.class);
51         doNothing().when(mockFeatures).registerListener(any(DeploymentListener.class));
52         doReturn(mockFeatures).when(bundleContext).getService(mockRef);
53         doReturn(mockRef).when(bundleContext).getServiceReference(FeaturesService.class);
54
55         final YangModuleInfoRegistry wrapped = KarafFeaturesSupport.wrap(bundleContext, infoRegistry);
56         assertThat(wrapped, instanceOf(KarafYangModuleInfoRegistry.class));
57     }
58
59     @Test
60     public void testWrapperWithoutKaraf() {
61         doReturn(null).when(bundleContext).getServiceReference(FeaturesService.class);
62
63         final YangModuleInfoRegistry wrapped = KarafFeaturesSupport.wrap(bundleContext, infoRegistry);
64         assertSame(infoRegistry, wrapped);
65     }
66 }