2 * Copyright (c) 2015 Brocade Communications 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
8 package org.opendaylight.controller.cluster.datastore;
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;
34 * Unit tests for DatastoreContextConfigAdminOverlay.
36 * @author Thomas Pantelis
38 @SuppressWarnings("unchecked")
39 public class DatastoreContextConfigAdminOverlayTest {
42 private BundleContext mockBundleContext;
45 private ServiceReference<ConfigurationAdmin> mockConfigAdminServiceRef;
48 private ConfigurationAdmin mockConfigAdmin;
51 private Configuration mockConfig;
54 private DatastoreContextIntrospector mockIntrospector;
57 private ServiceRegistration<?> configListenerServiceReg;
60 public void setup() throws IOException {
61 MockitoAnnotations.initMocks(this);
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));
68 doReturn(mockConfig).when(mockConfigAdmin).getConfiguration(DatastoreContextConfigAdminOverlay.CONFIG_ID);
70 doReturn(DatastoreContextConfigAdminOverlay.CONFIG_ID).when(mockConfig).getPid();
75 public void testUpdateOnConstruction() {
76 Dictionary<String, Object> properties = new Hashtable<>();
77 properties.put("property", "value");
78 doReturn(properties).when(mockConfig).getProperties();
80 DatastoreContextConfigAdminOverlay overlay = new DatastoreContextConfigAdminOverlay(
81 mockIntrospector, mockBundleContext);
83 verify(mockIntrospector).update(properties);
85 verify(mockBundleContext).ungetService(mockConfigAdminServiceRef);
91 public void testUpdateOnConfigurationEvent() {
92 DatastoreContextConfigAdminOverlay overlay = new DatastoreContextConfigAdminOverlay(
93 mockIntrospector, mockBundleContext);
95 reset(mockIntrospector);
97 DatastoreContext context = DatastoreContext.newBuilder().build();
98 doReturn(context).when(mockIntrospector).getContext();
99 DatastoreContextFactory contextFactory = new DatastoreContextFactory(mockIntrospector);
100 doReturn(contextFactory).when(mockIntrospector).newContextFactory();
102 DatastoreContextConfigAdminOverlay.Listener mockListener =
103 mock(DatastoreContextConfigAdminOverlay.Listener.class);
105 overlay.setListener(mockListener);
107 Dictionary<String, Object> properties = new Hashtable<>();
108 properties.put("property", "value");
109 doReturn(properties).when(mockConfig).getProperties();
111 doReturn(true).when(mockIntrospector).update(properties);
113 ArgumentCaptor<ConfigurationListener> configListener =
114 ArgumentCaptor.forClass(ConfigurationListener.class);
115 verify(mockBundleContext).registerService(eq(ConfigurationListener.class.getName()),
116 configListener.capture(), any(Dictionary.class));
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();
123 configListener.getValue().configurationEvent(configEvent);
125 verify(mockIntrospector).update(properties);
127 verify(mockListener).onDatastoreContextUpdated(contextFactory);
129 verify(mockBundleContext, times(2)).ungetService(mockConfigAdminServiceRef);
133 verify(configListenerServiceReg).unregister();
137 public void testConfigurationEventWithDifferentPid() {
138 DatastoreContextConfigAdminOverlay overlay = new DatastoreContextConfigAdminOverlay(
139 mockIntrospector, mockBundleContext);
141 reset(mockIntrospector);
143 ArgumentCaptor<ConfigurationListener> configListener =
144 ArgumentCaptor.forClass(ConfigurationListener.class);
145 verify(mockBundleContext).registerService(eq(ConfigurationListener.class.getName()),
146 configListener.capture(), any(Dictionary.class));
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();
153 configListener.getValue().configurationEvent(configEvent);
155 verify(mockIntrospector, times(0)).update(any(Dictionary.class));
161 public void testConfigurationEventWithNonUpdateEventType() {
162 DatastoreContextConfigAdminOverlay overlay = new DatastoreContextConfigAdminOverlay(
163 mockIntrospector, mockBundleContext);
165 reset(mockIntrospector);
167 ArgumentCaptor<ConfigurationListener> configListener =
168 ArgumentCaptor.forClass(ConfigurationListener.class);
169 verify(mockBundleContext).registerService(eq(ConfigurationListener.class.getName()),
170 configListener.capture(), any(Dictionary.class));
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();
177 configListener.getValue().configurationEvent(configEvent);
179 verify(mockIntrospector, times(0)).update(any(Dictionary.class));