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