bc3a1046566b2259012916c85e70ae933d921b3d
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardTransactionChainTest.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 com.google.common.util.concurrent.ListeningExecutorService;
7 import com.google.common.util.concurrent.MoreExecutors;
8 import org.junit.Test;
9 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChain;
10 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChainReply;
11 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
12 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
13 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
14 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertTrue;
18
19 public class ShardTransactionChainTest extends AbstractActorTest {
20
21   private static ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
22
23   private static final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", storeExecutor);
24
25   static {
26     store.onGlobalContextUpdated(TestModel.createTestContext());
27   }
28   @Test
29   public void testOnReceiveCreateTransaction() throws Exception {
30     new JavaTestKit(getSystem()) {{
31       final Props props = ShardTransactionChain.props(store.createTransactionChain());
32       final ActorRef subject = getSystem().actorOf(props, "testCreateTransaction");
33
34       new Within(duration("1 seconds")) {
35         protected void run() {
36
37           subject.tell(new CreateTransaction(), getRef());
38
39           final String out = new ExpectMsg<String>("match hint") {
40             // do not put code outside this method, will run afterwards
41             protected String match(Object in) {
42               if (in instanceof CreateTransactionReply) {
43                 return ((CreateTransactionReply) in).getTransactionPath().toString();
44               } else {
45                 throw noMatch();
46               }
47             }
48           }.get(); // this extracts the received message
49
50           assertTrue(out.matches("akka:\\/\\/test\\/user\\/testCreateTransaction\\/\\$.*"));
51           // Will wait for the rest of the 3 seconds
52           expectNoMsg();
53         }
54
55
56       };
57     }};
58   }
59
60   @Test
61   public void testOnReceiveCloseTransactionChain() throws Exception {
62     new JavaTestKit(getSystem()) {{
63       final Props props = ShardTransactionChain.props(store.createTransactionChain());
64       final ActorRef subject = getSystem().actorOf(props, "testCloseTransactionChain");
65
66       new Within(duration("1 seconds")) {
67         protected void run() {
68
69           subject.tell(new CloseTransactionChain(), getRef());
70
71           final String out = new ExpectMsg<String>("match hint") {
72             // do not put code outside this method, will run afterwards
73             protected String match(Object in) {
74               if (in instanceof CloseTransactionChainReply) {
75                 return "match";
76               } else {
77                 throw noMatch();
78               }
79             }
80           }.get(); // this extracts the received message
81
82           assertEquals("match", out);
83           // Will wait for the rest of the 3 seconds
84           expectNoMsg();
85         }
86
87
88       };
89     }};
90   }
91 }