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

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