Remove yang-test
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / osgi / BundleContextBackedModuleFactoriesResolverTest.java
1 /*
2  * Copyright (c) 2014, 2017 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
9 package org.opendaylight.controller.config.manager.impl.osgi;
10
11 import static org.hamcrest.CoreMatchers.containsString;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18
19 import com.google.common.collect.Lists;
20 import java.util.Map;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.controller.config.spi.ModuleFactory;
26 import org.osgi.framework.Bundle;
27 import org.osgi.framework.BundleContext;
28 import org.osgi.framework.ServiceReference;
29
30 public class BundleContextBackedModuleFactoriesResolverTest {
31
32     @Mock
33     private BundleContext bundleContext;
34     private BundleContextBackedModuleFactoriesResolver resolver;
35     private ServiceReference<?> s1;
36     private ServiceReference<?> s2;
37     private ModuleFactory f1;
38     private ModuleFactory f2;
39
40     @Before
41     public void setUp() throws Exception {
42         MockitoAnnotations.initMocks(this);
43         s1 = getServiceRef();
44         s2 = getServiceRef();
45         doReturn(Lists.newArrayList(s1, s2)).when(bundleContext).getServiceReferences(ModuleFactory.class, null);
46         f1 = getMockFactory("f1");
47         doReturn(f1).when(bundleContext).getService(s1);
48         f2 = getMockFactory("f2");
49         doReturn(f2).when(bundleContext).getService(s2);
50         resolver = new BundleContextBackedModuleFactoriesResolver(bundleContext);
51     }
52
53     private static ModuleFactory getMockFactory(final String name) {
54         ModuleFactory mock = mock(ModuleFactory.class);
55         doReturn(name).when(mock).toString();
56         doReturn(name).when(mock).getImplementationName();
57         return mock;
58     }
59
60     private ServiceReference<?> getServiceRef() {
61         ServiceReference<?> mock = mock(ServiceReference.class);
62         doReturn("serviceRef").when(mock).toString();
63         final Bundle bundle = mock(Bundle.class);
64         doReturn(bundleContext).when(bundle).getBundleContext();
65         doReturn(bundle).when(mock).getBundle();
66         return mock;
67     }
68
69     @Test
70     public void testGetAllFactories() throws Exception {
71         Map<String, Map.Entry<ModuleFactory, BundleContext>> allFactories = resolver.getAllFactories();
72         assertEquals(2, allFactories.size());
73         assertTrue(allFactories.containsKey(f1.getImplementationName()));
74         assertEquals(f1, allFactories.get(f1.getImplementationName()).getKey());
75         assertEquals(bundleContext, allFactories.get(f1.getImplementationName()).getValue());
76         assertTrue(allFactories.containsKey(f2.getImplementationName()));
77         assertEquals(f2, allFactories.get(f2.getImplementationName()).getKey());
78         assertEquals(bundleContext, allFactories.get(f2.getImplementationName()).getValue());
79     }
80
81     @Test
82     @SuppressWarnings("IllegalCatch")
83     public void testDuplicateFactories() throws Exception {
84         doReturn(f1).when(bundleContext).getService(s2);
85         try {
86             resolver.getAllFactories();
87         } catch (final Exception e) {
88             assertThat(e.getMessage(), containsString(f1.getImplementationName()));
89             assertThat(e.getMessage(), containsString("unique"));
90             return;
91         }
92
93         fail("Should fail with duplicate factory name");
94     }
95
96     @Test(expected = NullPointerException.class)
97     public void testNullFactory() throws Exception {
98         doReturn(null).when(bundleContext).getService(s2);
99         resolver.getAllFactories();
100     }
101
102     @Test(expected = IllegalStateException.class)
103     public void testNullFactoryName() throws Exception {
104         doReturn(null).when(f1).getImplementationName();
105         resolver.getAllFactories();
106     }
107
108     @Test(expected = NullPointerException.class)
109     public void testNullBundleName() throws Exception {
110         doReturn(null).when(s1).getBundle();
111         resolver.getAllFactories();
112     }
113 }