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.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;
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;
31 public class DataTreeChangeListenerActorTest extends AbstractActorTest {
32 private TestKit testKit;
35 public void before() {
36 testKit = new TestKit(getSystem());
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");
47 // Let the DataChangeListener know that notifications should be
49 subject.tell(new EnableNotification(true, "test"), testKit.getRef());
51 subject.tell(new DataTreeChanged(mockCandidates), testKit.getRef());
53 testKit.expectMsgClass(DataTreeChangedReply.class);
55 verify(mockListener).onDataTreeChanged(mockCandidates);
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");
66 subject.tell(new DataTreeChanged(mockCandidates), testKit.getRef());
68 testKit.within(testKit.duration("1 seconds"), () -> {
69 testKit.expectNoMessage();
70 verify(mockListener, never()).onDataTreeChanged(anyCollection());
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");
83 getSystem().eventStream().subscribe(testKit.getRef(), DeadLetter.class);
85 subject.tell(new DataTreeChanged(mockCandidates), ActorRef.noSender());
87 // Make sure no DataChangedReply is sent to DeadLetters.
89 DeadLetter deadLetter;
91 deadLetter = testKit.expectMsgClass(testKit.duration("1 seconds"), DeadLetter.class);
92 } catch (AssertionError e) {
93 // Timed out - got no DeadLetter - this is good
97 // We may get DeadLetters for other messages we don't care
99 assertFalse("Unexpected DataTreeChangedReply", deadLetter.message() instanceof DataTreeChangedReply);
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);
112 final DOMDataTreeChangeListener mockListener = mock(DOMDataTreeChangeListener.class);
113 doThrow(new RuntimeException("mock")).when(mockListener).onDataTreeChanged(mockCandidates2);
115 Props props = DataTreeChangeListenerActor.props(mockListener, TEST_PATH);
116 ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedWithListenerRuntimeEx");
118 // Let the DataChangeListener know that notifications should be
120 subject.tell(new EnableNotification(true, "test"), testKit.getRef());
122 subject.tell(new DataTreeChanged(mockCandidates1), testKit.getRef());
123 testKit.expectMsgClass(DataTreeChangedReply.class);
125 subject.tell(new DataTreeChanged(mockCandidates2), testKit.getRef());
126 testKit.expectMsgClass(DataTreeChangedReply.class);
128 subject.tell(new DataTreeChanged(mockCandidates3), testKit.getRef());
129 testKit.expectMsgClass(DataTreeChangedReply.class);
131 verify(mockListener).onDataTreeChanged(mockCandidates1);
132 verify(mockListener).onDataTreeChanged(mockCandidates2);
133 verify(mockListener).onDataTreeChanged(mockCandidates3);