Eliminate use of deprecated mockito methods
[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.ArgumentMatchers.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
18 import akka.actor.ActorContext;
19 import akka.actor.ActorRef;
20 import akka.actor.Props;
21 import akka.testkit.TestActorRef;
22 import akka.testkit.javadsl.TestKit;
23 import com.google.common.util.concurrent.Uninterruptibles;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.concurrent.TimeUnit;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.opendaylight.controller.cluster.raft.TestActorFactory;
31 import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
32 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
33 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange;
34 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import scala.collection.Iterator;
38 import scala.collection.immutable.Iterable;
39
40 /**
41  * Unit tests for EntityOwnershipListenerSupport.
42  *
43  * @author Thomas Pantelis
44  */
45 public class EntityOwnershipListenerSupportTest extends AbstractEntityOwnershipTest {
46     private final TestActorFactory actorFactory = new TestActorFactory(getSystem());
47     private ActorContext actorContext;
48
49     @Before
50     public void setup() {
51         TestActorRef<DoNothingActor> actor = actorFactory.createTestActor(
52                 Props.create(DoNothingActor.class), actorFactory.generateActorId("test"));
53
54         actorContext = actor.underlyingActor().getContext();
55     }
56
57     @After
58     public void tearDown() {
59         actorFactory.close();
60     }
61
62     @Test
63     public void testNotifyEntityOwnershipListeners() {
64         EntityOwnershipListenerSupport support = new EntityOwnershipListenerSupport(actorContext, "test");
65
66         DOMEntityOwnershipListener mockListener1 = mock(DOMEntityOwnershipListener.class, "EntityOwnershipListener1");
67         DOMEntityOwnershipListener mockListener2 = mock(DOMEntityOwnershipListener.class, "EntityOwnershipListener2");
68         DOMEntityOwnershipListener mockListener12 = mock(DOMEntityOwnershipListener.class,
69                 "EntityOwnershipListener1_2");
70         String entityType1 = "type1";
71         String entityType2 = "type2";
72         final DOMEntity entity1 = new DOMEntity(entityType1, YangInstanceIdentifier.of(QName.create("test", "id1")));
73         final DOMEntity entity2 = new DOMEntity(entityType2, YangInstanceIdentifier.of(QName.create("test", "id2")));
74         final DOMEntity entity3 = new DOMEntity("noListener", YangInstanceIdentifier.of(QName.create("test", "id5")));
75
76         // Add EntityOwnershipListener registrations.
77
78         support.addEntityOwnershipListener(entityType1, mockListener1);
79         support.addEntityOwnershipListener(entityType1, mockListener1); // register again - should be noop
80         support.addEntityOwnershipListener(entityType1, mockListener12);
81         support.addEntityOwnershipListener(entityType2, mockListener2);
82
83         // Notify entity1 changed and verify appropriate listeners are notified.
84
85         support.notifyEntityOwnershipListeners(entity1, false, true, true);
86
87         verify(mockListener1, timeout(5000)).ownershipChanged(ownershipChange(entity1, false, true, true));
88         verify(mockListener12, timeout(5000)).ownershipChanged(ownershipChange(entity1, false, true, true));
89         Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
90         verify(mockListener2, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
91         assertEquals("# of listener actors", 2, actorContext.children().size());
92         reset(mockListener1, mockListener2, mockListener12);
93
94         // Notify entity2 changed and verify appropriate listeners are notified.
95
96         support.notifyEntityOwnershipListeners(entity2, false, true, true);
97
98         verify(mockListener2, timeout(5000)).ownershipChanged(ownershipChange(entity2, false, true, true));
99         Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
100         verify(mockListener1, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
101         verify(mockListener12, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
102         assertEquals("# of listener actors", 3, actorContext.children().size());
103         reset(mockListener1, mockListener2, mockListener12);
104
105         // Notify entity3 changed and verify no listeners are notified.
106
107         support.notifyEntityOwnershipListeners(entity3, true, false, true);
108
109         Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
110         verify(mockListener1, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
111         verify(mockListener2, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
112         verify(mockListener12, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
113         reset(mockListener1, mockListener2, mockListener12);
114
115         Iterable<ActorRef> listenerActors = actorContext.children();
116         assertEquals("# of listener actors", 3, listenerActors.size());
117
118         // Unregister mockListener1, issue a change for entity1 and verify only remaining listeners are notified.
119
120         support.removeEntityOwnershipListener(entityType1, mockListener1);
121         support.notifyEntityOwnershipListeners(entity1, true, false, true);
122
123         verify(mockListener12, timeout(5000)).ownershipChanged(ownershipChange(entity1, true, false, true));
124         Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
125         verify(mockListener1, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
126         reset(mockListener1, mockListener2, mockListener12);
127
128         // Unregister all listeners and verify their listener actors are destroyed.
129
130         List<TestKit> watchers = new ArrayList<>();
131         for (Iterator<ActorRef> iter = listenerActors.iterator(); iter.hasNext();) {
132             TestKit kit = new TestKit(getSystem());
133             kit.watch(iter.next());
134             watchers.add(kit);
135         }
136
137         support.removeEntityOwnershipListener(entityType1, mockListener12);
138         support.removeEntityOwnershipListener(entityType1, mockListener12); // un-register again - should be noop
139         support.removeEntityOwnershipListener(entityType2, mockListener2);
140
141         Iterator<ActorRef> iter = listenerActors.iterator();
142         for (TestKit kit: watchers) {
143             kit.expectTerminated(kit.duration("3 seconds"), iter.next());
144         }
145
146         assertEquals("# of listener actors", 0, actorContext.children().size());
147
148         // Re-register mockListener1 and verify it is notified.
149
150         reset(mockListener1, mockListener2);
151
152         support.addEntityOwnershipListener(entityType1, mockListener1);
153         support.notifyEntityOwnershipListeners(entity1, false, false, true);
154
155         verify(mockListener1, timeout(5000)).ownershipChanged(ownershipChange(entity1, false, false, true));
156         verify(mockListener12, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
157         verify(mockListener2, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
158
159         // Quickly register and unregister mockListener2 - expecting no exceptions.
160
161         support.addEntityOwnershipListener(entityType1, mockListener2);
162         support.removeEntityOwnershipListener(entityType1, mockListener2);
163     }
164 }

©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.