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