f9a919fdae93806546cceff75f0c7db8af46a7bb
[mdsal.git] / singleton-service / mdsal-singleton-dom-impl / src / test / java / org / opendaylight / mdsal / singleton / dom / impl / ActiveServiceGroupTest.java
1 /*
2  * Copyright (c) 2016 Cisco 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.mdsal.singleton.dom.impl;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertThrows;
14 import static org.junit.Assert.assertTrue;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.never;
18 import static org.mockito.Mockito.verify;
19 import static org.opendaylight.mdsal.eos.common.api.EntityOwnershipStateChange.LOCAL_OWNERSHIP_GRANTED;
20 import static org.opendaylight.mdsal.eos.common.api.EntityOwnershipStateChange.LOCAL_OWNERSHIP_LOST_NEW_OWNER;
21 import static org.opendaylight.mdsal.eos.common.api.EntityOwnershipStateChange.LOCAL_OWNERSHIP_LOST_NO_OWNER;
22 import static org.opendaylight.mdsal.eos.common.api.EntityOwnershipStateChange.REMOTE_OWNERSHIP_CHANGED;
23 import static org.opendaylight.mdsal.eos.common.api.EntityOwnershipStateChange.REMOTE_OWNERSHIP_LOST_NO_OWNER;
24 import static org.opendaylight.mdsal.singleton.dom.impl.EOSClusterSingletonServiceProvider.CLOSE_SERVICE_ENTITY_TYPE;
25 import static org.opendaylight.mdsal.singleton.dom.impl.EOSClusterSingletonServiceProvider.SERVICE_ENTITY_TYPE;
26
27 import com.google.common.util.concurrent.Futures;
28 import com.google.common.util.concurrent.ListenableFuture;
29 import java.util.concurrent.ExecutionException;
30 import org.eclipse.jdt.annotation.NonNull;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.junit.MockitoJUnitRunner;
36 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
37 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
38 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener;
39 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipService;
40 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
41 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
42 import org.opendaylight.yangtools.concepts.Registration;
43
44 /**
45  * Testing {@link ActiveServiceGroup}.
46  */
47 @RunWith(MockitoJUnitRunner.StrictStubs.class)
48 public class ActiveServiceGroupTest {
49     private static final String SERVICE_IDENTIFIER = "TestServiceIdent";
50     private static final ServiceGroupIdentifier SERVICE_GROUP_IDENT = new ServiceGroupIdentifier(SERVICE_IDENTIFIER);
51
52     public static final @NonNull DOMEntity MAIN_ENTITY = new DOMEntity(SERVICE_ENTITY_TYPE, SERVICE_IDENTIFIER);
53     public static final @NonNull DOMEntity CLOSE_ENTITY = new DOMEntity(CLOSE_SERVICE_ENTITY_TYPE, SERVICE_IDENTIFIER);
54
55     @Mock
56     public ClusterSingletonService mockClusterSingletonService;
57     @Mock
58     public ClusterSingletonService mockClusterSingletonServiceSecond;
59     @Mock
60     public Registration mockEntityCandReg;
61     @Mock
62     public Registration mockCloseEntityCandReg;
63     @Mock
64     public DOMEntityOwnershipListener mockEosListener;
65     @Mock
66     public DOMEntityOwnershipService mockEosService;
67
68     private ActiveServiceGroup singletonServiceGroup;
69     private ServiceRegistration firstReg;
70     private ServiceRegistration secondReg;
71
72     /**
73      * Initialization functionality for every Tests in this suite.
74      *
75      * @throws CandidateAlreadyRegisteredException unexpected exception.
76      */
77     @Before
78     public void setup() throws CandidateAlreadyRegisteredException {
79         doReturn(mockEntityCandReg).when(mockEosService).registerCandidate(MAIN_ENTITY);
80         doReturn(mockCloseEntityCandReg).when(mockEosService).registerCandidate(CLOSE_ENTITY);
81         doNothing().when(mockEntityCandReg).close();
82         doNothing().when(mockCloseEntityCandReg).close();
83         doNothing().when(mockClusterSingletonService).instantiateServiceInstance();
84         doReturn(Futures.immediateFuture(null)).when(mockClusterSingletonService).closeServiceInstance();
85
86         doReturn(SERVICE_GROUP_IDENT).when(mockClusterSingletonService).getIdentifier();
87         doReturn(SERVICE_GROUP_IDENT).when(mockClusterSingletonServiceSecond).getIdentifier();
88
89         firstReg = new ServiceRegistration(mockClusterSingletonService) {
90             @Override
91             protected void removeRegistration() {
92                 // No-op
93             }
94         };
95         secondReg = new ServiceRegistration(mockClusterSingletonServiceSecond) {
96             @Override
97             protected void removeRegistration() {
98                 // No-op
99             }
100         };
101
102         singletonServiceGroup = new ActiveServiceGroup(SERVICE_IDENTIFIER, MAIN_ENTITY, CLOSE_ENTITY, mockEosService);
103     }
104
105     /**
106      * Test NULL ServiceIdent input for new ServiceGroup instance.
107      */
108     @Test
109     public void instantiationClusterSingletonServiceGroupNullIdentTest() {
110         assertThrows(NullPointerException.class,
111             () -> new ActiveServiceGroup(null, MAIN_ENTITY, CLOSE_ENTITY, mockEosService));
112     }
113
114     /**
115      * Test empty ServiceIdent input for new ServiceGroup instance.
116      */
117     @Test
118     public void instantiationClusterSingletonServiceGroupEmptyIdentTest() {
119         assertThrows(IllegalArgumentException.class,
120             () -> new ActiveServiceGroup("", MAIN_ENTITY, CLOSE_ENTITY, mockEosService));
121     }
122
123     /**
124      * Test NULL MainEntity input for new ServiceGroup instance.
125      */
126     @Test
127     public void instantiationClusterSingletonServiceGroupNullMainEntityTest() {
128         assertThrows(NullPointerException.class,
129             () -> new ActiveServiceGroup(SERVICE_IDENTIFIER, null, CLOSE_ENTITY, mockEosService));
130     }
131
132     /**
133      * Test NULL CloseEntity input for new ServiceGroup instance.
134      */
135     @Test
136     public void instantiationClusterSingletonServiceGroupNullCloseEntityTest() {
137         assertThrows(NullPointerException.class,
138             () -> new ActiveServiceGroup(SERVICE_IDENTIFIER, MAIN_ENTITY, null, mockEosService));
139     }
140
141     /**
142      * Test NULL EntityOwnershipService input for new ServiceGroup instance.
143      */
144     @Test
145     public void instantiationClusterSingletonServiceGroupNullEOS_Test() {
146         assertThrows(NullPointerException.class,
147             () -> new ActiveServiceGroup(SERVICE_IDENTIFIER, MAIN_ENTITY, CLOSE_ENTITY, null));
148     }
149
150     /**
151      * Test GoldPath for initialization ServiceGroup.
152      */
153     @Test
154     public void initializationClusterSingletonServiceGroupTest() throws CandidateAlreadyRegisteredException {
155         singletonServiceGroup.initialize();
156         verify(mockEosService).registerCandidate(MAIN_ENTITY);
157     }
158
159     /**
160      * Test GoldPath for NO-TO-SLAVE entity Candidate role change.
161      *
162      * @throws CandidateAlreadyRegisteredException - unexpected exception
163      */
164     @Test
165     public void initializationSlaveTest() throws CandidateAlreadyRegisteredException {
166         singletonServiceGroup.initialize();
167         verify(mockEosService).registerCandidate(MAIN_ENTITY);
168         singletonServiceGroup.registerService(firstReg);
169         singletonServiceGroup.ownershipChanged(MAIN_ENTITY, LOCAL_OWNERSHIP_LOST_NEW_OWNER, false);
170         verify(mockClusterSingletonService, never()).instantiateServiceInstance();
171         verify(mockEosService, never()).registerCandidate(CLOSE_ENTITY);
172     }
173
174     /**
175      * Test GoldPath for NO-TO-SLAVE but without MASTER entity Candidate role change.
176      *
177      * @throws CandidateAlreadyRegisteredException - unexpected exception
178      */
179     @Test
180     public void initializationNoMasterTest() throws CandidateAlreadyRegisteredException {
181         singletonServiceGroup.initialize();
182         verify(mockEosService).registerCandidate(MAIN_ENTITY);
183         singletonServiceGroup.registerService(firstReg);
184         singletonServiceGroup.ownershipChanged(MAIN_ENTITY, LOCAL_OWNERSHIP_LOST_NO_OWNER, false);
185         verify(mockClusterSingletonService, never()).instantiateServiceInstance();
186         verify(mockEosService, never()).registerCandidate(CLOSE_ENTITY);
187     }
188
189     /**
190      * Test GoldPath for InJeopardy entity Candidate role change.
191      *
192      * @throws CandidateAlreadyRegisteredException - unexpected exception
193      */
194     @Test
195     public void initializationInJeopardyTest() throws CandidateAlreadyRegisteredException {
196         singletonServiceGroup.initialize();
197         verify(mockEosService).registerCandidate(MAIN_ENTITY);
198         singletonServiceGroup.registerService(firstReg);
199         singletonServiceGroup.ownershipChanged(MAIN_ENTITY, REMOTE_OWNERSHIP_LOST_NO_OWNER, true);
200         verify(mockClusterSingletonService, never()).instantiateServiceInstance();
201         verify(mockEosService, never()).registerCandidate(CLOSE_ENTITY);
202     }
203
204     /**
205      * Test GoldPath for registration SingletonService.
206      *
207      * @throws CandidateAlreadyRegisteredException - unexpected exception
208      */
209     @Test
210     public void serviceRegistrationClusterSingletonServiceGroupTest() throws CandidateAlreadyRegisteredException {
211         singletonServiceGroup.initialize();
212         verify(mockEosService).registerCandidate(MAIN_ENTITY);
213         singletonServiceGroup.registerService(firstReg);
214     }
215
216     /**
217      * Test GoldPath for registration SingletonService.
218      *
219      * @throws CandidateAlreadyRegisteredException - unexpected exception
220      */
221     @Test
222     public void serviceRegistrationClusterSingletonServiceGroupTwoServiceTest()
223             throws CandidateAlreadyRegisteredException {
224         singletonServiceGroup.initialize();
225         verify(mockEosService).registerCandidate(MAIN_ENTITY);
226         singletonServiceGroup.registerService(firstReg);
227         singletonServiceGroup.registerService(secondReg);
228     }
229
230     /**
231      * Test GoldPath for unregistration SingletonService don't call closeServiceInstance
232      * without mastership and don't remove ServiceGroup from map.
233      *
234      * @throws CandidateAlreadyRegisteredException - unexpected exception
235      */
236     @Test
237     public void serviceUnregistrationClusterSingletonServiceGroupTest() throws CandidateAlreadyRegisteredException {
238         singletonServiceGroup.initialize();
239         verify(mockEosService).registerCandidate(MAIN_ENTITY);
240         singletonServiceGroup.registerService(firstReg);
241         assertNotNull(singletonServiceGroup.unregisterService(firstReg));
242         verify(mockClusterSingletonService, never()).closeServiceInstance();
243     }
244
245     /**
246      * Test GoldPath for unregistration SingletonService don't call closeServiceInstance
247      *     without mastership and don't remove ServiceGroup from map.
248      *
249      * @throws CandidateAlreadyRegisteredException - unexpected exception
250      */
251     @Test
252     public void serviceUnregistrationClusterSingletonServiceGroupTwoServicesTest()
253             throws CandidateAlreadyRegisteredException {
254         singletonServiceGroup.initialize();
255         verify(mockEosService).registerCandidate(MAIN_ENTITY);
256         singletonServiceGroup.registerService(firstReg);
257         singletonServiceGroup.registerService(secondReg);
258         assertNull(singletonServiceGroup.unregisterService(firstReg));
259         verify(mockClusterSingletonService, never()).closeServiceInstance();
260     }
261
262     /**
263      * Test GoldPath get Slave role for registered main entity.
264      *
265      * @throws CandidateAlreadyRegisteredException - unexpected exception
266      */
267     @Test
268     public void getSlaveClusterSingletonServiceGroupTest() throws CandidateAlreadyRegisteredException {
269         singletonServiceGroup.initialize();
270         verify(mockEosService).registerCandidate(MAIN_ENTITY);
271         singletonServiceGroup.registerService(firstReg);
272         singletonServiceGroup.ownershipChanged(MAIN_ENTITY, LOCAL_OWNERSHIP_LOST_NEW_OWNER, false);
273         verify(mockClusterSingletonService, never()).instantiateServiceInstance();
274     }
275
276     /**
277      * Test GoldPath get Master role for registered main entity.
278      *
279      * @throws CandidateAlreadyRegisteredException - unexpected exception
280      */
281     @Test
282     public void tryToTakeLeaderClusterSingletonServiceGroupTest() throws CandidateAlreadyRegisteredException {
283         singletonServiceGroup.initialize();
284         verify(mockEosService).registerCandidate(MAIN_ENTITY);
285         singletonServiceGroup.registerService(firstReg);
286         singletonServiceGroup.ownershipChanged(MAIN_ENTITY, LOCAL_OWNERSHIP_GRANTED, false);
287         verify(mockClusterSingletonService, never()).instantiateServiceInstance();
288         verify(mockEosService).registerCandidate(CLOSE_ENTITY);
289     }
290
291     /**
292      * Test GoldPath get Master role for registered close entity.
293      *
294      * @throws CandidateAlreadyRegisteredException - unexpected exception
295      */
296     @Test
297     public void takeMasterClusterSingletonServiceGroupTest() throws CandidateAlreadyRegisteredException {
298         singletonServiceGroup.initialize();
299         verify(mockEosService).registerCandidate(MAIN_ENTITY);
300         singletonServiceGroup.registerService(firstReg);
301         singletonServiceGroup.ownershipChanged(MAIN_ENTITY, LOCAL_OWNERSHIP_GRANTED, false);
302         verify(mockClusterSingletonService, never()).instantiateServiceInstance();
303         verify(mockEosService).registerCandidate(CLOSE_ENTITY);
304         singletonServiceGroup.ownershipChanged(CLOSE_ENTITY, LOCAL_OWNERSHIP_GRANTED, false);
305         verify(mockClusterSingletonService).instantiateServiceInstance();
306     }
307
308     /**
309      * Test GoldPath get Master role for registered entity but initial Slave
310      *     role for closeEntity.
311      *
312      * @throws CandidateAlreadyRegisteredException - unexpected exception
313      */
314     @Test
315     public void waitToTakeMasterClusterSingletonServiceGroupTest() throws CandidateAlreadyRegisteredException {
316         singletonServiceGroup.initialize();
317         verify(mockEosService).registerCandidate(MAIN_ENTITY);
318         singletonServiceGroup.registerService(firstReg);
319         singletonServiceGroup.ownershipChanged(MAIN_ENTITY, LOCAL_OWNERSHIP_GRANTED, false);
320         verify(mockClusterSingletonService, never()).instantiateServiceInstance();
321         verify(mockEosService).registerCandidate(CLOSE_ENTITY);
322         singletonServiceGroup.ownershipChanged(CLOSE_ENTITY, REMOTE_OWNERSHIP_CHANGED, false);
323         verify(mockClusterSingletonService, never()).instantiateServiceInstance();
324         verify(mockClusterSingletonService, never()).closeServiceInstance();
325     }
326
327     /**
328      * Test inJeopardy validation during wait phase for Master role for closeEntity.
329      *
330      * @throws CandidateAlreadyRegisteredException - unexpected exception
331      */
332     @Test
333     public void inJeopardyInWaitPhaseClusterSingletonServiceGroupTest() throws CandidateAlreadyRegisteredException {
334         singletonServiceGroup.initialize();
335         verify(mockEosService).registerCandidate(MAIN_ENTITY);
336         singletonServiceGroup.registerService(firstReg);
337         singletonServiceGroup.ownershipChanged(MAIN_ENTITY, LOCAL_OWNERSHIP_GRANTED, false);
338         verify(mockClusterSingletonService, never()).instantiateServiceInstance();
339         verify(mockEosService).registerCandidate(CLOSE_ENTITY);
340         singletonServiceGroup.ownershipChanged(MAIN_ENTITY, REMOTE_OWNERSHIP_LOST_NO_OWNER, true);
341         verify(mockClusterSingletonService, never()).instantiateServiceInstance();
342         verify(mockClusterSingletonService, never()).closeServiceInstance();
343     }
344
345     /**
346      * Test inJeopardy validation during wait phase for Master role for closeEntity.
347      *
348      * @throws CandidateAlreadyRegisteredException - unexpected exception
349      */
350     @Test
351     public void inJeopardyInWaitPhaseClusterSingletonServiceGroupTwoServiceTest()
352             throws CandidateAlreadyRegisteredException {
353         singletonServiceGroup.initialize();
354         verify(mockEosService).registerCandidate(MAIN_ENTITY);
355         singletonServiceGroup.registerService(firstReg);
356         singletonServiceGroup.registerService(secondReg);
357         singletonServiceGroup.ownershipChanged(MAIN_ENTITY, LOCAL_OWNERSHIP_GRANTED, false);
358         verify(mockClusterSingletonService, never()).instantiateServiceInstance();
359         verify(mockEosService).registerCandidate(CLOSE_ENTITY);
360         singletonServiceGroup.ownershipChanged(MAIN_ENTITY, REMOTE_OWNERSHIP_LOST_NO_OWNER, true);
361         verify(mockClusterSingletonService, never()).instantiateServiceInstance();
362         verify(mockClusterSingletonService, never()).closeServiceInstance();
363     }
364
365     /**
366      * Test inJeopardy validation for holding leadership.
367      *
368      * @throws CandidateAlreadyRegisteredException - unexpected exception
369      */
370     @Test
371     public void inJeopardyLeaderClusterSingletonServiceGroupTest() throws CandidateAlreadyRegisteredException {
372         singletonServiceGroup.initialize();
373         verify(mockEosService).registerCandidate(MAIN_ENTITY);
374         singletonServiceGroup.registerService(firstReg);
375         singletonServiceGroup.ownershipChanged(MAIN_ENTITY, LOCAL_OWNERSHIP_GRANTED, false);
376         verify(mockClusterSingletonService, never()).instantiateServiceInstance();
377         verify(mockEosService).registerCandidate(CLOSE_ENTITY);
378         singletonServiceGroup.ownershipChanged(CLOSE_ENTITY, LOCAL_OWNERSHIP_GRANTED, false);
379         verify(mockClusterSingletonService).instantiateServiceInstance();
380
381         // Base entity in jeopardy should not matter...
382         singletonServiceGroup.ownershipChanged(MAIN_ENTITY, LOCAL_OWNERSHIP_GRANTED, true);
383         verify(mockClusterSingletonService, never()).closeServiceInstance();
384
385         // ... application state is actually guarded by cleanup
386         singletonServiceGroup.ownershipChanged(CLOSE_ENTITY, REMOTE_OWNERSHIP_LOST_NO_OWNER, true);
387         verify(mockClusterSingletonService).closeServiceInstance();
388     }
389
390     /**
391      * Test GoldPath for SLAVE-TO-MASTER entity Candidate role change.
392      *
393      * @throws CandidateAlreadyRegisteredException - unexpected exception
394      */
395     @Test
396     public void lostLeaderClusterSingletonServiceGroupTest() throws CandidateAlreadyRegisteredException {
397         singletonServiceGroup.initialize();
398         verify(mockEosService).registerCandidate(MAIN_ENTITY);
399         singletonServiceGroup.registerService(firstReg);
400         singletonServiceGroup.ownershipChanged(MAIN_ENTITY, LOCAL_OWNERSHIP_GRANTED, false);
401         verify(mockClusterSingletonService, never()).instantiateServiceInstance();
402         verify(mockEosService).registerCandidate(CLOSE_ENTITY);
403         singletonServiceGroup.ownershipChanged(CLOSE_ENTITY, LOCAL_OWNERSHIP_GRANTED, false);
404         verify(mockClusterSingletonService).instantiateServiceInstance();
405         singletonServiceGroup.ownershipChanged(MAIN_ENTITY, LOCAL_OWNERSHIP_LOST_NEW_OWNER, false);
406         verify(mockClusterSingletonService).closeServiceInstance();
407     }
408
409     /**
410      * Test checks validation Error processing for SLAVE-TO-MASTER entity Candidate role change.
411      *     Not initialized provider has to close and remove all singletonServices from Group and
412      *     Group itself remove too.
413      */
414     @Test(expected = IllegalStateException.class)
415     public void tryToTakeLeaderForNotInitializedGroupTest() {
416         singletonServiceGroup.registerService(firstReg);
417     }
418
419     /**
420      * Test checks closing processing for close {@link ServiceRegistration}.
421      *
422      * @throws CandidateAlreadyRegisteredException - unexpected exception
423      */
424     @Test
425     public void checkClosingRegistrationTest() throws CandidateAlreadyRegisteredException {
426         singletonServiceGroup.initialize();
427         verify(mockEosService).registerCandidate(MAIN_ENTITY);
428         singletonServiceGroup.registerService(firstReg);
429         singletonServiceGroup.ownershipChanged(MAIN_ENTITY, LOCAL_OWNERSHIP_GRANTED, false);
430         verify(mockClusterSingletonService, never()).instantiateServiceInstance();
431         verify(mockEosService).registerCandidate(CLOSE_ENTITY);
432         singletonServiceGroup.ownershipChanged(CLOSE_ENTITY, LOCAL_OWNERSHIP_GRANTED, false);
433         verify(mockClusterSingletonService).instantiateServiceInstance();
434         assertNotNull(singletonServiceGroup.unregisterService(firstReg));
435         verify(mockClusterSingletonService, never()).closeServiceInstance();
436         singletonServiceGroup.ownershipChanged(MAIN_ENTITY, LOCAL_OWNERSHIP_LOST_NO_OWNER, false);
437         verify(mockClusterSingletonService).closeServiceInstance();
438     }
439
440     /**
441      * Test checks validation Error processing for MASTER-TO-SLAVE closeEntity Candidate role change.
442      *
443      * @throws CandidateAlreadyRegisteredException - unexpected exception
444      */
445     @Test
446     public void checkClosingUnexpectedDoubleEntityForMasterOwnershipChangeRegistrationTest()
447             throws CandidateAlreadyRegisteredException {
448         singletonServiceGroup.initialize();
449         verify(mockEosService).registerCandidate(MAIN_ENTITY);
450         singletonServiceGroup.registerService(firstReg);
451         singletonServiceGroup.ownershipChanged(MAIN_ENTITY, LOCAL_OWNERSHIP_GRANTED, false);
452         verify(mockClusterSingletonService, never()).instantiateServiceInstance();
453         verify(mockEosService).registerCandidate(CLOSE_ENTITY);
454         singletonServiceGroup.ownershipChanged(CLOSE_ENTITY, LOCAL_OWNERSHIP_GRANTED, false);
455         verify(mockClusterSingletonService).instantiateServiceInstance();
456         singletonServiceGroup.ownershipChanged(CLOSE_ENTITY, LOCAL_OWNERSHIP_LOST_NEW_OWNER, false);
457         verify(mockClusterSingletonService).closeServiceInstance();
458     }
459
460     /**
461      * Test checks validation Error processing for MASTER-TO-SLAVE closeEntity Candidate role change
462      *     without closeEntity registration.
463      *
464      * @throws CandidateAlreadyRegisteredException - unexpected exception
465      */
466     @Test
467     public void checkClosingUnexpectedDoubleEntityForSlaveOwnershipChangeRegistrationTest()
468             throws CandidateAlreadyRegisteredException {
469         singletonServiceGroup.initialize();
470         verify(mockEosService).registerCandidate(MAIN_ENTITY);
471         singletonServiceGroup.registerService(firstReg);
472         singletonServiceGroup.ownershipChanged(MAIN_ENTITY, LOCAL_OWNERSHIP_LOST_NEW_OWNER, false);
473         verify(mockClusterSingletonService, never()).instantiateServiceInstance();
474         verify(mockEosService, never()).registerCandidate(CLOSE_ENTITY);
475         singletonServiceGroup.ownershipChanged(CLOSE_ENTITY, LOCAL_OWNERSHIP_LOST_NEW_OWNER, false);
476         verify(mockClusterSingletonService, never()).closeServiceInstance();
477     }
478
479     @Test
480     public void testRegisterCloseShutdown() throws CandidateAlreadyRegisteredException, InterruptedException,
481             ExecutionException {
482         initializeGroupAndStartService();
483
484         assertNotNull(singletonServiceGroup.unregisterService(firstReg));
485         verify(mockClusterSingletonService, never()).closeServiceInstance();
486         verify(mockEntityCandReg).close();
487
488         final ListenableFuture<?> future = singletonServiceGroup.closeClusterSingletonGroup();
489         assertNotNull(future);
490         assertFalse(future.isDone());
491         verify(mockClusterSingletonService).closeServiceInstance();
492
493         singletonServiceGroup.ownershipChanged(MAIN_ENTITY, LOCAL_OWNERSHIP_LOST_NEW_OWNER, false);
494         verify(mockCloseEntityCandReg).close();
495
496         singletonServiceGroup.ownershipChanged(CLOSE_ENTITY, LOCAL_OWNERSHIP_LOST_NEW_OWNER, false);
497         assertTrue(future.isDone());
498         assertNull(future.get());
499     }
500
501     private void initialize() throws CandidateAlreadyRegisteredException {
502         singletonServiceGroup.initialize();
503         verify(mockEosService).registerCandidate(MAIN_ENTITY);
504     }
505
506     private void initializeGroupAndStartService() throws CandidateAlreadyRegisteredException {
507         initialize();
508         singletonServiceGroup.registerService(firstReg);
509         singletonServiceGroup.ownershipChanged(MAIN_ENTITY, LOCAL_OWNERSHIP_GRANTED, false);
510         verify(mockEosService).registerCandidate(CLOSE_ENTITY);
511         singletonServiceGroup.ownershipChanged(CLOSE_ENTITY, LOCAL_OWNERSHIP_GRANTED, false);
512         verify(mockClusterSingletonService).instantiateServiceInstance();
513     }
514 }