Merge "Bug 2517: Catch RuntimeExceptions thrown from the DCL in DataChangeListener"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DataChangeListenerTest.java
1 package org.opendaylight.controller.cluster.datastore;
2
3 import akka.actor.ActorRef;
4 import akka.actor.DeadLetter;
5 import akka.actor.Props;
6 import akka.testkit.JavaTestKit;
7 import org.junit.Assert;
8 import org.junit.Test;
9 import org.mockito.Mockito;
10 import org.opendaylight.controller.cluster.datastore.messages.DataChanged;
11 import org.opendaylight.controller.cluster.datastore.messages.DataChangedReply;
12 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
13 import org.opendaylight.controller.md.cluster.datastore.model.CompositeModel;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
16 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
17
18 public class DataChangeListenerTest extends AbstractActorTest {
19
20     @SuppressWarnings({ "rawtypes", "unchecked" })
21     @Test
22     public void testDataChangedWhenNotificationsAreEnabled(){
23         new JavaTestKit(getSystem()) {{
24             final AsyncDataChangeEvent mockChangeEvent = Mockito.mock(AsyncDataChangeEvent.class);
25             final AsyncDataChangeListener mockListener = Mockito.mock(AsyncDataChangeListener.class);
26             final Props props = DataChangeListener.props(mockListener);
27             final ActorRef subject = getSystem().actorOf(props, "testDataChangedNotificationsEnabled");
28
29             // Let the DataChangeListener know that notifications should be enabled
30             subject.tell(new EnableNotification(true), getRef());
31
32             subject.tell(new DataChanged(CompositeModel.createTestContext(), mockChangeEvent),
33                     getRef());
34
35             expectMsgClass(DataChangedReply.class);
36
37             Mockito.verify(mockListener).onDataChanged(mockChangeEvent);
38         }};
39     }
40
41     @SuppressWarnings({ "rawtypes", "unchecked" })
42     @Test
43     public void testDataChangedWhenNotificationsAreDisabled(){
44         new JavaTestKit(getSystem()) {{
45             final AsyncDataChangeEvent mockChangeEvent = Mockito.mock(AsyncDataChangeEvent.class);
46             final AsyncDataChangeListener mockListener = Mockito.mock(AsyncDataChangeListener.class);
47             final Props props = DataChangeListener.props(mockListener);
48             final ActorRef subject =
49                 getSystem().actorOf(props, "testDataChangedNotificationsDisabled");
50
51             subject.tell(new DataChanged(CompositeModel.createTestContext(), mockChangeEvent),
52                     getRef());
53
54             new Within(duration("1 seconds")) {
55                 @Override
56                 protected void run() {
57                     expectNoMsg();
58
59                     Mockito.verify(mockListener, Mockito.never()).onDataChanged(
60                             Mockito.any(AsyncDataChangeEvent.class));
61                 }
62             };
63         }};
64     }
65
66     @SuppressWarnings({ "rawtypes", "unchecked" })
67     @Test
68     public void testDataChangedWithNoSender(){
69         new JavaTestKit(getSystem()) {{
70             final AsyncDataChangeEvent mockChangeEvent = Mockito.mock(AsyncDataChangeEvent.class);
71             final AsyncDataChangeListener mockListener = Mockito.mock(AsyncDataChangeListener.class);
72             final Props props = DataChangeListener.props(mockListener);
73             final ActorRef subject = getSystem().actorOf(props, "testDataChangedWithNoSender");
74
75             getSystem().eventStream().subscribe(getRef(), DeadLetter.class);
76
77             subject.tell(new DataChanged(CompositeModel.createTestContext(), mockChangeEvent),
78                     ActorRef.noSender());
79
80             // Make sure no DataChangedReply is sent to DeadLetters.
81             while(true) {
82                 DeadLetter deadLetter;
83                 try {
84                     deadLetter = expectMsgClass(duration("1 seconds"), DeadLetter.class);
85                 } catch (AssertionError e) {
86                     // Timed out - got no DeadLetter - this is good
87                     break;
88                 }
89
90                 // We may get DeadLetters for other messages we don't care about.
91                 Assert.assertFalse("Unexpected DataChangedReply",
92                         deadLetter.message() instanceof DataChangedReply);
93             }
94         }};
95     }
96
97     @SuppressWarnings({ "rawtypes", "unchecked" })
98     @Test
99     public void testDataChangedWithListenerRuntimeEx(){
100         new JavaTestKit(getSystem()) {{
101             AsyncDataChangeEvent mockChangeEvent1 = Mockito.mock(AsyncDataChangeEvent.class);
102             AsyncDataChangeEvent mockChangeEvent2 = Mockito.mock(AsyncDataChangeEvent.class);
103             AsyncDataChangeEvent mockChangeEvent3 = Mockito.mock(AsyncDataChangeEvent.class);
104
105             AsyncDataChangeListener mockListener = Mockito.mock(AsyncDataChangeListener.class);
106             Mockito.doThrow(new RuntimeException("mock")).when(mockListener).onDataChanged(mockChangeEvent2);
107
108             Props props = DataChangeListener.props(mockListener);
109             ActorRef subject = getSystem().actorOf(props, "testDataChangedWithListenerRuntimeEx");
110
111             // Let the DataChangeListener know that notifications should be enabled
112             subject.tell(new EnableNotification(true), getRef());
113
114             SchemaContext schemaContext = CompositeModel.createTestContext();
115
116             subject.tell(new DataChanged(schemaContext, mockChangeEvent1),getRef());
117             expectMsgClass(DataChangedReply.class);
118
119             subject.tell(new DataChanged(schemaContext, mockChangeEvent2),getRef());
120             expectMsgClass(DataChangedReply.class);
121
122             subject.tell(new DataChanged(schemaContext, mockChangeEvent3),getRef());
123             expectMsgClass(DataChangedReply.class);
124
125             Mockito.verify(mockListener).onDataChanged(mockChangeEvent1);
126             Mockito.verify(mockListener).onDataChanged(mockChangeEvent2);
127             Mockito.verify(mockListener).onDataChanged(mockChangeEvent3);
128         }};
129     }
130 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.