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