Eliminate use of deprecated mockito methods
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DataTreeChangeListenerActorTest.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.controller.cluster.datastore;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.mockito.ArgumentMatchers.anyCollection;
12 import static org.mockito.Mockito.doThrow;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.never;
15 import static org.mockito.Mockito.verify;
16 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.TEST_PATH;
17
18 import akka.actor.ActorRef;
19 import akka.actor.DeadLetter;
20 import akka.actor.Props;
21 import akka.testkit.javadsl.TestKit;
22 import com.google.common.collect.ImmutableList;
23 import java.time.Duration;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.controller.cluster.datastore.messages.DataTreeChanged;
27 import org.opendaylight.controller.cluster.datastore.messages.DataTreeChangedReply;
28 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
31
32 public class DataTreeChangeListenerActorTest extends AbstractActorTest {
33     private TestKit testKit;
34
35     @Before
36     public void before() {
37         testKit = new TestKit(getSystem());
38     }
39
40     @Test
41     public void testDataChangedWhenNotificationsAreEnabled() {
42         final DataTreeCandidate mockTreeCandidate = mock(DataTreeCandidate.class);
43         final ImmutableList<DataTreeCandidate> mockCandidates = ImmutableList.of(mockTreeCandidate);
44         final DOMDataTreeChangeListener mockListener = mock(DOMDataTreeChangeListener.class);
45         final Props props = DataTreeChangeListenerActor.props(mockListener, TEST_PATH);
46         final ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedNotificationsEnabled");
47
48         // Let the DataChangeListener know that notifications should be
49         // enabled
50         subject.tell(new EnableNotification(true, "test"), testKit.getRef());
51
52         subject.tell(new DataTreeChanged(mockCandidates), testKit.getRef());
53
54         testKit.expectMsgClass(DataTreeChangedReply.class);
55
56         verify(mockListener).onDataTreeChanged(mockCandidates);
57     }
58
59     @Test
60     public void testDataChangedWhenNotificationsAreDisabled() {
61         final DataTreeCandidate mockTreeCandidate = mock(DataTreeCandidate.class);
62         final ImmutableList<DataTreeCandidate> mockCandidates = ImmutableList.of(mockTreeCandidate);
63         final DOMDataTreeChangeListener mockListener = mock(DOMDataTreeChangeListener.class);
64         final Props props = DataTreeChangeListenerActor.props(mockListener, TEST_PATH);
65         final ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedNotificationsDisabled");
66
67         subject.tell(new DataTreeChanged(mockCandidates), testKit.getRef());
68
69         testKit.within(Duration.ofSeconds(1), () -> {
70             testKit.expectNoMessage();
71             verify(mockListener, never()).onDataTreeChanged(anyCollection());
72             return null;
73         });
74     }
75
76     @Test
77     public void testDataChangedWithNoSender() {
78         final DataTreeCandidate mockTreeCandidate = mock(DataTreeCandidate.class);
79         final ImmutableList<DataTreeCandidate> mockCandidates = ImmutableList.of(mockTreeCandidate);
80         final DOMDataTreeChangeListener mockListener = mock(DOMDataTreeChangeListener.class);
81         final Props props = DataTreeChangeListenerActor.props(mockListener, TEST_PATH);
82         final ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedWithNoSender");
83
84         getSystem().eventStream().subscribe(testKit.getRef(), DeadLetter.class);
85
86         subject.tell(new DataTreeChanged(mockCandidates), ActorRef.noSender());
87
88         // Make sure no DataChangedReply is sent to DeadLetters.
89         while (true) {
90             DeadLetter deadLetter;
91             try {
92                 deadLetter = testKit.expectMsgClass(Duration.ofSeconds(1), DeadLetter.class);
93             } catch (AssertionError e) {
94                 // Timed out - got no DeadLetter - this is good
95                 break;
96             }
97
98             // We may get DeadLetters for other messages we don't care
99             // about.
100             assertFalse("Unexpected DataTreeChangedReply", deadLetter.message() instanceof DataTreeChangedReply);
101         }
102     }
103
104     @Test
105     public void testDataChangedWithListenerRuntimeEx() {
106         final DataTreeCandidate mockTreeCandidate1 = mock(DataTreeCandidate.class);
107         final ImmutableList<DataTreeCandidate> mockCandidates1 = ImmutableList.of(mockTreeCandidate1);
108         final DataTreeCandidate mockTreeCandidate2 = mock(DataTreeCandidate.class);
109         final ImmutableList<DataTreeCandidate> mockCandidates2 = ImmutableList.of(mockTreeCandidate2);
110         final DataTreeCandidate mockTreeCandidate3 = mock(DataTreeCandidate.class);
111         final ImmutableList<DataTreeCandidate> mockCandidates3 = ImmutableList.of(mockTreeCandidate3);
112
113         final DOMDataTreeChangeListener mockListener = mock(DOMDataTreeChangeListener.class);
114         doThrow(new RuntimeException("mock")).when(mockListener).onDataTreeChanged(mockCandidates2);
115
116         Props props = DataTreeChangeListenerActor.props(mockListener, TEST_PATH);
117         ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedWithListenerRuntimeEx");
118
119         // Let the DataChangeListener know that notifications should be
120         // enabled
121         subject.tell(new EnableNotification(true, "test"), testKit.getRef());
122
123         subject.tell(new DataTreeChanged(mockCandidates1), testKit.getRef());
124         testKit.expectMsgClass(DataTreeChangedReply.class);
125
126         subject.tell(new DataTreeChanged(mockCandidates2), testKit.getRef());
127         testKit.expectMsgClass(DataTreeChangedReply.class);
128
129         subject.tell(new DataTreeChanged(mockCandidates3), testKit.getRef());
130         testKit.expectMsgClass(DataTreeChangedReply.class);
131
132         verify(mockListener).onDataTreeChanged(mockCandidates1);
133         verify(mockListener).onDataTreeChanged(mockCandidates2);
134         verify(mockListener).onDataTreeChanged(mockCandidates3);
135     }
136 }

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