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