1 package org.opendaylight.controller.cluster.datastore;
3 import akka.actor.ActorRef;
4 import akka.actor.Props;
5 import akka.testkit.JavaTestKit;
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;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
24 public class ShardTest extends AbstractActorTest {
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");
32 new Within(duration("1 seconds")) {
33 protected void run() {
35 subject.tell(new CreateTransactionChain(), getRef());
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()
49 }.get(); // this extracts the received message
51 assertEquals("Unexpected transaction path " + out,
52 "akka://test/user/testCreateTransactionChain/$a",
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");
70 new Within(duration("1 seconds")) {
71 protected void run() {
74 new UpdateSchemaContext(TestModel.createTestContext()),
77 subject.tell(new RegisterChangeListener(TestModel.TEST_PATH,
78 getRef().path(), AsyncDataBroker.DataChangeScope.BASE),
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()
93 }.get(); // this extracts the received message
95 assertTrue(out.matches(
96 "akka:\\/\\/test\\/user\\/testRegisterChangeListener\\/\\$.*"));
97 // Will wait for the rest of the 3 seconds
107 public void testCreateTransaction(){
108 new JavaTestKit(getSystem()) {{
109 final Props props = Shard.props("config");
110 final ActorRef subject =
111 getSystem().actorOf(props, "testCreateTransaction");
113 new Within(duration("1 seconds")) {
114 protected void run() {
117 new UpdateSchemaContext(TestModel.createTestContext()),
120 subject.tell(new CreateTransaction("txn-1"),
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()
135 }.get(); // this extracts the received message
137 assertEquals("Unexpected transaction path " + out,
138 "akka://test/user/testCreateTransaction/shard-txn-1",
150 private AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>> noOpDataChangeListener() {
151 return new AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>() {
153 public void onDataChanged(
154 AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> change) {