Merge "Initial implementation of ShardTransactionChain"
[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.CreateTransaction;
10 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
11 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
12 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
13
14 import static org.junit.Assert.assertTrue;
15
16 public class ShardTransactionChainTest extends AbstractActorTest {
17
18   private static ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
19
20   private static final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", storeExecutor);
21
22   static {
23     store.onGlobalContextUpdated(TestModel.createTestContext());
24   }
25   @Test
26   public void testOnReceiveCreateTransaction() throws Exception {
27     new JavaTestKit(getSystem()) {{
28       final Props props = ShardTransactionChain.props(store.createTransactionChain());
29       final ActorRef subject = getSystem().actorOf(props, "testCreateTransaction");
30
31       new Within(duration("1 seconds")) {
32         protected void run() {
33
34           subject.tell(new CreateTransaction(), getRef());
35
36           final String out = new ExpectMsg<String>("match hint") {
37             // do not put code outside this method, will run afterwards
38             protected String match(Object in) {
39               if (in instanceof CreateTransactionReply) {
40                 return ((CreateTransactionReply) in).getTransactionPath().toString();
41               } else {
42                 throw noMatch();
43               }
44             }
45           }.get(); // this extracts the received message
46
47           assertTrue(out.matches("akka:\\/\\/test\\/user\\/testCreateTransaction\\/\\$.*"));
48           // Will wait for the rest of the 3 seconds
49           expectNoMsg();
50         }
51
52
53       };
54     }};
55   }
56 }