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