Merge "BUG-997 Use shared schema context factory in netconf-connector"
[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 org.junit.Test;
18 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
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 import static junit.framework.Assert.assertTrue;
41 import static org.junit.Assert.assertNotNull;
42
43 public class BasicIntegrationTest extends AbstractActorTest {
44
45     @Test
46     public void integrationTest() throws Exception{
47         // System.setProperty("shard.persistent", "true");
48         // This test will
49         // - create a Shard
50         // - initiate a transaction
51         // - write something
52         // - read the transaction for commit
53         // - commit the transaction
54
55
56         new JavaTestKit(getSystem()) {{
57             final ShardIdentifier identifier =
58                 ShardIdentifier.builder().memberName("member-1")
59                     .shardName("inventory").type("config").build();
60
61             final Props props = Shard.props(identifier, Collections.EMPTY_MAP);
62             final ActorRef shard = getSystem().actorOf(props);
63
64             new Within(duration("5 seconds")) {
65                 protected void run() {
66
67
68                     shard.tell(
69                         new UpdateSchemaContext(TestModel.createTestContext()),
70                         getRef());
71
72
73                     // Wait for a specific log message to show up
74                     final boolean result =
75                         new JavaTestKit.EventFilter<Boolean>(Logging.Info.class
76                         ) {
77                             protected Boolean run() {
78                                 return true;
79                             }
80                         }.from(shard.path().toString())
81                             .message("Switching from state Candidate to Leader")
82                             .occurrences(1).exec();
83
84                     assertEquals(true, result);
85
86                     // 1. Create a TransactionChain
87                     shard.tell(new CreateTransactionChain().toSerializable(), getRef());
88
89                     final ActorSelection transactionChain =
90                         new ExpectMsg<ActorSelection>(duration("1 seconds"), "CreateTransactionChainReply") {
91                             protected ActorSelection match(Object in) {
92                                 if (in.getClass().equals(CreateTransactionChainReply.SERIALIZABLE_CLASS)) {
93                                     ActorPath transactionChainPath =
94                                         CreateTransactionChainReply.fromSerializable(getSystem(),in)
95                                             .getTransactionChainPath();
96                                     return getSystem()
97                                         .actorSelection(transactionChainPath);
98                                 } else {
99                                     throw noMatch();
100                                 }
101                             }
102                         }.get(); // this extracts the received message
103
104                     assertNotNull(transactionChain);
105
106                     System.out.println("Successfully created transaction chain");
107
108                     // 2. Create a Transaction on the TransactionChain
109                     transactionChain.tell(new CreateTransaction("txn-1", TransactionProxy.TransactionType.WRITE_ONLY.ordinal() ).toSerializable(), getRef());
110
111                     final ActorSelection transaction =
112                         new ExpectMsg<ActorSelection>(duration("1 seconds"), "CreateTransactionReply") {
113                             protected ActorSelection match(Object in) {
114                                 if (CreateTransactionReply.SERIALIZABLE_CLASS.equals(in.getClass())) {
115                                     CreateTransactionReply reply = CreateTransactionReply.fromSerializable(in);
116                                     return getSystem()
117                                         .actorSelection(reply
118                                             .getTransactionPath());
119                                 } else {
120                                     throw noMatch();
121                                 }
122                             }
123                         }.get(); // this extracts the received message
124
125                     assertNotNull(transaction);
126
127                     System.out.println("Successfully created transaction");
128
129                     // 3. Write some data
130                     transaction.tell(new WriteData(TestModel.TEST_PATH,
131                         ImmutableNodes.containerNode(TestModel.TEST_QNAME), TestModel.createTestContext()).toSerializable(),
132                         getRef());
133
134                     Boolean writeDone = new ExpectMsg<Boolean>(duration("1 seconds"), "WriteDataReply") {
135                         protected Boolean match(Object in) {
136                             if (in.getClass().equals(WriteDataReply.SERIALIZABLE_CLASS)) {
137                                 return true;
138                             } else {
139                                 throw noMatch();
140                             }
141                         }
142                     }.get(); // this extracts the received message
143
144                     assertTrue(writeDone);
145
146                     System.out.println("Successfully wrote data");
147
148                     // 4. Ready the transaction for commit
149
150                     transaction.tell(new ReadyTransaction().toSerializable(), getRef());
151
152                     final ActorSelection cohort =
153                         new ExpectMsg<ActorSelection>(duration("1 seconds"), "ReadyTransactionReply") {
154                             protected ActorSelection match(Object in) {
155                                 if (in.getClass().equals(ReadyTransactionReply.SERIALIZABLE_CLASS)) {
156                                     ActorPath cohortPath =
157                                         ReadyTransactionReply.fromSerializable(getSystem(),in)
158                                             .getCohortPath();
159                                     return getSystem()
160                                         .actorSelection(cohortPath);
161                                 } else {
162                                     throw noMatch();
163                                 }
164                             }
165                         }.get(); // this extracts the received message
166
167                     assertNotNull(cohort);
168
169                     System.out.println("Successfully readied the transaction");
170
171                     // 5. PreCommit the transaction
172
173                     cohort.tell(new PreCommitTransaction().toSerializable(), getRef());
174
175                     Boolean preCommitDone =
176                         new ExpectMsg<Boolean>(duration("1 seconds"), "PreCommitTransactionReply") {
177                             protected Boolean match(Object in) {
178                                 if (in.getClass().equals(PreCommitTransactionReply.SERIALIZABLE_CLASS)) {
179                                     return true;
180                                 } else {
181                                     throw noMatch();
182                                 }
183                             }
184                         }.get(); // this extracts the received message
185
186                     assertTrue(preCommitDone);
187
188                     System.out.println("Successfully pre-committed the transaction");
189
190                     // 6. Commit the transaction
191                     cohort.tell(new CommitTransaction().toSerializable(), getRef());
192
193                     // FIXME : Add assertions that the commit worked and that the cohort and transaction actors were terminated
194
195                     System.out.println("TODO : Check Successfully committed the transaction");
196                 }
197
198
199             };
200         }
201
202             private ActorRef watchActor(ActorSelection actor) {
203                 Future<ActorRef> future = actor
204                     .resolveOne(FiniteDuration.apply(100, "milliseconds"));
205
206                 try {
207                     ActorRef actorRef = Await.result(future,
208                         FiniteDuration.apply(100, "milliseconds"));
209
210                     watch(actorRef);
211
212                     return actorRef;
213                 } catch (Exception e) {
214                     throw new RuntimeException(e);
215                 }
216
217             }
218         };
219
220
221     }
222 }