Make REUSABLE_*_TL final
[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     @Test
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");
35
36             // Let the DataChangeListener know that notifications should be enabled
37             subject.tell(new EnableNotification(true), getRef());
38
39             subject.tell(new DataTreeChanged(mockCandidates),
40                     getRef());
41
42             expectMsgClass(DataTreeChangedReply.class);
43
44             Mockito.verify(mockListener).onDataTreeChanged(mockCandidates);
45         }};
46     }
47
48     @Test
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");
57
58             subject.tell(new DataTreeChanged(mockCandidates),
59                     getRef());
60
61             new Within(duration("1 seconds")) {
62                 @Override
63                 protected void run() {
64                     expectNoMsg();
65
66                     Mockito.verify(mockListener, Mockito.never()).onDataTreeChanged(
67                             Matchers.anyCollectionOf(DataTreeCandidate.class));
68                 }
69             };
70         }};
71     }
72
73     @Test
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");
81
82             getSystem().eventStream().subscribe(getRef(), DeadLetter.class);
83
84             subject.tell(new DataTreeChanged(mockCandidates), ActorRef.noSender());
85
86             // Make sure no DataChangedReply is sent to DeadLetters.
87             while(true) {
88                 DeadLetter deadLetter;
89                 try {
90                     deadLetter = expectMsgClass(duration("1 seconds"), DeadLetter.class);
91                 } catch (AssertionError e) {
92                     // Timed out - got no DeadLetter - this is good
93                     break;
94                 }
95
96                 // We may get DeadLetters for other messages we don't care about.
97                 Assert.assertFalse("Unexpected DataTreeChangedReply",
98                         deadLetter.message() instanceof DataTreeChangedReply);
99             }
100         }};
101     }
102
103     @Test
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);
112
113             final DOMDataTreeChangeListener mockListener = Mockito.mock(DOMDataTreeChangeListener.class);
114             Mockito.doThrow(new RuntimeException("mock")).when(mockListener).onDataTreeChanged(mockCandidates2);
115
116             Props props = DataTreeChangeListenerActor.props(mockListener);
117             ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedWithListenerRuntimeEx");
118
119             // Let the DataChangeListener know that notifications should be enabled
120             subject.tell(new EnableNotification(true), getRef());
121
122             subject.tell(new DataTreeChanged(mockCandidates1),getRef());
123             expectMsgClass(DataTreeChangedReply.class);
124
125             subject.tell(new DataTreeChanged(mockCandidates2),getRef());
126             expectMsgClass(DataTreeChangedReply.class);
127
128             subject.tell(new DataTreeChanged(mockCandidates3),getRef());
129             expectMsgClass(DataTreeChangedReply.class);
130
131             Mockito.verify(mockListener).onDataTreeChanged(mockCandidates1);
132             Mockito.verify(mockListener).onDataTreeChanged(mockCandidates2);
133             Mockito.verify(mockListener).onDataTreeChanged(mockCandidates3);
134         }};
135     }
136 }