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.RegisterChangeListener;
11 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
12 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
13 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
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.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
25 public class ShardTest extends AbstractActorTest {
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");
33 new Within(duration("1 seconds")) {
34 protected void run() {
36 subject.tell(new CreateTransactionChain().toSerializable(), getRef());
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.getClass().equals(CreateTransactionChainReply.SERIALIZABLE_CLASS)){
42 CreateTransactionChainReply reply =
43 CreateTransactionChainReply.fromSerializable(getSystem(),in);
44 return reply.getTransactionChainPath()
50 }.get(); // this extracts the received message
52 assertEquals("Unexpected transaction path " + out,
53 "akka://test/user/testCreateTransactionChain/$a",
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");
71 new Within(duration("1 seconds")) {
72 protected void run() {
75 new UpdateSchemaContext(SchemaContextHelper.full()),
78 subject.tell(new RegisterChangeListener(TestModel.TEST_PATH,
79 getRef().path(), AsyncDataBroker.DataChangeScope.BASE).toSerializable(),
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()
94 }.get(); // this extracts the received message
96 assertTrue(out.matches(
97 "akka:\\/\\/test\\/user\\/testRegisterChangeListener\\/\\$.*"));
98 // Will wait for the rest of the 3 seconds
108 public void testCreateTransaction(){
109 new JavaTestKit(getSystem()) {{
110 final Props props = Shard.props("config");
111 final ActorRef subject =
112 getSystem().actorOf(props, "testCreateTransaction");
114 new Within(duration("1 seconds")) {
115 protected void run() {
118 new UpdateSchemaContext(TestModel.createTestContext()),
121 subject.tell(new CreateTransaction("txn-1").toSerializable(),
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()
136 }.get(); // this extracts the received message
138 assertEquals("Unexpected transaction path " + out,
139 "akka://test/user/testCreateTransaction/shard-txn-1",
151 private AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> noOpDataChangeListener() {
152 return new AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>() {
154 public void onDataChanged(
155 AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {