Implement DataChangeListener
[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.Props;
5 import akka.testkit.JavaTestKit;
6 import org.junit.Test;
7 import org.opendaylight.controller.cluster.datastore.messages.DataChanged;
8 import org.opendaylight.controller.cluster.datastore.messages.DataChangedReply;
9 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
10 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
11 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
12 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
13
14 import java.util.Map;
15 import java.util.Set;
16
17 import static org.junit.Assert.assertTrue;
18
19 public class DataChangeListenerTest extends AbstractActorTest {
20
21     private static class MockDataChangedEvent implements AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> {
22
23         @Override
24         public Map<InstanceIdentifier, NormalizedNode<?, ?>> getCreatedData() {
25             throw new UnsupportedOperationException("getCreatedData");
26         }
27
28         @Override
29         public Map<InstanceIdentifier, NormalizedNode<?, ?>> getUpdatedData() {
30             throw new UnsupportedOperationException("getUpdatedData");
31         }
32
33         @Override public Set<InstanceIdentifier> getRemovedPaths() {
34             throw new UnsupportedOperationException("getRemovedPaths");
35         }
36
37         @Override
38         public Map<InstanceIdentifier, ? extends NormalizedNode<?, ?>> getOriginalData() {
39             throw new UnsupportedOperationException("getOriginalData");
40         }
41
42         @Override public NormalizedNode<?, ?> getOriginalSubtree() {
43             throw new UnsupportedOperationException("getOriginalSubtree");
44         }
45
46         @Override public NormalizedNode<?, ?> getUpdatedSubtree() {
47             throw new UnsupportedOperationException("getUpdatedSubtree");
48         }
49     }
50
51     private class MockDataChangeListener implements AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>> {
52         private boolean gotIt = false;
53
54         @Override public void onDataChanged(
55             AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> change) {
56             gotIt = true;
57         }
58
59         public boolean gotIt() {
60             return gotIt;
61         }
62     }
63
64     @Test
65     public void testDataChanged(){
66         new JavaTestKit(getSystem()) {{
67             final MockDataChangeListener listener = new MockDataChangeListener();
68             final Props props = DataChangeListener.props(listener);
69             final ActorRef subject =
70                 getSystem().actorOf(props, "testDataChanged");
71
72             new Within(duration("1 seconds")) {
73                 protected void run() {
74
75                     subject.tell(
76                         new DataChanged(new MockDataChangedEvent()),
77                         getRef());
78
79                     final Boolean out = new ExpectMsg<Boolean>("dataChanged") {
80                         // do not put code outside this method, will run afterwards
81                         protected Boolean match(Object in) {
82                             if (in instanceof DataChangedReply) {
83                                 DataChangedReply reply =
84                                     (DataChangedReply) in;
85                                 return true;
86                             } else {
87                                 throw noMatch();
88                             }
89                         }
90                     }.get(); // this extracts the received message
91
92                     assertTrue(out);
93                     assertTrue(listener.gotIt());
94                     // Will wait for the rest of the 3 seconds
95                     expectNoMsg();
96                 }
97
98
99             };
100         }};
101     }
102 }

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