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
36 * @author Basheeruddin Ahmed <syedbahm@cisco.com>
38 public class ShardTransactionFailureTest extends AbstractActorTest {
39 private static ListeningExecutorService storeExecutor =
40 MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
42 private static final InMemoryDOMDataStore store =
43 new InMemoryDOMDataStore("OPER", storeExecutor,
44 MoreExecutors.sameThreadExecutor());
46 private static final SchemaContext testSchemaContext =
47 TestModel.createTestContext();
49 private static final ShardIdentifier SHARD_IDENTIFIER =
50 ShardIdentifier.builder().memberName("member-1")
51 .shardName("inventory").type("config").build();
54 store.onGlobalContextUpdated(testSchemaContext);
58 @Test(expected = ReadFailedException.class)
59 public void testNegativeReadWithReadOnlyTransactionClosed()
62 final ActorRef shard =
63 getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP));
65 ShardTransaction.props(store.newReadOnlyTransaction(), shard,
66 TestModel.createTestContext());
68 final TestActorRef<ShardTransaction> subject = TestActorRef
69 .create(getSystem(), props,
70 "testNegativeReadWithReadOnlyTransactionClosed");
72 ShardTransactionMessages.ReadData readData =
73 ShardTransactionMessages.ReadData.newBuilder()
74 .setInstanceIdentifierPathArguments(
75 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
78 Future<Object> future =
79 akka.pattern.Patterns.ask(subject, readData, 3000);
80 assertTrue(future.isCompleted());
81 Await.result(future, Duration.Zero());
83 ((ShardReadTransaction) subject.underlyingActor())
84 .forUnitTestOnlyExplicitTransactionClose();
86 future = akka.pattern.Patterns.ask(subject, readData, 3000);
87 Await.result(future, Duration.Zero());
93 @Test(expected = ReadFailedException.class)
94 public void testNegativeReadWithReadWriteOnlyTransactionClosed()
97 final ActorRef shard =
98 getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP));
100 ShardTransaction.props(store.newReadWriteTransaction(), shard,
101 TestModel.createTestContext());
103 final TestActorRef<ShardTransaction> subject = TestActorRef
104 .create(getSystem(), props,
105 "testNegativeReadWithReadWriteOnlyTransactionClosed");
107 ShardTransactionMessages.ReadData readData =
108 ShardTransactionMessages.ReadData.newBuilder()
109 .setInstanceIdentifierPathArguments(
110 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
113 Future<Object> future =
114 akka.pattern.Patterns.ask(subject, readData, 3000);
115 assertTrue(future.isCompleted());
116 Await.result(future, Duration.Zero());
118 ((ShardReadWriteTransaction) subject.underlyingActor())
119 .forUnitTestOnlyExplicitTransactionClose();
121 future = akka.pattern.Patterns.ask(subject, readData, 3000);
122 Await.result(future, Duration.Zero());
127 @Test(expected = ReadFailedException.class)
128 public void testNegativeExistsWithReadWriteOnlyTransactionClosed()
131 final ActorRef shard =
132 getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP));
134 ShardTransaction.props(store.newReadWriteTransaction(), shard,
135 TestModel.createTestContext());
137 final TestActorRef<ShardTransaction> subject = TestActorRef
138 .create(getSystem(), props,
139 "testNegativeExistsWithReadWriteOnlyTransactionClosed");
141 ShardTransactionMessages.DataExists dataExists =
142 ShardTransactionMessages.DataExists.newBuilder()
143 .setInstanceIdentifierPathArguments(
144 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
148 Future<Object> future =
149 akka.pattern.Patterns.ask(subject, dataExists, 3000);
150 assertTrue(future.isCompleted());
151 Await.result(future, Duration.Zero());
153 ((ShardReadWriteTransaction) subject.underlyingActor())
154 .forUnitTestOnlyExplicitTransactionClose();
156 future = akka.pattern.Patterns.ask(subject, dataExists, 3000);
157 Await.result(future, Duration.Zero());
162 @Test(expected = IllegalStateException.class)
163 public void testNegativeWriteWithTransactionReady() throws Exception {
166 final ActorRef shard =
167 getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP));
169 ShardTransaction.props(store.newWriteOnlyTransaction(), shard,
170 TestModel.createTestContext());
172 final TestActorRef<ShardTransaction> subject = TestActorRef
173 .create(getSystem(), props,
174 "testNegativeWriteWithTransactionReady");
176 ShardTransactionMessages.ReadyTransaction readyTransaction =
177 ShardTransactionMessages.ReadyTransaction.newBuilder().build();
179 Future<Object> future =
180 akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
181 assertTrue(future.isCompleted());
182 Await.result(future, Duration.Zero());
184 ShardTransactionMessages.WriteData writeData =
185 ShardTransactionMessages.WriteData.newBuilder()
186 .setInstanceIdentifierPathArguments(
187 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
188 .build()).setNormalizedNode(
189 NormalizedNodeMessages.Node.newBuilder().build()
193 future = akka.pattern.Patterns.ask(subject, writeData, 3000);
194 assertTrue(future.isCompleted());
195 Await.result(future, Duration.Zero());
201 @Test(expected = IllegalStateException.class)
202 public void testNegativeReadWriteWithTransactionReady() throws Exception {
205 final ActorRef shard =
206 getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP));
208 ShardTransaction.props(store.newReadWriteTransaction(), shard,
209 TestModel.createTestContext());
211 final TestActorRef<ShardTransaction> subject = TestActorRef
212 .create(getSystem(), props,
213 "testNegativeReadWriteWithTransactionReady");
215 ShardTransactionMessages.ReadyTransaction readyTransaction =
216 ShardTransactionMessages.ReadyTransaction.newBuilder().build();
218 Future<Object> future =
219 akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
220 assertTrue(future.isCompleted());
221 Await.result(future, Duration.Zero());
223 ShardTransactionMessages.WriteData writeData =
224 ShardTransactionMessages.WriteData.newBuilder()
225 .setInstanceIdentifierPathArguments(
226 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
227 .build()).setNormalizedNode(
228 NormalizedNodeMessages.Node.newBuilder().build()
232 future = akka.pattern.Patterns.ask(subject, writeData, 3000);
233 assertTrue(future.isCompleted());
234 Await.result(future, Duration.Zero());
239 @Test(expected = IllegalStateException.class)
240 public void testNegativeMergeTransactionReady() throws Exception {
243 final ActorRef shard =
244 getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP));
246 ShardTransaction.props(store.newReadWriteTransaction(), shard,
247 TestModel.createTestContext());
249 final TestActorRef<ShardTransaction> subject = TestActorRef
250 .create(getSystem(), props, "testNegativeMergeTransactionReady");
252 ShardTransactionMessages.ReadyTransaction readyTransaction =
253 ShardTransactionMessages.ReadyTransaction.newBuilder().build();
255 Future<Object> future =
256 akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
257 assertTrue(future.isCompleted());
258 Await.result(future, Duration.Zero());
260 ShardTransactionMessages.MergeData mergeData =
261 ShardTransactionMessages.MergeData.newBuilder()
262 .setInstanceIdentifierPathArguments(
263 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
264 .build()).setNormalizedNode(
265 NormalizedNodeMessages.Node.newBuilder().build()
269 future = akka.pattern.Patterns.ask(subject, mergeData, 3000);
270 assertTrue(future.isCompleted());
271 Await.result(future, Duration.Zero());
277 @Test(expected = IllegalStateException.class)
278 public void testNegativeDeleteDataWhenTransactionReady() throws Exception {
281 final ActorRef shard =
282 getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP));
284 ShardTransaction.props(store.newReadWriteTransaction(), shard,
285 TestModel.createTestContext());
287 final TestActorRef<ShardTransaction> subject = TestActorRef
288 .create(getSystem(), props,
289 "testNegativeDeleteDataWhenTransactionReady");
291 ShardTransactionMessages.ReadyTransaction readyTransaction =
292 ShardTransactionMessages.ReadyTransaction.newBuilder().build();
294 Future<Object> future =
295 akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
296 assertTrue(future.isCompleted());
297 Await.result(future, Duration.Zero());
299 ShardTransactionMessages.DeleteData deleteData =
300 ShardTransactionMessages.DeleteData.newBuilder()
301 .setInstanceIdentifierPathArguments(
302 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
305 future = akka.pattern.Patterns.ask(subject, deleteData, 3000);
306 assertTrue(future.isCompleted());
307 Await.result(future, Duration.Zero());