Merge "Small fix to xsql dependencies"
[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
7 import com.google.common.util.concurrent.ListeningExecutorService;
8 import com.google.common.util.concurrent.MoreExecutors;
9
10 import org.junit.BeforeClass;
11 import org.junit.Test;
12 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChain;
13 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChainReply;
14 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
15 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
16 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
17 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19
20 import static org.junit.Assert.assertEquals;
21
22 public class ShardTransactionChainTest extends AbstractActorTest {
23
24     private static ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
25
26     private static final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", storeExecutor,
27             MoreExecutors.sameThreadExecutor());
28
29     private static final SchemaContext testSchemaContext = TestModel.createTestContext();
30
31     private static final ShardContext shardContext = new ShardContext();
32
33     @BeforeClass
34     public static void staticSetup() {
35         store.onGlobalContextUpdated(testSchemaContext);
36     }
37
38     @Test
39     public void testOnReceiveCreateTransaction() throws Exception {
40         new JavaTestKit(getSystem()) {{
41             final Props props = ShardTransactionChain.props(store.createTransactionChain(),
42                     testSchemaContext, shardContext);
43             final ActorRef subject = getSystem().actorOf(props, "testCreateTransaction");
44
45             new Within(duration("1 seconds")) {
46                 @Override
47                 protected void run() {
48
49                     subject.tell(new CreateTransaction("txn-1", TransactionProxy.TransactionType.READ_ONLY.ordinal() ).toSerializable(), getRef());
50
51                     final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
52                         // do not put code outside this method, will run afterwards
53                         @Override
54                         protected String match(Object in) {
55                             if (in.getClass().equals(CreateTransactionReply.SERIALIZABLE_CLASS)) {
56                                 return CreateTransactionReply.fromSerializable(in).getTransactionPath();
57                             }else{
58                                 throw noMatch();
59                             }
60                         }
61                     }.get(); // this extracts the received message
62
63                     assertEquals("Unexpected transaction path " + out,
64                             "akka://test/user/testCreateTransaction/shard-txn-1",
65                             out);
66
67                     // Will wait for the rest of the 3 seconds
68                     expectNoMsg();
69                 }
70
71
72             };
73         }};
74     }
75
76     @Test
77     public void testOnReceiveCloseTransactionChain() throws Exception {
78         new JavaTestKit(getSystem()) {{
79             final Props props = ShardTransactionChain.props(store.createTransactionChain(),
80                     testSchemaContext, shardContext);
81             final ActorRef subject = getSystem().actorOf(props, "testCloseTransactionChain");
82
83             new Within(duration("1 seconds")) {
84                 @Override
85                 protected void run() {
86
87                     subject.tell(new CloseTransactionChain().toSerializable(), getRef());
88
89                     final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
90                         // do not put code outside this method, will run afterwards
91                         @Override
92                         protected String match(Object in) {
93                             if (in.getClass().equals(CloseTransactionChainReply.SERIALIZABLE_CLASS)) {
94                                 return "match";
95                             } else {
96                                 throw noMatch();
97                             }
98                         }
99                     }.get(); // this extracts the received message
100
101                     assertEquals("match", out);
102                     // Will wait for the rest of the 3 seconds
103                     expectNoMsg();
104                 }
105
106
107             };
108         }};
109     }
110 }