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

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