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 static org.opendaylight.controller.md.cluster.datastore.model.TestModel.TEST_PATH;
12 import akka.actor.ActorRef;
13 import akka.actor.DeadLetter;
14 import akka.actor.Props;
15 import akka.testkit.javadsl.TestKit;
16 import com.google.common.collect.ImmutableList;
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.mockito.Matchers;
20 import org.mockito.Mockito;
21 import org.opendaylight.controller.cluster.datastore.messages.DataTreeChanged;
22 import org.opendaylight.controller.cluster.datastore.messages.DataTreeChangedReply;
23 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
27 public class DataTreeChangeListenerActorTest extends AbstractActorTest {
30 public void testDataChangedWhenNotificationsAreEnabled() {
31 new TestKit(getSystem()) {
33 final DataTreeCandidate mockTreeCandidate = Mockito.mock(DataTreeCandidate.class);
34 final ImmutableList<DataTreeCandidate> mockCandidates = ImmutableList.of(mockTreeCandidate);
35 final DOMDataTreeChangeListener mockListener = Mockito.mock(DOMDataTreeChangeListener.class);
36 final Props props = DataTreeChangeListenerActor.props(mockListener, TEST_PATH);
37 final ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedNotificationsEnabled");
39 // Let the DataChangeListener know that notifications should be
41 subject.tell(new EnableNotification(true, "test"), getRef());
43 subject.tell(new DataTreeChanged(mockCandidates), getRef());
45 expectMsgClass(DataTreeChangedReply.class);
47 Mockito.verify(mockListener).onDataTreeChanged(mockCandidates);
53 public void testDataChangedWhenNotificationsAreDisabled() {
54 new TestKit(getSystem()) {
56 final DataTreeCandidate mockTreeCandidate = Mockito.mock(DataTreeCandidate.class);
57 final ImmutableList<DataTreeCandidate> mockCandidates = ImmutableList.of(mockTreeCandidate);
58 final DOMDataTreeChangeListener mockListener = Mockito.mock(DOMDataTreeChangeListener.class);
59 final Props props = DataTreeChangeListenerActor.props(mockListener, TEST_PATH);
60 final ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedNotificationsDisabled");
62 subject.tell(new DataTreeChanged(mockCandidates), getRef());
64 within(duration("1 seconds"), () -> {
66 Mockito.verify(mockListener, Mockito.never())
67 .onDataTreeChanged(Matchers.anyCollectionOf(DataTreeCandidate.class));
75 public void testDataChangedWithNoSender() {
76 new TestKit(getSystem()) {
78 final DataTreeCandidate mockTreeCandidate = Mockito.mock(DataTreeCandidate.class);
79 final ImmutableList<DataTreeCandidate> mockCandidates = ImmutableList.of(mockTreeCandidate);
80 final DOMDataTreeChangeListener mockListener = Mockito.mock(DOMDataTreeChangeListener.class);
81 final Props props = DataTreeChangeListenerActor.props(mockListener, TEST_PATH);
82 final ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedWithNoSender");
84 getSystem().eventStream().subscribe(getRef(), DeadLetter.class);
86 subject.tell(new DataTreeChanged(mockCandidates), ActorRef.noSender());
88 // Make sure no DataChangedReply is sent to DeadLetters.
90 DeadLetter deadLetter;
92 deadLetter = expectMsgClass(duration("1 seconds"), DeadLetter.class);
93 } catch (AssertionError e) {
94 // Timed out - got no DeadLetter - this is good
98 // We may get DeadLetters for other messages we don't care
100 Assert.assertFalse("Unexpected DataTreeChangedReply",
101 deadLetter.message() instanceof DataTreeChangedReply);
108 public void testDataChangedWithListenerRuntimeEx() {
109 new TestKit(getSystem()) {
111 final DataTreeCandidate mockTreeCandidate1 = Mockito.mock(DataTreeCandidate.class);
112 final ImmutableList<DataTreeCandidate> mockCandidates1 = ImmutableList.of(mockTreeCandidate1);
113 final DataTreeCandidate mockTreeCandidate2 = Mockito.mock(DataTreeCandidate.class);
114 final ImmutableList<DataTreeCandidate> mockCandidates2 = ImmutableList.of(mockTreeCandidate2);
115 final DataTreeCandidate mockTreeCandidate3 = Mockito.mock(DataTreeCandidate.class);
116 final ImmutableList<DataTreeCandidate> mockCandidates3 = ImmutableList.of(mockTreeCandidate3);
118 final DOMDataTreeChangeListener mockListener = Mockito.mock(DOMDataTreeChangeListener.class);
119 Mockito.doThrow(new RuntimeException("mock")).when(mockListener).onDataTreeChanged(mockCandidates2);
121 Props props = DataTreeChangeListenerActor.props(mockListener, TEST_PATH);
122 ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedWithListenerRuntimeEx");
124 // Let the DataChangeListener know that notifications should be
126 subject.tell(new EnableNotification(true, "test"), getRef());
128 subject.tell(new DataTreeChanged(mockCandidates1), getRef());
129 expectMsgClass(DataTreeChangedReply.class);
131 subject.tell(new DataTreeChanged(mockCandidates2), getRef());
132 expectMsgClass(DataTreeChangedReply.class);
134 subject.tell(new DataTreeChanged(mockCandidates3), getRef());
135 expectMsgClass(DataTreeChangedReply.class);
137 Mockito.verify(mockListener).onDataTreeChanged(mockCandidates1);
138 Mockito.verify(mockListener).onDataTreeChanged(mockCandidates2);
139 Mockito.verify(mockListener).onDataTreeChanged(mockCandidates3);