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