2 * Copyright (c) 2014, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.config.manager.impl.osgi;
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;
19 import com.google.common.collect.Lists;
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;
30 public class BundleContextBackedModuleFactoriesResolverTest {
33 private BundleContext bundleContext;
34 private BundleContextBackedModuleFactoriesResolver resolver;
35 private ServiceReference<?> s1;
36 private ServiceReference<?> s2;
37 private ModuleFactory f1;
38 private ModuleFactory f2;
41 public void setUp() throws Exception {
42 MockitoAnnotations.initMocks(this);
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);
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();
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();
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());
82 @SuppressWarnings("IllegalCatch")
83 public void testDuplicateFactories() throws Exception {
84 doReturn(f1).when(bundleContext).getService(s2);
86 resolver.getAllFactories();
87 } catch (final Exception e) {
88 assertThat(e.getMessage(), containsString(f1.getImplementationName()));
89 assertThat(e.getMessage(), containsString("unique"));
93 fail("Should fail with duplicate factory name");
96 @Test(expected = NullPointerException.class)
97 public void testNullFactory() throws Exception {
98 doReturn(null).when(bundleContext).getService(s2);
99 resolver.getAllFactories();
102 @Test(expected = IllegalStateException.class)
103 public void testNullFactoryName() throws Exception {
104 doReturn(null).when(f1).getImplementationName();
105 resolver.getAllFactories();
108 @Test(expected = NullPointerException.class)
109 public void testNullBundleName() throws Exception {
110 doReturn(null).when(s1).getBundle();
111 resolver.getAllFactories();