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