Implement DistributedDataStore#registerDataChangeListener
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardTest.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.CreateTransactionChain;
8 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChainReply;
9 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
10 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
11 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
12 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18
19 import static org.junit.Assert.assertTrue;
20
21 public class ShardTest extends AbstractActorTest{
22   @Test
23   public void testOnReceiveCreateTransactionChain() throws Exception {
24     new JavaTestKit(getSystem()) {{
25       final Props props = Shard.props("config");
26       final ActorRef subject = getSystem().actorOf(props, "testCreateTransactionChain");
27
28       new Within(duration("1 seconds")) {
29         protected void run() {
30
31           subject.tell(new CreateTransactionChain(), getRef());
32
33           final String out = new ExpectMsg<String>("match hint") {
34             // do not put code outside this method, will run afterwards
35             protected String match(Object in) {
36               if (in instanceof CreateTransactionChainReply) {
37                 CreateTransactionChainReply reply = (CreateTransactionChainReply) in;
38                 return reply.getTransactionChainPath().toString();
39               } else {
40                 throw noMatch();
41               }
42             }
43           }.get(); // this extracts the received message
44
45           assertTrue(out.matches("akka:\\/\\/test\\/user\\/testCreateTransactionChain\\/\\$.*"));
46           // Will wait for the rest of the 3 seconds
47           expectNoMsg();
48         }
49
50
51       };
52     }};
53   }
54
55   @Test
56   public void testOnReceiveRegisterListener() throws Exception {
57     new JavaTestKit(getSystem()) {{
58       final Props props = Shard.props("config");
59       final ActorRef subject = getSystem().actorOf(props, "testRegisterChangeListener");
60
61       new Within(duration("1 seconds")) {
62         protected void run() {
63
64           subject.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
65
66           subject.tell(new RegisterChangeListener(TestModel.TEST_PATH, getRef().path() , AsyncDataBroker.DataChangeScope.BASE), getRef());
67
68           final String out = new ExpectMsg<String>("match hint") {
69             // do not put code outside this method, will run afterwards
70             protected String match(Object in) {
71               if (in instanceof RegisterChangeListenerReply) {
72                 RegisterChangeListenerReply reply = (RegisterChangeListenerReply) in;
73                 return reply.getListenerRegistrationPath().toString();
74               } else {
75                 throw noMatch();
76               }
77             }
78           }.get(); // this extracts the received message
79
80           assertTrue(out.matches("akka:\\/\\/test\\/user\\/testRegisterChangeListener\\/\\$.*"));
81           // Will wait for the rest of the 3 seconds
82           expectNoMsg();
83         }
84
85
86       };
87     }};
88   }
89
90
91
92   private  AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>> noOpDataChangeListener(){
93     return new AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>() {
94       @Override
95       public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> change) {
96
97       }
98     };
99   }
100 }

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