Bug 2194: Modify FindPrimary to check for leader
[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
100         DatastoreContextConfigAdminOverlay.Listener mockListener =
101                 mock(DatastoreContextConfigAdminOverlay.Listener.class);
102
103         overlay.setListener(mockListener);
104
105         Dictionary<String, Object> properties = new Hashtable<>();
106         properties.put("property", "value");
107         doReturn(properties).when(mockConfig).getProperties();
108
109         doReturn(true).when(mockIntrospector).update(properties);
110
111         ArgumentCaptor<ConfigurationListener> configListener =
112                 ArgumentCaptor.forClass(ConfigurationListener.class);
113         verify(mockBundleContext).registerService(eq(ConfigurationListener.class.getName()),
114                 configListener.capture(), any(Dictionary.class));
115
116         ConfigurationEvent configEvent = mock(ConfigurationEvent.class);
117         doReturn(DatastoreContextConfigAdminOverlay.CONFIG_ID).when(configEvent).getPid();
118         doReturn(mockConfigAdminServiceRef).when(configEvent).getReference();
119         doReturn(ConfigurationEvent.CM_UPDATED).when(configEvent).getType();
120
121         configListener.getValue().configurationEvent(configEvent);
122
123         verify(mockIntrospector).update(properties);
124
125         verify(mockListener).onDatastoreContextUpdated(context);
126
127         verify(mockBundleContext, times(2)).ungetService(mockConfigAdminServiceRef);
128
129         overlay.close();
130
131         verify(configListenerServiceReg).unregister();
132     }
133
134     @Test
135     public void testConfigurationEventWithDifferentPid() {
136         DatastoreContextConfigAdminOverlay overlay = new DatastoreContextConfigAdminOverlay(
137                 mockIntrospector, mockBundleContext);
138
139         reset(mockIntrospector);
140
141         ArgumentCaptor<ConfigurationListener> configListener =
142                 ArgumentCaptor.forClass(ConfigurationListener.class);
143         verify(mockBundleContext).registerService(eq(ConfigurationListener.class.getName()),
144                 configListener.capture(), any(Dictionary.class));
145
146         ConfigurationEvent configEvent = mock(ConfigurationEvent.class);
147         doReturn("other-pid").when(configEvent).getPid();
148         doReturn(mockConfigAdminServiceRef).when(configEvent).getReference();
149         doReturn(ConfigurationEvent.CM_UPDATED).when(configEvent).getType();
150
151         configListener.getValue().configurationEvent(configEvent);
152
153         verify(mockIntrospector, times(0)).update(any(Dictionary.class));
154
155         overlay.close();
156     }
157
158     @Test
159     public void testConfigurationEventWithNonUpdateEventType() {
160         DatastoreContextConfigAdminOverlay overlay = new DatastoreContextConfigAdminOverlay(
161                 mockIntrospector, mockBundleContext);
162
163         reset(mockIntrospector);
164
165         ArgumentCaptor<ConfigurationListener> configListener =
166                 ArgumentCaptor.forClass(ConfigurationListener.class);
167         verify(mockBundleContext).registerService(eq(ConfigurationListener.class.getName()),
168                 configListener.capture(), any(Dictionary.class));
169
170         ConfigurationEvent configEvent = mock(ConfigurationEvent.class);
171         doReturn(DatastoreContextConfigAdminOverlay.CONFIG_ID).when(configEvent).getPid();
172         doReturn(mockConfigAdminServiceRef).when(configEvent).getReference();
173         doReturn(ConfigurationEvent.CM_DELETED).when(configEvent).getType();
174
175         configListener.getValue().configurationEvent(configEvent);
176
177         verify(mockIntrospector, times(0)).update(any(Dictionary.class));
178
179         overlay.close();
180     }
181 }