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.junit.Assert.assertFalse;
11 import static org.mockito.ArgumentMatchers.anyList;
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;
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.tree.api.DataTreeCandidate;
32 public class DataTreeChangeListenerActorTest extends AbstractActorTest {
33 private TestKit testKit;
36 public void before() {
37 testKit = new TestKit(getSystem());
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");
48 // Let the DataChangeListener know that notifications should be
50 subject.tell(new EnableNotification(true, "test"), testKit.getRef());
52 subject.tell(new DataTreeChanged(mockCandidates), testKit.getRef());
54 testKit.expectMsgClass(DataTreeChangedReply.class);
56 verify(mockListener).onDataTreeChanged(mockCandidates);
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");
67 subject.tell(new DataTreeChanged(mockCandidates), testKit.getRef());
69 testKit.within(Duration.ofSeconds(1), () -> {
70 testKit.expectNoMessage();
71 verify(mockListener, never()).onDataTreeChanged(anyList());
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");
84 getSystem().eventStream().subscribe(testKit.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 = testKit.expectMsgClass(Duration.ofSeconds(1), 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 assertFalse("Unexpected DataTreeChangedReply", deadLetter.message() instanceof DataTreeChangedReply);
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);
113 final DOMDataTreeChangeListener mockListener = mock(DOMDataTreeChangeListener.class);
114 doThrow(new RuntimeException("mock")).when(mockListener).onDataTreeChanged(mockCandidates2);
116 Props props = DataTreeChangeListenerActor.props(mockListener, TEST_PATH);
117 ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedWithListenerRuntimeEx");
119 // Let the DataChangeListener know that notifications should be
121 subject.tell(new EnableNotification(true, "test"), testKit.getRef());
123 subject.tell(new DataTreeChanged(mockCandidates1), testKit.getRef());
124 testKit.expectMsgClass(DataTreeChangedReply.class);
126 subject.tell(new DataTreeChanged(mockCandidates2), testKit.getRef());
127 testKit.expectMsgClass(DataTreeChangedReply.class);
129 subject.tell(new DataTreeChanged(mockCandidates3), testKit.getRef());
130 testKit.expectMsgClass(DataTreeChangedReply.class);
132 verify(mockListener).onDataTreeChanged(mockCandidates1);
133 verify(mockListener).onDataTreeChanged(mockCandidates2);
134 verify(mockListener).onDataTreeChanged(mockCandidates3);