9a3ba64419e59c07660b49ac78386fc400235421
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / osgi / ExtensibleBundleTrackerTest.java
1 package org.opendaylight.controller.config.manager.impl.osgi;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.mockito.Mockito.doNothing;
5 import static org.mockito.Mockito.doReturn;
6 import static org.mockito.Mockito.verifyZeroInteractions;
7
8 import com.google.common.util.concurrent.Futures;
9 import org.junit.Before;
10 import org.junit.Test;
11 import org.mockito.InOrder;
12 import org.mockito.Mock;
13 import org.mockito.Mockito;
14 import org.mockito.MockitoAnnotations;
15 import org.osgi.framework.Bundle;
16 import org.osgi.framework.BundleContext;
17 import org.osgi.framework.BundleEvent;
18 import org.osgi.util.tracker.BundleTrackerCustomizer;
19
20 public class ExtensibleBundleTrackerTest {
21
22     @Mock
23     private BundleContext bundleContext;
24     @Mock
25     private Bundle bundle;
26     @Mock
27     private BundleEvent bundleEvent;
28
29     @Mock
30     private BundleTrackerCustomizer<Object> primaryTracker;
31     @Mock
32     private BundleTrackerCustomizer<?> additionalTracker;
33
34     private ExtensibleBundleTracker<Object> extensibleBundleTracker;
35     private Object primaryValue = new Object();
36
37     @Before
38     public void setUp() throws Exception {
39         MockitoAnnotations.initMocks(this);
40         doReturn("bundle").when(bundle).toString();
41         doReturn("bundleEvent").when(bundleEvent).toString();
42
43         doReturn(primaryValue).when(primaryTracker).addingBundle(bundle, bundleEvent);
44         doNothing().when(primaryTracker).modifiedBundle(bundle, bundleEvent, primaryValue);
45         doNothing().when(primaryTracker).removedBundle(bundle, bundleEvent, primaryValue);
46
47         doReturn(new Object()).when(additionalTracker).addingBundle(bundle, bundleEvent);
48         doNothing().when(additionalTracker).modifiedBundle(bundle, bundleEvent, null);
49         doNothing().when(additionalTracker).removedBundle(bundle, bundleEvent, null);
50         extensibleBundleTracker = new ExtensibleBundleTracker<>(bundleContext, primaryTracker, additionalTracker);
51     }
52
53     @Test
54     public void testAddingBundle() throws Exception {
55         assertEquals(primaryValue, extensibleBundleTracker.addingBundle(bundle, bundleEvent).get());
56         InOrder inOrder = Mockito.inOrder(primaryTracker, additionalTracker);
57         inOrder.verify(primaryTracker).addingBundle(bundle, bundleEvent);
58         inOrder.verify(additionalTracker).addingBundle(bundle, bundleEvent);
59     }
60
61     @Test
62     public void testRemovedBundle() throws Exception {
63         extensibleBundleTracker.removedBundle(bundle, bundleEvent, Futures.immediateFuture(primaryValue));
64         InOrder inOrder = Mockito.inOrder(primaryTracker, additionalTracker);
65         inOrder.verify(primaryTracker).removedBundle(bundle, bundleEvent, primaryValue);
66         inOrder.verify(additionalTracker).removedBundle(bundle, bundleEvent, null);
67     }
68
69     @Test
70     public void testRemovedBundleWithEx() throws Exception {
71         IllegalStateException throwable = new IllegalStateException();
72         extensibleBundleTracker.removedBundle(bundle, bundleEvent, Futures.immediateFailedFuture(throwable));
73         verifyZeroInteractions(primaryTracker);
74         verifyZeroInteractions(additionalTracker);
75     }
76 }