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