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             {
33                 final DataTreeCandidate mockTreeCandidate = Mockito.mock(DataTreeCandidate.class);
34                 final ImmutableList<DataTreeCandidate> mockCandidates = ImmutableList.of(mockTreeCandidate);
35                 final DOMDataTreeChangeListener mockListener = Mockito.mock(DOMDataTreeChangeListener.class);
36                 final Props props = DataTreeChangeListenerActor.props(mockListener, TEST_PATH);
37                 final ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedNotificationsEnabled");
38
39                 // Let the DataChangeListener know that notifications should be
40                 // enabled
41                 subject.tell(new EnableNotification(true), getRef());
42
43                 subject.tell(new DataTreeChanged(mockCandidates), getRef());
44
45                 expectMsgClass(DataTreeChangedReply.class);
46
47                 Mockito.verify(mockListener).onDataTreeChanged(mockCandidates);
48             }
49         };
50     }
51
52     @Test
53     public void testDataChangedWhenNotificationsAreDisabled() {
54         new JavaTestKit(getSystem()) {
55             {
56                 final DataTreeCandidate mockTreeCandidate = Mockito.mock(DataTreeCandidate.class);
57                 final ImmutableList<DataTreeCandidate> mockCandidates = ImmutableList.of(mockTreeCandidate);
58                 final DOMDataTreeChangeListener mockListener = Mockito.mock(DOMDataTreeChangeListener.class);
59                 final Props props = DataTreeChangeListenerActor.props(mockListener, TEST_PATH);
60                 final ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedNotificationsDisabled");
61
62                 subject.tell(new DataTreeChanged(mockCandidates), getRef());
63
64                 new Within(duration("1 seconds")) {
65                     @Override
66                     protected void run() {
67                         expectNoMsg();
68
69                         Mockito.verify(mockListener, Mockito.never())
70                                 .onDataTreeChanged(Matchers.anyCollectionOf(DataTreeCandidate.class));
71                     }
72                 };
73             }
74         };
75     }
76
77     @Test
78     public void testDataChangedWithNoSender() {
79         new JavaTestKit(getSystem()) {
80             {
81                 final DataTreeCandidate mockTreeCandidate = Mockito.mock(DataTreeCandidate.class);
82                 final ImmutableList<DataTreeCandidate> mockCandidates = ImmutableList.of(mockTreeCandidate);
83                 final DOMDataTreeChangeListener mockListener = Mockito.mock(DOMDataTreeChangeListener.class);
84                 final Props props = DataTreeChangeListenerActor.props(mockListener, TEST_PATH);
85                 final ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedWithNoSender");
86
87                 getSystem().eventStream().subscribe(getRef(), DeadLetter.class);
88
89                 subject.tell(new DataTreeChanged(mockCandidates), ActorRef.noSender());
90
91                 // Make sure no DataChangedReply is sent to DeadLetters.
92                 while (true) {
93                     DeadLetter deadLetter;
94                     try {
95                         deadLetter = expectMsgClass(duration("1 seconds"), DeadLetter.class);
96                     } catch (AssertionError e) {
97                         // Timed out - got no DeadLetter - this is good
98                         break;
99                     }
100
101                     // We may get DeadLetters for other messages we don't care
102                     // about.
103                     Assert.assertFalse("Unexpected DataTreeChangedReply",
104                             deadLetter.message() instanceof DataTreeChangedReply);
105                 }
106             }
107         };
108     }
109
110     @Test
111     public void testDataChangedWithListenerRuntimeEx() {
112         new JavaTestKit(getSystem()) {
113             {
114                 final DataTreeCandidate mockTreeCandidate1 = Mockito.mock(DataTreeCandidate.class);
115                 final ImmutableList<DataTreeCandidate> mockCandidates1 = ImmutableList.of(mockTreeCandidate1);
116                 final DataTreeCandidate mockTreeCandidate2 = Mockito.mock(DataTreeCandidate.class);
117                 final ImmutableList<DataTreeCandidate> mockCandidates2 = ImmutableList.of(mockTreeCandidate2);
118                 final DataTreeCandidate mockTreeCandidate3 = Mockito.mock(DataTreeCandidate.class);
119                 final ImmutableList<DataTreeCandidate> mockCandidates3 = ImmutableList.of(mockTreeCandidate3);
120
121                 final DOMDataTreeChangeListener mockListener = Mockito.mock(DOMDataTreeChangeListener.class);
122                 Mockito.doThrow(new RuntimeException("mock")).when(mockListener).onDataTreeChanged(mockCandidates2);
123
124                 Props props = DataTreeChangeListenerActor.props(mockListener, TEST_PATH);
125                 ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedWithListenerRuntimeEx");
126
127                 // Let the DataChangeListener know that notifications should be
128                 // enabled
129                 subject.tell(new EnableNotification(true), getRef());
130
131                 subject.tell(new DataTreeChanged(mockCandidates1), getRef());
132                 expectMsgClass(DataTreeChangedReply.class);
133
134                 subject.tell(new DataTreeChanged(mockCandidates2), getRef());
135                 expectMsgClass(DataTreeChangedReply.class);
136
137                 subject.tell(new DataTreeChanged(mockCandidates3), getRef());
138                 expectMsgClass(DataTreeChangedReply.class);
139
140                 Mockito.verify(mockListener).onDataTreeChanged(mockCandidates1);
141                 Mockito.verify(mockListener).onDataTreeChanged(mockCandidates2);
142                 Mockito.verify(mockListener).onDataTreeChanged(mockCandidates3);
143             }
144         };
145     }
146 }

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