2 * Copyright (c) 2014, 2015 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.doAnswer;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.mock;
19 import com.google.common.util.concurrent.Uninterruptibles;
20 import java.util.AbstractMap;
21 import java.util.Arrays;
23 import java.util.concurrent.CountDownLatch;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.atomic.AtomicReference;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
30 import org.mockito.invocation.InvocationOnMock;
31 import org.mockito.stubbing.Answer;
32 import org.opendaylight.controller.config.spi.ModuleFactory;
33 import org.osgi.framework.Bundle;
34 import org.osgi.framework.BundleContext;
36 public class BundleContextBackedModuleFactoriesResolverTest {
39 private ModuleFactoryBundleTracker mockBundleTracker;
42 private BundleContext mockBundleContext1;
45 private BundleContext mockBundleContext2;
48 private Bundle mockBundle1;
51 private Bundle mockBundle2;
53 private BundleContextBackedModuleFactoriesResolver resolver;
54 private ModuleFactory f1;
55 private ModuleFactory f2;
58 public void setUp() throws Exception {
59 MockitoAnnotations.initMocks(this);
60 doReturn(mockBundleContext1).when(mockBundle1).getBundleContext();
61 doReturn(mockBundleContext2).when(mockBundle2).getBundleContext();
63 f1 = getMockFactory("f1");
64 f2 = getMockFactory("f2");
66 resolver = new BundleContextBackedModuleFactoriesResolver();
67 resolver.setModuleFactoryBundleTracker(mockBundleTracker);
70 private ModuleFactory getMockFactory(final String name) {
71 ModuleFactory mock = mock(ModuleFactory.class);
72 doReturn(name).when(mock).toString();
73 doReturn(name).when(mock).getImplementationName();
78 public void testGetAllFactories() throws Exception {
79 doReturn(Arrays.asList(new AbstractMap.SimpleImmutableEntry<>(f1, mockBundleContext1),
80 new AbstractMap.SimpleImmutableEntry<>(f2, mockBundleContext2))).
81 when(mockBundleTracker).getModuleFactoryEntries();
83 Map<String, Map.Entry<ModuleFactory, BundleContext>> allFactories = resolver.getAllFactories();
84 assertEquals(2, allFactories.size());
85 assertTrue(allFactories.containsKey(f1.getImplementationName()));
86 assertEquals(f1, allFactories.get(f1.getImplementationName()).getKey());
87 assertEquals(mockBundleContext1, allFactories.get(f1.getImplementationName()).getValue());
88 assertTrue(allFactories.containsKey(f2.getImplementationName()));
89 assertEquals(f2, allFactories.get(f2.getImplementationName()).getKey());
90 assertEquals(mockBundleContext2, allFactories.get(f2.getImplementationName()).getValue());
94 public void testDuplicateFactories() throws Exception {
95 doReturn(Arrays.asList(new AbstractMap.SimpleImmutableEntry<>(f1, mockBundleContext1),
96 new AbstractMap.SimpleImmutableEntry<>(f1, mockBundleContext2))).
97 when(mockBundleTracker).getModuleFactoryEntries();
100 resolver.getAllFactories();
101 } catch (Exception e) {
102 assertThat(e.getMessage(), containsString(f1.getImplementationName()));
103 assertThat(e.getMessage(), containsString("unique"));
107 fail("Should fail with duplicate factory name");
110 @Test(expected = IllegalStateException.class)
111 public void testNullFactoryName() throws Exception {
112 doReturn(Arrays.asList(new AbstractMap.SimpleImmutableEntry<>(f1, mockBundleContext1))).
113 when(mockBundleTracker).getModuleFactoryEntries();
115 doReturn(null).when(f1).getImplementationName();
116 resolver.getAllFactories();
120 public void testBundleContextInitiallyNull() throws Exception {
121 final AtomicReference<BundleContext> bundleContext = new AtomicReference<>();
122 Answer<BundleContext> answer = new Answer<BundleContext>() {
124 public BundleContext answer(InvocationOnMock invocation) throws Throwable {
125 return bundleContext.get();
129 doAnswer(answer).when(mockBundle1).getBundleContext();
130 doReturn(Arrays.asList(new AbstractMap.SimpleImmutableEntry<>(f1, mockBundleContext1))).
131 when(mockBundleTracker).getModuleFactoryEntries();
133 final AtomicReference<Map<String, Map.Entry<ModuleFactory, BundleContext>>> allFactories = new AtomicReference<>();
134 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
135 final CountDownLatch doneLatch = new CountDownLatch(1);
140 allFactories.set(resolver.getAllFactories());
141 } catch (Exception e) {
144 doneLatch.countDown();
149 Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
150 bundleContext.set(mockBundleContext1);
152 assertEquals(true, doneLatch.await(5, TimeUnit.SECONDS));
153 if(caughtEx.get() != null) {
154 throw caughtEx.get();
157 assertEquals(1, allFactories.get().size());
158 assertTrue(allFactories.get().containsKey(f1.getImplementationName()));