Fix license header violations in config-manager
[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
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 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     public void testDuplicateFactories() throws Exception {
83         doReturn(f1).when(bundleContext).getService(s2);
84         try {
85             resolver.getAllFactories();
86         } catch (Exception e) {
87             assertThat(e.getMessage(), containsString(f1.getImplementationName()));
88             assertThat(e.getMessage(), containsString("unique"));
89             return;
90         }
91
92         fail("Should fail with duplicate factory name");
93     }
94
95     @Test(expected = NullPointerException.class)
96     public void testNullFactory() throws Exception {
97         doReturn(null).when(bundleContext).getService(s2);
98         resolver.getAllFactories();
99     }
100
101     @Test(expected = IllegalStateException.class)
102     public void testNullFactoryName() throws Exception {
103         doReturn(null).when(f1).getImplementationName();
104         resolver.getAllFactories();
105     }
106
107     @Test(expected = NullPointerException.class)
108     public void testNullBundleName() throws Exception {
109         doReturn(null).when(s1).getBundle();
110         resolver.getAllFactories();
111     }
112 }