ed447e004fd9b91a7ad7da1561fdb83bf3cbb2d7
[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.CreateTransaction;
8 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChain;
9 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChainReply;
10 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
11 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
12 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
13 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
14 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
18 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23
24 public class ShardTest extends AbstractActorTest {
25     @Test
26     public void testOnReceiveCreateTransactionChain() throws Exception {
27         new JavaTestKit(getSystem()) {{
28             final Props props = Shard.props("config");
29             final ActorRef subject =
30                 getSystem().actorOf(props, "testCreateTransactionChain");
31
32             new Within(duration("1 seconds")) {
33                 protected void run() {
34
35                     subject.tell(new CreateTransactionChain(), getRef());
36
37                     final String out = new ExpectMsg<String>("match hint") {
38                         // do not put code outside this method, will run afterwards
39                         protected String match(Object in) {
40                             if (in instanceof CreateTransactionChainReply) {
41                                 CreateTransactionChainReply reply =
42                                     (CreateTransactionChainReply) in;
43                                 return reply.getTransactionChainPath()
44                                     .toString();
45                             } else {
46                                 throw noMatch();
47                             }
48                         }
49                     }.get(); // this extracts the received message
50
51                     assertEquals("Unexpected transaction path " + out,
52                         "akka://test/user/testCreateTransactionChain/$a",
53                         out);
54
55                     expectNoMsg();
56                 }
57
58
59             };
60         }};
61     }
62
63     @Test
64     public void testOnReceiveRegisterListener() throws Exception {
65         new JavaTestKit(getSystem()) {{
66             final Props props = Shard.props("config");
67             final ActorRef subject =
68                 getSystem().actorOf(props, "testRegisterChangeListener");
69
70             new Within(duration("1 seconds")) {
71                 protected void run() {
72
73                     subject.tell(
74                         new UpdateSchemaContext(TestModel.createTestContext()),
75                         getRef());
76
77                     subject.tell(new RegisterChangeListener(TestModel.TEST_PATH,
78                         getRef().path(), AsyncDataBroker.DataChangeScope.BASE),
79                         getRef());
80
81                     final String out = new ExpectMsg<String>("match hint") {
82                         // do not put code outside this method, will run afterwards
83                         protected String match(Object in) {
84                             if (in instanceof RegisterChangeListenerReply) {
85                                 RegisterChangeListenerReply reply =
86                                     (RegisterChangeListenerReply) in;
87                                 return reply.getListenerRegistrationPath()
88                                     .toString();
89                             } else {
90                                 throw noMatch();
91                             }
92                         }
93                     }.get(); // this extracts the received message
94
95                     assertTrue(out.matches(
96                         "akka:\\/\\/test\\/user\\/testRegisterChangeListener\\/\\$.*"));
97                     // Will wait for the rest of the 3 seconds
98                     expectNoMsg();
99                 }
100
101
102             };
103         }};
104     }
105
106     @Test
107     public void testCreateTransaction(){
108         new JavaTestKit(getSystem()) {{
109             final Props props = Shard.props("config");
110             final ActorRef subject =
111                 getSystem().actorOf(props, "testCreateTransaction");
112
113             new Within(duration("1 seconds")) {
114                 protected void run() {
115
116                     subject.tell(
117                         new UpdateSchemaContext(TestModel.createTestContext()),
118                         getRef());
119
120                     subject.tell(new CreateTransaction("txn-1"),
121                         getRef());
122
123                     final String out = new ExpectMsg<String>("match hint") {
124                         // do not put code outside this method, will run afterwards
125                         protected String match(Object in) {
126                             if (in instanceof CreateTransactionReply) {
127                                 CreateTransactionReply reply =
128                                     (CreateTransactionReply) in;
129                                 return reply.getTransactionPath()
130                                     .toString();
131                             } else {
132                                 throw noMatch();
133                             }
134                         }
135                     }.get(); // this extracts the received message
136
137                     assertEquals("Unexpected transaction path " + out,
138                         "akka://test/user/testCreateTransaction/shard-txn-1",
139                         out);
140                     expectNoMsg();
141                 }
142
143
144             };
145         }};
146     }
147
148
149
150     private AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>> noOpDataChangeListener() {
151         return new AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>() {
152             @Override
153             public void onDataChanged(
154                 AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> change) {
155
156             }
157         };
158     }
159 }