dee52e488dde2f758caadef22acfdff2e98709a5
[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.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;
22 import java.util.Map;
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;
35
36 public class BundleContextBackedModuleFactoriesResolverTest {
37
38     @Mock
39     private ModuleFactoryBundleTracker mockBundleTracker;
40
41     @Mock
42     private BundleContext mockBundleContext1;
43
44     @Mock
45     private BundleContext mockBundleContext2;
46
47     @Mock
48     private Bundle mockBundle1;
49
50     @Mock
51     private Bundle mockBundle2;
52
53     private BundleContextBackedModuleFactoriesResolver resolver;
54     private ModuleFactory f1;
55     private ModuleFactory f2;
56
57     @Before
58     public void setUp() throws Exception {
59         MockitoAnnotations.initMocks(this);
60         doReturn(mockBundleContext1).when(mockBundle1).getBundleContext();
61         doReturn(mockBundleContext2).when(mockBundle2).getBundleContext();
62
63         f1 = getMockFactory("f1");
64         f2 = getMockFactory("f2");
65
66         resolver = new BundleContextBackedModuleFactoriesResolver();
67         resolver.setModuleFactoryBundleTracker(mockBundleTracker);
68     }
69
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();
74         return mock;
75     }
76
77     @Test
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();
82
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());
91     }
92
93     @Test
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();
98
99         try {
100             resolver.getAllFactories();
101         } catch (Exception e) {
102             assertThat(e.getMessage(), containsString(f1.getImplementationName()));
103             assertThat(e.getMessage(), containsString("unique"));
104             return;
105         }
106
107         fail("Should fail with duplicate factory name");
108     }
109
110     @Test(expected = IllegalStateException.class)
111     public void testNullFactoryName() throws Exception {
112         doReturn(Arrays.asList(new AbstractMap.SimpleImmutableEntry<>(f1, mockBundleContext1))).
113                 when(mockBundleTracker).getModuleFactoryEntries();
114
115         doReturn(null).when(f1).getImplementationName();
116         resolver.getAllFactories();
117     }
118
119     @Test
120     public void testBundleContextInitiallyNull() throws Exception {
121         final AtomicReference<BundleContext> bundleContext = new AtomicReference<>();
122         Answer<BundleContext> answer = new Answer<BundleContext>() {
123             @Override
124             public BundleContext answer(InvocationOnMock invocation) throws Throwable {
125                 return bundleContext.get();
126             }
127         };
128
129         doAnswer(answer).when(mockBundle1).getBundleContext();
130         doReturn(Arrays.asList(new AbstractMap.SimpleImmutableEntry<>(f1, mockBundleContext1))).
131                 when(mockBundleTracker).getModuleFactoryEntries();
132
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);
136         new Thread() {
137             @Override
138             public void run() {
139                 try {
140                     allFactories.set(resolver.getAllFactories());
141                 } catch (Exception e) {
142                     caughtEx.set(e);
143                 } finally {
144                     doneLatch.countDown();
145                 }
146             }
147         }.start();
148
149         Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
150         bundleContext.set(mockBundleContext1);
151
152         assertEquals(true, doneLatch.await(5, TimeUnit.SECONDS));
153         if(caughtEx.get() != null) {
154             throw caughtEx.get();
155         }
156
157         assertEquals(1, allFactories.get().size());
158         assertTrue(allFactories.get().containsKey(f1.getImplementationName()));
159     }
160 }