3 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
11 package org.opendaylight.controller.cluster.datastore;
13 import akka.actor.ActorRef;
14 import akka.actor.Props;
15 import akka.testkit.TestActorRef;
16 import com.google.common.util.concurrent.ListeningExecutorService;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import org.junit.Test;
19 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
20 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
23 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
24 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import scala.concurrent.Await;
27 import scala.concurrent.Future;
28 import scala.concurrent.duration.Duration;
30 import java.util.Collections;
32 import static org.junit.Assert.assertTrue;
35 * Covers negative test cases
37 * @author Basheeruddin Ahmed <syedbahm@cisco.com>
39 public class ShardTransactionFailureTest extends AbstractActorTest {
40 private static ListeningExecutorService storeExecutor =
41 MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
43 private static final InMemoryDOMDataStore store =
44 new InMemoryDOMDataStore("OPER", storeExecutor,
45 MoreExecutors.sameThreadExecutor());
47 private static final SchemaContext testSchemaContext =
48 TestModel.createTestContext();
50 private static final ShardIdentifier SHARD_IDENTIFIER =
51 ShardIdentifier.builder().memberName("member-1")
52 .shardName("inventory").type("operational").build();
55 store.onGlobalContextUpdated(testSchemaContext);
59 @Test(expected = ReadFailedException.class)
60 public void testNegativeReadWithReadOnlyTransactionClosed()
63 final ActorRef shard =
64 getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP, null));
66 ShardTransaction.props(store.newReadOnlyTransaction(), shard,
67 TestModel.createTestContext());
69 final TestActorRef<ShardTransaction> subject = TestActorRef
70 .create(getSystem(), props,
71 "testNegativeReadWithReadOnlyTransactionClosed");
73 ShardTransactionMessages.ReadData readData =
74 ShardTransactionMessages.ReadData.newBuilder()
75 .setInstanceIdentifierPathArguments(
76 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
79 Future<Object> future =
80 akka.pattern.Patterns.ask(subject, readData, 3000);
81 assertTrue(future.isCompleted());
82 Await.result(future, Duration.Zero());
84 ((ShardReadTransaction) subject.underlyingActor())
85 .forUnitTestOnlyExplicitTransactionClose();
87 future = akka.pattern.Patterns.ask(subject, readData, 3000);
88 Await.result(future, Duration.Zero());
94 @Test(expected = ReadFailedException.class)
95 public void testNegativeReadWithReadWriteOnlyTransactionClosed()
98 final ActorRef shard =
99 getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP,null));
101 ShardTransaction.props(store.newReadWriteTransaction(), shard,
102 TestModel.createTestContext());
104 final TestActorRef<ShardTransaction> subject = TestActorRef
105 .create(getSystem(), props,
106 "testNegativeReadWithReadWriteOnlyTransactionClosed");
108 ShardTransactionMessages.ReadData readData =
109 ShardTransactionMessages.ReadData.newBuilder()
110 .setInstanceIdentifierPathArguments(
111 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
114 Future<Object> future =
115 akka.pattern.Patterns.ask(subject, readData, 3000);
116 assertTrue(future.isCompleted());
117 Await.result(future, Duration.Zero());
119 ((ShardReadWriteTransaction) subject.underlyingActor())
120 .forUnitTestOnlyExplicitTransactionClose();
122 future = akka.pattern.Patterns.ask(subject, readData, 3000);
123 Await.result(future, Duration.Zero());
128 @Test(expected = ReadFailedException.class)
129 public void testNegativeExistsWithReadWriteOnlyTransactionClosed()
132 final ActorRef shard =
133 getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP,null));
135 ShardTransaction.props(store.newReadWriteTransaction(), shard,
136 TestModel.createTestContext());
138 final TestActorRef<ShardTransaction> subject = TestActorRef
139 .create(getSystem(), props,
140 "testNegativeExistsWithReadWriteOnlyTransactionClosed");
142 ShardTransactionMessages.DataExists dataExists =
143 ShardTransactionMessages.DataExists.newBuilder()
144 .setInstanceIdentifierPathArguments(
145 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
149 Future<Object> future =
150 akka.pattern.Patterns.ask(subject, dataExists, 3000);
151 assertTrue(future.isCompleted());
152 Await.result(future, Duration.Zero());
154 ((ShardReadWriteTransaction) subject.underlyingActor())
155 .forUnitTestOnlyExplicitTransactionClose();
157 future = akka.pattern.Patterns.ask(subject, dataExists, 3000);
158 Await.result(future, Duration.Zero());
163 @Test(expected = IllegalStateException.class)
164 public void testNegativeWriteWithTransactionReady() throws Exception {
167 final ActorRef shard =
168 getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP,null));
170 ShardTransaction.props(store.newWriteOnlyTransaction(), shard,
171 TestModel.createTestContext());
173 final TestActorRef<ShardTransaction> subject = TestActorRef
174 .create(getSystem(), props,
175 "testNegativeWriteWithTransactionReady");
177 ShardTransactionMessages.ReadyTransaction readyTransaction =
178 ShardTransactionMessages.ReadyTransaction.newBuilder().build();
180 Future<Object> future =
181 akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
182 assertTrue(future.isCompleted());
183 Await.result(future, Duration.Zero());
185 ShardTransactionMessages.WriteData writeData =
186 ShardTransactionMessages.WriteData.newBuilder()
187 .setInstanceIdentifierPathArguments(
188 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
189 .build()).setNormalizedNode(
190 NormalizedNodeMessages.Node.newBuilder().build()
194 future = akka.pattern.Patterns.ask(subject, writeData, 3000);
195 assertTrue(future.isCompleted());
196 Await.result(future, Duration.Zero());
202 @Test(expected = IllegalStateException.class)
203 public void testNegativeReadWriteWithTransactionReady() throws Exception {
206 final ActorRef shard =
207 getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP,null));
209 ShardTransaction.props(store.newReadWriteTransaction(), shard,
210 TestModel.createTestContext());
212 final TestActorRef<ShardTransaction> subject = TestActorRef
213 .create(getSystem(), props,
214 "testNegativeReadWriteWithTransactionReady");
216 ShardTransactionMessages.ReadyTransaction readyTransaction =
217 ShardTransactionMessages.ReadyTransaction.newBuilder().build();
219 Future<Object> future =
220 akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
221 assertTrue(future.isCompleted());
222 Await.result(future, Duration.Zero());
224 ShardTransactionMessages.WriteData writeData =
225 ShardTransactionMessages.WriteData.newBuilder()
226 .setInstanceIdentifierPathArguments(
227 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
228 .build()).setNormalizedNode(
229 NormalizedNodeMessages.Node.newBuilder().build()
233 future = akka.pattern.Patterns.ask(subject, writeData, 3000);
234 assertTrue(future.isCompleted());
235 Await.result(future, Duration.Zero());
240 @Test(expected = IllegalStateException.class)
241 public void testNegativeMergeTransactionReady() throws Exception {
244 final ActorRef shard =
245 getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP,null));
247 ShardTransaction.props(store.newReadWriteTransaction(), shard,
248 TestModel.createTestContext());
250 final TestActorRef<ShardTransaction> subject = TestActorRef
251 .create(getSystem(), props, "testNegativeMergeTransactionReady");
253 ShardTransactionMessages.ReadyTransaction readyTransaction =
254 ShardTransactionMessages.ReadyTransaction.newBuilder().build();
256 Future<Object> future =
257 akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
258 assertTrue(future.isCompleted());
259 Await.result(future, Duration.Zero());
261 ShardTransactionMessages.MergeData mergeData =
262 ShardTransactionMessages.MergeData.newBuilder()
263 .setInstanceIdentifierPathArguments(
264 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
265 .build()).setNormalizedNode(
266 NormalizedNodeMessages.Node.newBuilder().build()
270 future = akka.pattern.Patterns.ask(subject, mergeData, 3000);
271 assertTrue(future.isCompleted());
272 Await.result(future, Duration.Zero());
278 @Test(expected = IllegalStateException.class)
279 public void testNegativeDeleteDataWhenTransactionReady() throws Exception {
282 final ActorRef shard =
283 getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP,null));
285 ShardTransaction.props(store.newReadWriteTransaction(), shard,
286 TestModel.createTestContext());
288 final TestActorRef<ShardTransaction> subject = TestActorRef
289 .create(getSystem(), props,
290 "testNegativeDeleteDataWhenTransactionReady");
292 ShardTransactionMessages.ReadyTransaction readyTransaction =
293 ShardTransactionMessages.ReadyTransaction.newBuilder().build();
295 Future<Object> future =
296 akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
297 assertTrue(future.isCompleted());
298 Await.result(future, Duration.Zero());
300 ShardTransactionMessages.DeleteData deleteData =
301 ShardTransactionMessages.DeleteData.newBuilder()
302 .setInstanceIdentifierPathArguments(
303 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
306 future = akka.pattern.Patterns.ask(subject, deleteData, 3000);
307 assertTrue(future.isCompleted());
308 Await.result(future, Duration.Zero());