Add OnDemandShardState to report additional Shard state
[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 static org.opendaylight.controller.md.cluster.datastore.model.TestModel.TEST_PATH;
11
12 import akka.actor.ActorRef;
13 import akka.actor.DeadLetter;
14 import akka.actor.Props;
15 import akka.testkit.JavaTestKit;
16 import com.google.common.collect.ImmutableList;
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.mockito.Matchers;
20 import org.mockito.Mockito;
21 import org.opendaylight.controller.cluster.datastore.messages.DataTreeChanged;
22 import org.opendaylight.controller.cluster.datastore.messages.DataTreeChangedReply;
23 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
26
27 public class DataTreeChangeListenerActorTest extends AbstractActorTest {
28
29     @Test
30     public void testDataChangedWhenNotificationsAreEnabled(){
31         new JavaTestKit(getSystem()) {{
32             final DataTreeCandidate mockTreeCandidate = Mockito.mock(DataTreeCandidate.class);
33             final ImmutableList<DataTreeCandidate> mockCandidates = ImmutableList.of(mockTreeCandidate);
34             final DOMDataTreeChangeListener mockListener = Mockito.mock(DOMDataTreeChangeListener.class);
35             final Props props = DataTreeChangeListenerActor.props(mockListener, TEST_PATH);
36             final ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedNotificationsEnabled");
37
38             // Let the DataChangeListener know that notifications should be enabled
39             subject.tell(new EnableNotification(true), getRef());
40
41             subject.tell(new DataTreeChanged(mockCandidates),
42                     getRef());
43
44             expectMsgClass(DataTreeChangedReply.class);
45
46             Mockito.verify(mockListener).onDataTreeChanged(mockCandidates);
47         }};
48     }
49
50     @Test
51     public void testDataChangedWhenNotificationsAreDisabled(){
52         new JavaTestKit(getSystem()) {{
53             final DataTreeCandidate mockTreeCandidate = Mockito.mock(DataTreeCandidate.class);
54             final ImmutableList<DataTreeCandidate> mockCandidates = ImmutableList.of(mockTreeCandidate);
55             final DOMDataTreeChangeListener mockListener = Mockito.mock(DOMDataTreeChangeListener.class);
56             final Props props = DataTreeChangeListenerActor.props(mockListener, TEST_PATH);
57             final ActorRef subject =
58                     getSystem().actorOf(props, "testDataTreeChangedNotificationsDisabled");
59
60             subject.tell(new DataTreeChanged(mockCandidates),
61                     getRef());
62
63             new Within(duration("1 seconds")) {
64                 @Override
65                 protected void run() {
66                     expectNoMsg();
67
68                     Mockito.verify(mockListener, Mockito.never()).onDataTreeChanged(
69                             Matchers.anyCollectionOf(DataTreeCandidate.class));
70                 }
71             };
72         }};
73     }
74
75     @Test
76     public void testDataChangedWithNoSender(){
77         new JavaTestKit(getSystem()) {{
78             final DataTreeCandidate mockTreeCandidate = Mockito.mock(DataTreeCandidate.class);
79             final ImmutableList<DataTreeCandidate> mockCandidates = ImmutableList.of(mockTreeCandidate);
80             final DOMDataTreeChangeListener mockListener = Mockito.mock(DOMDataTreeChangeListener.class);
81             final Props props = DataTreeChangeListenerActor.props(mockListener, TEST_PATH);
82             final ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedWithNoSender");
83
84             getSystem().eventStream().subscribe(getRef(), DeadLetter.class);
85
86             subject.tell(new DataTreeChanged(mockCandidates), 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 about.
99                 Assert.assertFalse("Unexpected DataTreeChangedReply",
100                         deadLetter.message() instanceof DataTreeChangedReply);
101             }
102         }};
103     }
104
105     @Test
106     public void testDataChangedWithListenerRuntimeEx(){
107         new JavaTestKit(getSystem()) {{
108             final DataTreeCandidate mockTreeCandidate1 = Mockito.mock(DataTreeCandidate.class);
109             final ImmutableList<DataTreeCandidate> mockCandidates1 = ImmutableList.of(mockTreeCandidate1);
110             final DataTreeCandidate mockTreeCandidate2 = Mockito.mock(DataTreeCandidate.class);
111             final ImmutableList<DataTreeCandidate> mockCandidates2 = ImmutableList.of(mockTreeCandidate2);
112             final DataTreeCandidate mockTreeCandidate3 = Mockito.mock(DataTreeCandidate.class);
113             final ImmutableList<DataTreeCandidate> mockCandidates3 = ImmutableList.of(mockTreeCandidate3);
114
115             final DOMDataTreeChangeListener mockListener = Mockito.mock(DOMDataTreeChangeListener.class);
116             Mockito.doThrow(new RuntimeException("mock")).when(mockListener).onDataTreeChanged(mockCandidates2);
117
118             Props props = DataTreeChangeListenerActor.props(mockListener, TEST_PATH);
119             ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedWithListenerRuntimeEx");
120
121             // Let the DataChangeListener know that notifications should be enabled
122             subject.tell(new EnableNotification(true), getRef());
123
124             subject.tell(new DataTreeChanged(mockCandidates1),getRef());
125             expectMsgClass(DataTreeChangedReply.class);
126
127             subject.tell(new DataTreeChanged(mockCandidates2),getRef());
128             expectMsgClass(DataTreeChangedReply.class);
129
130             subject.tell(new DataTreeChanged(mockCandidates3),getRef());
131             expectMsgClass(DataTreeChangedReply.class);
132
133             Mockito.verify(mockListener).onDataTreeChanged(mockCandidates1);
134             Mockito.verify(mockListener).onDataTreeChanged(mockCandidates2);
135             Mockito.verify(mockListener).onDataTreeChanged(mockCandidates3);
136         }};
137     }
138 }