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