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