NormalizedNode serialization using protocol buffer
[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.protobuff.messages.transaction.ShardTransactionMessages.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
18 public class ShardTransactionChainTest extends AbstractActorTest {
19
20   private static ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
21
22   private static final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", storeExecutor);
23
24   static {
25     store.onGlobalContextUpdated(TestModel.createTestContext());
26   }
27   @Test
28   public void testOnReceiveCreateTransaction() throws Exception {
29     new JavaTestKit(getSystem()) {{
30       final Props props = ShardTransactionChain.props(store.createTransactionChain());
31       final ActorRef subject = getSystem().actorOf(props, "testCreateTransaction");
32
33       new Within(duration("1 seconds")) {
34         protected void run() {
35
36           subject.tell(new CreateTransaction("txn-1"), getRef());
37
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 instanceof CreateTransactionReply) {
42                 return ((CreateTransactionReply) in).getTransactionActorPath().toString();
43               } else {
44                 throw noMatch();
45               }
46             }
47           }.get(); // this extracts the received message
48
49             assertEquals("Unexpected transaction path " + out,
50                 "akka://test/user/testCreateTransaction/shard-txn-1",
51                 out);
52
53             // Will wait for the rest of the 3 seconds
54           expectNoMsg();
55         }
56
57
58       };
59     }};
60   }
61
62   @Test
63   public void testOnReceiveCloseTransactionChain() throws Exception {
64     new JavaTestKit(getSystem()) {{
65       final Props props = ShardTransactionChain.props(store.createTransactionChain());
66       final ActorRef subject = getSystem().actorOf(props, "testCloseTransactionChain");
67
68       new Within(duration("1 seconds")) {
69         protected void run() {
70
71           subject.tell(new CloseTransactionChain(), getRef());
72
73           final String out = new ExpectMsg<String>("match hint") {
74             // do not put code outside this method, will run afterwards
75             protected String match(Object in) {
76               if (in instanceof CloseTransactionChainReply) {
77                 return "match";
78               } else {
79                 throw noMatch();
80               }
81             }
82           }.get(); // this extracts the received message
83
84           assertEquals("match", out);
85           // Will wait for the rest of the 3 seconds
86           expectNoMsg();
87         }
88
89
90       };
91     }};
92   }
93 }