Bug 4105: Remove EntityOwnershipCandidate
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / entityownership / EntityOwnershipListenerSupportTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore.entityownership;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.never;
14 import static org.mockito.Mockito.reset;
15 import static org.mockito.Mockito.timeout;
16 import static org.mockito.Mockito.verify;
17 import akka.actor.ActorContext;
18 import akka.actor.ActorRef;
19 import akka.actor.Props;
20 import akka.testkit.JavaTestKit;
21 import akka.testkit.TestActorRef;
22 import java.util.ArrayList;
23 import java.util.List;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.controller.cluster.raft.TestActorFactory;
28 import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
29 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
30 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
31 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import scala.collection.Iterator;
35 import scala.collection.immutable.Iterable;
36
37 /**
38  * Unit tests for EntityOwnershipListenerSupport.
39  *
40  * @author Thomas Pantelis
41  */
42 public class EntityOwnershipListenerSupportTest extends AbstractEntityOwnershipTest {
43     private final TestActorFactory actorFactory = new TestActorFactory(getSystem());
44     private ActorContext actorContext;
45
46     @Before
47     public void setup() {
48         TestActorRef<DoNothingActor> actor = actorFactory.createTestActor(
49                 Props.create(DoNothingActor.class), actorFactory.generateActorId("test"));
50
51         actorContext = actor.underlyingActor().getContext();
52     }
53
54     @After
55     public void tearDown() {
56         actorFactory.close();
57     }
58
59     @Test
60     public void testNotifyEntityOwnershipListeners() {
61         EntityOwnershipListenerSupport support = new EntityOwnershipListenerSupport(actorContext, "test");
62
63         EntityOwnershipListener mockListener1 = mock(EntityOwnershipListener.class, "EntityOwnershipListener1");
64         EntityOwnershipListener mockListener2 = mock(EntityOwnershipListener.class, "EntityOwnershipListener2");
65         EntityOwnershipListener mockListener1_2 = mock(EntityOwnershipListener.class, "EntityOwnershipListener1_2");
66         String entityType1 = "type1";
67         String entityType2 = "type2";
68         Entity entity1 = new Entity(entityType1, YangInstanceIdentifier.of(QName.create("test", "id1")));
69         Entity entity2 = new Entity(entityType2, YangInstanceIdentifier.of(QName.create("test", "id2")));
70         Entity entity3 = new Entity("noListener", YangInstanceIdentifier.of(QName.create("test", "id5")));
71
72         // Add EntityOwnershipListener registrations.
73
74         support.addEntityOwnershipListener(entityType1, mockListener1);
75         support.addEntityOwnershipListener(entityType1, mockListener1); // register again - should be noop
76         support.addEntityOwnershipListener(entityType1, mockListener1_2);
77         support.addEntityOwnershipListener(entityType2, mockListener2);
78
79         // Notify entity1 changed and verify appropriate listeners are notified.
80
81         support.notifyEntityOwnershipListeners(entity1, false, true, true);
82
83         verify(mockListener1, timeout(5000)).ownershipChanged(ownershipChange(entity1, false, true, true));
84         verify(mockListener1_2, timeout(5000)).ownershipChanged(ownershipChange(entity1, false, true, true));
85         verify(mockListener2, timeout(300).never()).ownershipChanged(any(EntityOwnershipChange.class));
86         assertEquals("# of listener actors", 2, actorContext.children().size());
87         reset(mockListener1, mockListener2, mockListener1_2);
88
89         // Notify entity2 changed and verify appropriate listeners are notified.
90
91         support.notifyEntityOwnershipListeners(entity2, false, true, true);
92
93         verify(mockListener2, timeout(5000)).ownershipChanged(ownershipChange(entity2, false, true, true));
94         verify(mockListener1, timeout(300).never()).ownershipChanged(any(EntityOwnershipChange.class));
95         verify(mockListener1_2, never()).ownershipChanged(any(EntityOwnershipChange.class));
96         assertEquals("# of listener actors", 3, actorContext.children().size());
97         reset(mockListener1, mockListener2, mockListener1_2);
98
99         // Notify entity3 changed and verify no listeners are notified.
100
101         support.notifyEntityOwnershipListeners(entity3, true, false, true);
102
103         verify(mockListener1, timeout(300).never()).ownershipChanged(any(EntityOwnershipChange.class));
104         verify(mockListener2, never()).ownershipChanged(any(EntityOwnershipChange.class));
105         verify(mockListener1_2, never()).ownershipChanged(any(EntityOwnershipChange.class));
106         reset(mockListener1, mockListener2, mockListener1_2);
107
108         Iterable<ActorRef> listenerActors = actorContext.children();
109         assertEquals("# of listener actors", 3, listenerActors.size());
110
111         // Unregister mockListener1, issue a change for entity1 and verify only remaining listeners are notified.
112
113         support.removeEntityOwnershipListener(entityType1, mockListener1);
114         support.notifyEntityOwnershipListeners(entity1, true, false, true);
115
116         verify(mockListener1_2, timeout(5000)).ownershipChanged(ownershipChange(entity1, true, false, true));
117         verify(mockListener1, timeout(300).never()).ownershipChanged(any(EntityOwnershipChange.class));
118         reset(mockListener1, mockListener2, mockListener1_2);
119
120         // Unregister all listeners and verify their listener actors are destroyed.
121
122         List<JavaTestKit> watchers = new ArrayList<>();
123         for(Iterator<ActorRef> iter = listenerActors.iterator(); iter.hasNext();) {
124             JavaTestKit kit = new JavaTestKit(getSystem());
125             kit.watch(iter.next());
126             watchers.add(kit);
127         }
128
129         support.removeEntityOwnershipListener(entityType1, mockListener1_2);
130         support.removeEntityOwnershipListener(entityType1, mockListener1_2); // un-register again - should be noop
131         support.removeEntityOwnershipListener(entityType2, mockListener2);
132
133         Iterator<ActorRef> iter = listenerActors.iterator();
134         for(JavaTestKit kit: watchers) {
135             kit.expectTerminated(JavaTestKit.duration("3 seconds"), iter.next());
136         }
137
138         assertEquals("# of listener actors", 0, actorContext.children().size());
139
140         // Re-register mockListener1 and verify it is notified.
141
142         reset(mockListener1, mockListener2);
143
144         support.addEntityOwnershipListener(entityType1, mockListener1);
145         support.notifyEntityOwnershipListeners(entity1, false, false, true);
146
147         verify(mockListener1, timeout(5000)).ownershipChanged(ownershipChange(entity1, false, false, true));
148         verify(mockListener1_2, never()).ownershipChanged(any(EntityOwnershipChange.class));
149         verify(mockListener2, never()).ownershipChanged(any(EntityOwnershipChange.class));
150
151         // Quickly register and unregister mockListener2 - expecting no exceptions.
152
153         support.addEntityOwnershipListener(entityType1, mockListener2);
154         support.removeEntityOwnershipListener(entityType1, mockListener2);
155     }
156
157     @Test
158     public void testHasCandidateForEntity() {
159         EntityOwnershipListenerSupport support = new EntityOwnershipListenerSupport(actorContext, "test");
160         Entity entity = new Entity("type", YangInstanceIdentifier.of(QName.create("test", "id")));
161
162         assertEquals("hasCandidateForEntity", false, support.hasCandidateForEntity(entity));
163
164         support.setHasCandidateForEntity(entity);
165         support.setHasCandidateForEntity(entity); // set again - should be noop
166         assertEquals("hasCandidateForEntity", true, support.hasCandidateForEntity(entity));
167
168         support.unsetHasCandidateForEntity(entity);
169         assertEquals("hasCandidateForEntity", false, support.hasCandidateForEntity(entity));
170
171         support.unsetHasCandidateForEntity(entity); // unset again - should be noop
172         assertEquals("hasCandidateForEntity", false, support.hasCandidateForEntity(entity));
173
174         support.setHasCandidateForEntity(entity);
175         assertEquals("hasCandidateForEntity", true, support.hasCandidateForEntity(entity));
176     }
177 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.