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