Merge "Remove sonar.profile since it's deprecated in Sonar 4.5"
[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
17 public class DataChangeListenerTest extends AbstractActorTest {
18
19     @SuppressWarnings({ "rawtypes", "unchecked" })
20     @Test
21     public void testDataChangedWhenNotificationsAreEnabled(){
22         new JavaTestKit(getSystem()) {{
23             final AsyncDataChangeEvent mockChangeEvent = Mockito.mock(AsyncDataChangeEvent.class);
24             final AsyncDataChangeListener mockListener = Mockito.mock(AsyncDataChangeListener.class);
25             final Props props = DataChangeListener.props(mockListener);
26             final ActorRef subject = getSystem().actorOf(props, "testDataChangedNotificationsEnabled");
27
28             // Let the DataChangeListener know that notifications should be enabled
29             subject.tell(new EnableNotification(true), getRef());
30
31             subject.tell(new DataChanged(CompositeModel.createTestContext(), mockChangeEvent),
32                     getRef());
33
34             expectMsgClass(DataChangedReply.class);
35
36             Mockito.verify(mockListener).onDataChanged(mockChangeEvent);
37         }};
38     }
39
40     @SuppressWarnings({ "rawtypes", "unchecked" })
41     @Test
42     public void testDataChangedWhenNotificationsAreDisabled(){
43         new JavaTestKit(getSystem()) {{
44             final AsyncDataChangeEvent mockChangeEvent = Mockito.mock(AsyncDataChangeEvent.class);
45             final AsyncDataChangeListener mockListener = Mockito.mock(AsyncDataChangeListener.class);
46             final Props props = DataChangeListener.props(mockListener);
47             final ActorRef subject =
48                 getSystem().actorOf(props, "testDataChangedNotificationsDisabled");
49
50             subject.tell(new DataChanged(CompositeModel.createTestContext(), mockChangeEvent),
51                     getRef());
52
53             new Within(duration("1 seconds")) {
54                 @Override
55                 protected void run() {
56                     expectNoMsg();
57
58                     Mockito.verify(mockListener, Mockito.never()).onDataChanged(
59                             Mockito.any(AsyncDataChangeEvent.class));
60                 }
61             };
62         }};
63     }
64
65     @SuppressWarnings({ "rawtypes", "unchecked" })
66     @Test
67     public void testDataChangedWithNoSender(){
68         new JavaTestKit(getSystem()) {{
69             final AsyncDataChangeEvent mockChangeEvent = Mockito.mock(AsyncDataChangeEvent.class);
70             final AsyncDataChangeListener mockListener = Mockito.mock(AsyncDataChangeListener.class);
71             final Props props = DataChangeListener.props(mockListener);
72             final ActorRef subject = getSystem().actorOf(props, "testDataChangedWithNoSender");
73
74             getSystem().eventStream().subscribe(getRef(), DeadLetter.class);
75
76             subject.tell(new DataChanged(CompositeModel.createTestContext(), mockChangeEvent),
77                     ActorRef.noSender());
78
79             // Make sure no DataChangedReply is sent to DeadLetters.
80             while(true) {
81                 DeadLetter deadLetter;
82                 try {
83                     deadLetter = expectMsgClass(duration("1 seconds"), DeadLetter.class);
84                 } catch (AssertionError e) {
85                     // Timed out - got no DeadLetter - this is good
86                     break;
87                 }
88
89                 // We may get DeadLetters for other messages we don't care about.
90                 Assert.assertFalse("Unexpected DataChangedReply",
91                         deadLetter.message() instanceof DataChangedReply);
92             }
93         }};
94     }
95 }

©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.