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