32d90d0ef76deb6afc4af31fa7d695874865e3ab
[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.EnableNotification;
11 import org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved;
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.SchemaContextHelper;
16 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
20 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import static junit.framework.Assert.assertFalse;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertTrue;
31
32 public class ShardTest extends AbstractActorTest {
33     @Test
34     public void testOnReceiveCreateTransactionChain() throws Exception {
35         new JavaTestKit(getSystem()) {{
36             final Props props = Shard.props("config", Collections.EMPTY_MAP);
37             final ActorRef subject =
38                 getSystem().actorOf(props, "testCreateTransactionChain");
39
40
41             // Wait for Shard to become a Leader
42             try {
43                 Thread.sleep(1000);
44             } catch (InterruptedException e) {
45                 e.printStackTrace();
46             }
47
48             new Within(duration("1 seconds")) {
49                 protected void run() {
50
51                     subject.tell(new CreateTransactionChain().toSerializable(), getRef());
52
53                     final String out = new ExpectMsg<String>("match hint") {
54                         // do not put code outside this method, will run afterwards
55                         protected String match(Object in) {
56                             if (in.getClass().equals(CreateTransactionChainReply.SERIALIZABLE_CLASS)){
57                                 CreateTransactionChainReply reply =
58                                     CreateTransactionChainReply.fromSerializable(getSystem(),in);
59                                 return reply.getTransactionChainPath()
60                                     .toString();
61                             } else {
62                                 throw noMatch();
63                             }
64                         }
65                     }.get(); // this extracts the received message
66
67                     assertEquals("Unexpected transaction path " + out,
68                         "akka://test/user/testCreateTransactionChain/$a",
69                         out);
70
71                     expectNoMsg();
72                 }
73
74
75             };
76         }};
77     }
78
79     @Test
80     public void testOnReceiveRegisterListener() throws Exception {
81         new JavaTestKit(getSystem()) {{
82             final Props props = Shard.props("config", Collections.EMPTY_MAP);
83             final ActorRef subject =
84                 getSystem().actorOf(props, "testRegisterChangeListener");
85
86             new Within(duration("1 seconds")) {
87                 protected void run() {
88
89                     subject.tell(
90                         new UpdateSchemaContext(SchemaContextHelper.full()),
91                         getRef());
92
93                     subject.tell(new RegisterChangeListener(TestModel.TEST_PATH,
94                         getRef().path(), AsyncDataBroker.DataChangeScope.BASE),
95                         getRef());
96
97                     final Boolean notificationEnabled = new ExpectMsg<Boolean>("enable notification") {
98                         // do not put code outside this method, will run afterwards
99                         protected Boolean match(Object in) {
100                             if(in instanceof EnableNotification){
101                                 return ((EnableNotification) in).isEnabled();
102                             } else {
103                                 throw noMatch();
104                             }
105                         }
106                     }.get(); // this extracts the received message
107
108                     assertFalse(notificationEnabled);
109
110                     final String out = new ExpectMsg<String>("match hint") {
111                         // do not put code outside this method, will run afterwards
112                         protected String match(Object in) {
113                             if (in.getClass().equals(RegisterChangeListenerReply.class)) {
114                                 RegisterChangeListenerReply reply =
115                                     (RegisterChangeListenerReply) in;
116                                 return reply.getListenerRegistrationPath()
117                                     .toString();
118                             } else {
119                                 throw noMatch();
120                             }
121                         }
122                     }.get(); // this extracts the received message
123
124                     assertTrue(out.matches(
125                         "akka:\\/\\/test\\/user\\/testRegisterChangeListener\\/\\$.*"));
126                 }
127
128
129             };
130         }};
131     }
132
133     @Test
134     public void testCreateTransaction(){
135         new JavaTestKit(getSystem()) {{
136             final Props props = Shard.props("config", Collections.EMPTY_MAP);
137             final ActorRef subject =
138                 getSystem().actorOf(props, "testCreateTransaction");
139
140
141             // Wait for Shard to become a Leader
142             try {
143                 Thread.sleep(1000);
144             } catch (InterruptedException e) {
145                 e.printStackTrace();
146             }
147
148
149             new Within(duration("1 seconds")) {
150                 protected void run() {
151
152                     subject.tell(
153                         new UpdateSchemaContext(TestModel.createTestContext()),
154                         getRef());
155
156                     subject.tell(new CreateTransaction("txn-1", TransactionProxy.TransactionType.READ_ONLY.ordinal() ).toSerializable(),
157                         getRef());
158
159                     final String out = new ExpectMsg<String>("match hint") {
160                         // do not put code outside this method, will run afterwards
161                         protected String match(Object in) {
162                             if (in instanceof CreateTransactionReply) {
163                                 CreateTransactionReply reply =
164                                     (CreateTransactionReply) in;
165                                 return reply.getTransactionActorPath()
166                                     .toString();
167                             } else {
168                                 throw noMatch();
169                             }
170                         }
171                     }.get(); // this extracts the received message
172
173                     assertTrue("Unexpected transaction path " + out,
174                         out.contains("akka://test/user/testCreateTransaction/shard-txn-1"));
175                     expectNoMsg();
176                 }
177
178
179             };
180         }};
181     }
182
183     @Test
184     public void testPeerAddressResolved(){
185         new JavaTestKit(getSystem()) {{
186             Map<String, String> peerAddresses = new HashMap<>();
187             peerAddresses.put("member-2", null);
188             final Props props = Shard.props("config", peerAddresses);
189             final ActorRef subject =
190                 getSystem().actorOf(props, "testPeerAddressResolved");
191
192             new Within(duration("1 seconds")) {
193                 protected void run() {
194
195                     subject.tell(
196                         new PeerAddressResolved("member-2", "akka://foobar"),
197                         getRef());
198
199                     expectNoMsg();
200                 }
201
202
203             };
204         }};
205     }
206
207     private AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> noOpDataChangeListener() {
208         return new AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>() {
209             @Override
210             public void onDataChanged(
211                 AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
212
213             }
214         };
215     }
216 }