Fix intermittent unit test failures
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DatastoreContextConfigAdminOverlayTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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 package org.opendaylight.controller.cluster.datastore;
9
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Matchers.eq;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.reset;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import java.io.IOException;
18 import java.util.Dictionary;
19 import java.util.Hashtable;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.ArgumentCaptor;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.osgi.framework.BundleContext;
26 import org.osgi.framework.ServiceReference;
27 import org.osgi.framework.ServiceRegistration;
28 import org.osgi.service.cm.Configuration;
29 import org.osgi.service.cm.ConfigurationAdmin;
30 import org.osgi.service.cm.ConfigurationEvent;
31 import org.osgi.service.cm.ConfigurationListener;
32
33 /**
34  * Unit tests for DatastoreContextConfigAdminOverlay.
35  *
36  * @author Thomas Pantelis
37  */
38 @SuppressWarnings("unchecked")
39 public class DatastoreContextConfigAdminOverlayTest {
40
41     @Mock
42     private BundleContext mockBundleContext;
43
44     @Mock
45     private ServiceReference<ConfigurationAdmin> mockConfigAdminServiceRef;
46
47     @Mock
48     private ConfigurationAdmin mockConfigAdmin;
49
50     @Mock
51     private Configuration mockConfig;
52
53     @Mock
54     private DatastoreContextIntrospector mockIntrospector;
55
56     @Mock
57     private ServiceRegistration<?> configListenerServiceReg;
58
59     @Before
60     public void setup() throws IOException {
61         MockitoAnnotations.initMocks(this);
62
63         doReturn(mockConfigAdminServiceRef).when(mockBundleContext).getServiceReference(ConfigurationAdmin.class);
64         doReturn(mockConfigAdmin).when(mockBundleContext).getService(mockConfigAdminServiceRef);
65         doReturn(configListenerServiceReg).when(mockBundleContext).registerService(
66                 eq(ConfigurationListener.class.getName()), any(), any(Dictionary.class));
67
68         doReturn(mockConfig).when(mockConfigAdmin).getConfiguration(DatastoreContextConfigAdminOverlay.CONFIG_ID);
69
70         doReturn(DatastoreContextConfigAdminOverlay.CONFIG_ID).when(mockConfig).getPid();
71
72     }
73
74     @Test
75     public void testUpdateOnConstruction() {
76         Dictionary<String, Object> properties = new Hashtable<>();
77         properties.put("property", "value");
78         doReturn(properties).when(mockConfig).getProperties();
79
80         DatastoreContextConfigAdminOverlay overlay = new DatastoreContextConfigAdminOverlay(
81                 mockIntrospector, mockBundleContext);
82
83         verify(mockIntrospector).update(properties);
84
85         verify(mockBundleContext).ungetService(mockConfigAdminServiceRef);
86
87         overlay.close();
88     }
89
90     @Test
91     public void testUpdateOnConfigurationEvent() {
92         DatastoreContextConfigAdminOverlay overlay = new DatastoreContextConfigAdminOverlay(
93                 mockIntrospector, mockBundleContext);
94
95         reset(mockIntrospector);
96
97         DatastoreContext context = DatastoreContext.newBuilder().build();
98         doReturn(context).when(mockIntrospector).getContext();
99         DatastoreContextFactory contextFactory = new DatastoreContextFactory(mockIntrospector);
100         doReturn(contextFactory).when(mockIntrospector).newContextFactory();
101
102         DatastoreContextConfigAdminOverlay.Listener mockListener =
103                 mock(DatastoreContextConfigAdminOverlay.Listener.class);
104
105         overlay.setListener(mockListener);
106
107         Dictionary<String, Object> properties = new Hashtable<>();
108         properties.put("property", "value");
109         doReturn(properties).when(mockConfig).getProperties();
110
111         doReturn(true).when(mockIntrospector).update(properties);
112
113         ArgumentCaptor<ConfigurationListener> configListener =
114                 ArgumentCaptor.forClass(ConfigurationListener.class);
115         verify(mockBundleContext).registerService(eq(ConfigurationListener.class.getName()),
116                 configListener.capture(), any(Dictionary.class));
117
118         ConfigurationEvent configEvent = mock(ConfigurationEvent.class);
119         doReturn(DatastoreContextConfigAdminOverlay.CONFIG_ID).when(configEvent).getPid();
120         doReturn(mockConfigAdminServiceRef).when(configEvent).getReference();
121         doReturn(ConfigurationEvent.CM_UPDATED).when(configEvent).getType();
122
123         configListener.getValue().configurationEvent(configEvent);
124
125         verify(mockIntrospector).update(properties);
126
127         verify(mockListener).onDatastoreContextUpdated(contextFactory);
128
129         verify(mockBundleContext, times(2)).ungetService(mockConfigAdminServiceRef);
130
131         overlay.close();
132
133         verify(configListenerServiceReg).unregister();
134     }
135
136     @Test
137     public void testConfigurationEventWithDifferentPid() {
138         DatastoreContextConfigAdminOverlay overlay = new DatastoreContextConfigAdminOverlay(
139                 mockIntrospector, mockBundleContext);
140
141         reset(mockIntrospector);
142
143         ArgumentCaptor<ConfigurationListener> configListener =
144                 ArgumentCaptor.forClass(ConfigurationListener.class);
145         verify(mockBundleContext).registerService(eq(ConfigurationListener.class.getName()),
146                 configListener.capture(), any(Dictionary.class));
147
148         ConfigurationEvent configEvent = mock(ConfigurationEvent.class);
149         doReturn("other-pid").when(configEvent).getPid();
150         doReturn(mockConfigAdminServiceRef).when(configEvent).getReference();
151         doReturn(ConfigurationEvent.CM_UPDATED).when(configEvent).getType();
152
153         configListener.getValue().configurationEvent(configEvent);
154
155         verify(mockIntrospector, times(0)).update(any(Dictionary.class));
156
157         overlay.close();
158     }
159
160     @Test
161     public void testConfigurationEventWithNonUpdateEventType() {
162         DatastoreContextConfigAdminOverlay overlay = new DatastoreContextConfigAdminOverlay(
163                 mockIntrospector, mockBundleContext);
164
165         reset(mockIntrospector);
166
167         ArgumentCaptor<ConfigurationListener> configListener =
168                 ArgumentCaptor.forClass(ConfigurationListener.class);
169         verify(mockBundleContext).registerService(eq(ConfigurationListener.class.getName()),
170                 configListener.capture(), any(Dictionary.class));
171
172         ConfigurationEvent configEvent = mock(ConfigurationEvent.class);
173         doReturn(DatastoreContextConfigAdminOverlay.CONFIG_ID).when(configEvent).getPid();
174         doReturn(mockConfigAdminServiceRef).when(configEvent).getReference();
175         doReturn(ConfigurationEvent.CM_DELETED).when(configEvent).getType();
176
177         configListener.getValue().configurationEvent(configEvent);
178
179         verify(mockIntrospector, times(0)).update(any(Dictionary.class));
180
181         overlay.close();
182     }
183 }