2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
10 import akka.actor.ActorRef;
11 import akka.actor.DeadLetter;
12 import akka.actor.Props;
13 import akka.testkit.JavaTestKit;
14 import com.google.common.collect.ImmutableList;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.mockito.Matchers;
18 import org.mockito.Mockito;
19 import org.opendaylight.controller.cluster.datastore.messages.DataTreeChanged;
20 import org.opendaylight.controller.cluster.datastore.messages.DataTreeChangedReply;
21 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
25 public class DataTreeChangeListenerActorTest extends AbstractActorTest {
28 public void testDataChangedWhenNotificationsAreEnabled(){
29 new JavaTestKit(getSystem()) {{
30 final DataTreeCandidate mockTreeCandidate = Mockito.mock(DataTreeCandidate.class);
31 final ImmutableList<DataTreeCandidate> mockCandidates = ImmutableList.of(mockTreeCandidate);
32 final DOMDataTreeChangeListener mockListener = Mockito.mock(DOMDataTreeChangeListener.class);
33 final Props props = DataTreeChangeListenerActor.props(mockListener);
34 final ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedNotificationsEnabled");
36 // Let the DataChangeListener know that notifications should be enabled
37 subject.tell(new EnableNotification(true), getRef());
39 subject.tell(new DataTreeChanged(mockCandidates),
42 expectMsgClass(DataTreeChangedReply.class);
44 Mockito.verify(mockListener).onDataTreeChanged(mockCandidates);
49 public void testDataChangedWhenNotificationsAreDisabled(){
50 new JavaTestKit(getSystem()) {{
51 final DataTreeCandidate mockTreeCandidate = Mockito.mock(DataTreeCandidate.class);
52 final ImmutableList<DataTreeCandidate> mockCandidates = ImmutableList.of(mockTreeCandidate);
53 final DOMDataTreeChangeListener mockListener = Mockito.mock(DOMDataTreeChangeListener.class);
54 final Props props = DataTreeChangeListenerActor.props(mockListener);
55 final ActorRef subject =
56 getSystem().actorOf(props, "testDataTreeChangedNotificationsDisabled");
58 subject.tell(new DataTreeChanged(mockCandidates),
61 new Within(duration("1 seconds")) {
63 protected void run() {
66 Mockito.verify(mockListener, Mockito.never()).onDataTreeChanged(
67 Matchers.anyCollectionOf(DataTreeCandidate.class));
74 public void testDataChangedWithNoSender(){
75 new JavaTestKit(getSystem()) {{
76 final DataTreeCandidate mockTreeCandidate = Mockito.mock(DataTreeCandidate.class);
77 final ImmutableList<DataTreeCandidate> mockCandidates = ImmutableList.of(mockTreeCandidate);
78 final DOMDataTreeChangeListener mockListener = Mockito.mock(DOMDataTreeChangeListener.class);
79 final Props props = DataTreeChangeListenerActor.props(mockListener);
80 final ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedWithNoSender");
82 getSystem().eventStream().subscribe(getRef(), DeadLetter.class);
84 subject.tell(new DataTreeChanged(mockCandidates), ActorRef.noSender());
86 // Make sure no DataChangedReply is sent to DeadLetters.
88 DeadLetter deadLetter;
90 deadLetter = expectMsgClass(duration("1 seconds"), DeadLetter.class);
91 } catch (AssertionError e) {
92 // Timed out - got no DeadLetter - this is good
96 // We may get DeadLetters for other messages we don't care about.
97 Assert.assertFalse("Unexpected DataTreeChangedReply",
98 deadLetter.message() instanceof DataTreeChangedReply);
104 public void testDataChangedWithListenerRuntimeEx(){
105 new JavaTestKit(getSystem()) {{
106 final DataTreeCandidate mockTreeCandidate1 = Mockito.mock(DataTreeCandidate.class);
107 final ImmutableList<DataTreeCandidate> mockCandidates1 = ImmutableList.of(mockTreeCandidate1);
108 final DataTreeCandidate mockTreeCandidate2 = Mockito.mock(DataTreeCandidate.class);
109 final ImmutableList<DataTreeCandidate> mockCandidates2 = ImmutableList.of(mockTreeCandidate2);
110 final DataTreeCandidate mockTreeCandidate3 = Mockito.mock(DataTreeCandidate.class);
111 final ImmutableList<DataTreeCandidate> mockCandidates3 = ImmutableList.of(mockTreeCandidate3);
113 final DOMDataTreeChangeListener mockListener = Mockito.mock(DOMDataTreeChangeListener.class);
114 Mockito.doThrow(new RuntimeException("mock")).when(mockListener).onDataTreeChanged(mockCandidates2);
116 Props props = DataTreeChangeListenerActor.props(mockListener);
117 ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedWithListenerRuntimeEx");
119 // Let the DataChangeListener know that notifications should be enabled
120 subject.tell(new EnableNotification(true), getRef());
122 subject.tell(new DataTreeChanged(mockCandidates1),getRef());
123 expectMsgClass(DataTreeChangedReply.class);
125 subject.tell(new DataTreeChanged(mockCandidates2),getRef());
126 expectMsgClass(DataTreeChangedReply.class);
128 subject.tell(new DataTreeChanged(mockCandidates3),getRef());
129 expectMsgClass(DataTreeChangedReply.class);
131 Mockito.verify(mockListener).onDataTreeChanged(mockCandidates1);
132 Mockito.verify(mockListener).onDataTreeChanged(mockCandidates2);
133 Mockito.verify(mockListener).onDataTreeChanged(mockCandidates3);