Merge "Fixed remove rpc api in rpc registry"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / BasicIntegrationTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.cluster.datastore;
10
11 import akka.actor.ActorPath;
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSelection;
14 import akka.actor.Props;
15 import akka.event.Logging;
16 import akka.testkit.JavaTestKit;
17 import junit.framework.Assert;
18 import org.junit.Test;
19 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
20 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
21 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChain;
22 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChainReply;
23 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
24 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransaction;
25 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransactionReply;
26 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
27 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
28 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
29 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
30 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
31 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
32 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
33 import scala.concurrent.Await;
34 import scala.concurrent.Future;
35 import scala.concurrent.duration.FiniteDuration;
36
37 import java.util.Collections;
38
39 import static junit.framework.Assert.assertEquals;
40
41 public class BasicIntegrationTest extends AbstractActorTest {
42
43     @Test
44     public void integrationTest() throws Exception{
45         // System.setProperty("shard.persistent", "true");
46         // This test will
47         // - create a Shard
48         // - initiate a transaction
49         // - write something
50         // - read the transaction for commit
51         // - commit the transaction
52
53
54         new JavaTestKit(getSystem()) {{
55             final Props props = Shard.props("config", Collections.EMPTY_MAP);
56             final ActorRef shard = getSystem().actorOf(props);
57
58             new Within(duration("5 seconds")) {
59                 protected void run() {
60
61
62                     shard.tell(
63                         new UpdateSchemaContext(TestModel.createTestContext()),
64                         getRef());
65
66
67                     // Wait for a specific log message to show up
68                     final boolean result =
69                         new JavaTestKit.EventFilter<Boolean>(Logging.Info.class
70                         ) {
71                             protected Boolean run() {
72                                 return true;
73                             }
74                         }.from(shard.path().toString())
75                             .message("Switching from state Candidate to Leader")
76                             .occurrences(1).exec();
77
78                     assertEquals(true, result);
79
80                     // 1. Create a TransactionChain
81                     shard.tell(new CreateTransactionChain().toSerializable(), getRef());
82
83                     final ActorSelection transactionChain =
84                         new ExpectMsg<ActorSelection>(duration("1 seconds"), "CreateTransactionChainReply") {
85                             protected ActorSelection match(Object in) {
86                                 if (in.getClass().equals(CreateTransactionChainReply.SERIALIZABLE_CLASS)) {
87                                     ActorPath transactionChainPath =
88                                         CreateTransactionChainReply.fromSerializable(getSystem(),in)
89                                             .getTransactionChainPath();
90                                     return getSystem()
91                                         .actorSelection(transactionChainPath);
92                                 } else {
93                                     throw noMatch();
94                                 }
95                             }
96                         }.get(); // this extracts the received message
97
98                     Assert.assertNotNull(transactionChain);
99
100                     System.out.println("Successfully created transaction chain");
101
102                     // 2. Create a Transaction on the TransactionChain
103                     transactionChain.tell(new CreateTransaction("txn-1", TransactionProxy.TransactionType.WRITE_ONLY.ordinal() ).toSerializable(), getRef());
104
105                     final ActorSelection transaction =
106                         new ExpectMsg<ActorSelection>(duration("1 seconds"), "CreateTransactionReply") {
107                             protected ActorSelection match(Object in) {
108                                 if (CreateTransactionReply.SERIALIZABLE_CLASS.equals(in.getClass())) {
109                                     CreateTransactionReply reply = CreateTransactionReply.fromSerializable(in);
110                                     return getSystem()
111                                         .actorSelection(reply
112                                             .getTransactionPath());
113                                 } else {
114                                     throw noMatch();
115                                 }
116                             }
117                         }.get(); // this extracts the received message
118
119                     Assert.assertNotNull(transaction);
120
121                     System.out.println("Successfully created transaction");
122
123                     // 3. Write some data
124                     transaction.tell(new WriteData(TestModel.TEST_PATH,
125                         ImmutableNodes.containerNode(TestModel.TEST_QNAME), TestModel.createTestContext()).toSerializable(),
126                         getRef());
127
128                     Boolean writeDone = new ExpectMsg<Boolean>(duration("1 seconds"), "WriteDataReply") {
129                         protected Boolean match(Object in) {
130                             if (in.getClass().equals(WriteDataReply.SERIALIZABLE_CLASS)) {
131                                 return true;
132                             } else {
133                                 throw noMatch();
134                             }
135                         }
136                     }.get(); // this extracts the received message
137
138                     Assert.assertTrue(writeDone);
139
140                     System.out.println("Successfully wrote data");
141
142                     // 4. Ready the transaction for commit
143
144                     transaction.tell(new ReadyTransaction().toSerializable(), getRef());
145
146                     final ActorSelection cohort =
147                         new ExpectMsg<ActorSelection>(duration("1 seconds"), "ReadyTransactionReply") {
148                             protected ActorSelection match(Object in) {
149                                 if (in.getClass().equals(ReadyTransactionReply.SERIALIZABLE_CLASS)) {
150                                     ActorPath cohortPath =
151                                         ReadyTransactionReply.fromSerializable(getSystem(),in)
152                                             .getCohortPath();
153                                     return getSystem()
154                                         .actorSelection(cohortPath);
155                                 } else {
156                                     throw noMatch();
157                                 }
158                             }
159                         }.get(); // this extracts the received message
160
161                     Assert.assertNotNull(cohort);
162
163                     System.out.println("Successfully readied the transaction");
164
165                     // 5. PreCommit the transaction
166
167                     cohort.tell(new PreCommitTransaction().toSerializable(), getRef());
168
169                     Boolean preCommitDone =
170                         new ExpectMsg<Boolean>(duration("1 seconds"), "PreCommitTransactionReply") {
171                             protected Boolean match(Object in) {
172                                 if (in.getClass().equals(PreCommitTransactionReply.SERIALIZABLE_CLASS)) {
173                                     return true;
174                                 } else {
175                                     throw noMatch();
176                                 }
177                             }
178                         }.get(); // this extracts the received message
179
180                     Assert.assertTrue(preCommitDone);
181
182                     System.out.println("Successfully pre-committed the transaction");
183
184                     // 6. Commit the transaction
185                     cohort.tell(new CommitTransaction().toSerializable(), getRef());
186
187                     // FIXME : Add assertions that the commit worked and that the cohort and transaction actors were terminated
188
189                     System.out.println("TODO : Check Successfully committed the transaction");
190                 }
191
192
193             };
194         }
195
196             private ActorRef watchActor(ActorSelection actor) {
197                 Future<ActorRef> future = actor
198                     .resolveOne(FiniteDuration.apply(100, "milliseconds"));
199
200                 try {
201                     ActorRef actorRef = Await.result(future,
202                         FiniteDuration.apply(100, "milliseconds"));
203
204                     watch(actorRef);
205
206                     return actorRef;
207                 } catch (Exception e) {
208                     throw new RuntimeException(e);
209                 }
210
211             }
212         };
213
214
215     }
216 }