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 = IllegalStateException.class)
128 public void testNegativeWriteWithTransactionReady() throws Exception {
131 final ActorRef shard =
132 getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP));
134 ShardTransaction.props(store.newWriteOnlyTransaction(), shard,
135 TestModel.createTestContext());
137 final TestActorRef<ShardTransaction> subject = TestActorRef
138 .create(getSystem(), props,
139 "testNegativeWriteWithTransactionReady");
141 ShardTransactionMessages.ReadyTransaction readyTransaction =
142 ShardTransactionMessages.ReadyTransaction.newBuilder().build();
144 Future<Object> future =
145 akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
146 assertTrue(future.isCompleted());
147 Await.result(future, Duration.Zero());
149 ShardTransactionMessages.WriteData writeData =
150 ShardTransactionMessages.WriteData.newBuilder()
151 .setInstanceIdentifierPathArguments(
152 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
153 .build()).setNormalizedNode(
154 NormalizedNodeMessages.Node.newBuilder().build()
158 future = akka.pattern.Patterns.ask(subject, writeData, 3000);
159 assertTrue(future.isCompleted());
160 Await.result(future, Duration.Zero());
166 @Test(expected = IllegalStateException.class)
167 public void testNegativeReadWriteWithTransactionReady() throws Exception {
170 final ActorRef shard =
171 getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP));
173 ShardTransaction.props(store.newReadWriteTransaction(), shard,
174 TestModel.createTestContext());
176 final TestActorRef<ShardTransaction> subject = TestActorRef
177 .create(getSystem(), props,
178 "testNegativeReadWriteWithTransactionReady");
180 ShardTransactionMessages.ReadyTransaction readyTransaction =
181 ShardTransactionMessages.ReadyTransaction.newBuilder().build();
183 Future<Object> future =
184 akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
185 assertTrue(future.isCompleted());
186 Await.result(future, Duration.Zero());
188 ShardTransactionMessages.WriteData writeData =
189 ShardTransactionMessages.WriteData.newBuilder()
190 .setInstanceIdentifierPathArguments(
191 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
192 .build()).setNormalizedNode(
193 NormalizedNodeMessages.Node.newBuilder().build()
197 future = akka.pattern.Patterns.ask(subject, writeData, 3000);
198 assertTrue(future.isCompleted());
199 Await.result(future, Duration.Zero());
204 @Test(expected = IllegalStateException.class)
205 public void testNegativeMergeTransactionReady() throws Exception {
208 final ActorRef shard =
209 getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP));
211 ShardTransaction.props(store.newReadWriteTransaction(), shard,
212 TestModel.createTestContext());
214 final TestActorRef<ShardTransaction> subject = TestActorRef
215 .create(getSystem(), props, "testNegativeMergeTransactionReady");
217 ShardTransactionMessages.ReadyTransaction readyTransaction =
218 ShardTransactionMessages.ReadyTransaction.newBuilder().build();
220 Future<Object> future =
221 akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
222 assertTrue(future.isCompleted());
223 Await.result(future, Duration.Zero());
225 ShardTransactionMessages.MergeData mergeData =
226 ShardTransactionMessages.MergeData.newBuilder()
227 .setInstanceIdentifierPathArguments(
228 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
229 .build()).setNormalizedNode(
230 NormalizedNodeMessages.Node.newBuilder().build()
234 future = akka.pattern.Patterns.ask(subject, mergeData, 3000);
235 assertTrue(future.isCompleted());
236 Await.result(future, Duration.Zero());
242 @Test(expected = IllegalStateException.class)
243 public void testNegativeDeleteDataWhenTransactionReady() throws Exception {
246 final ActorRef shard =
247 getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP));
249 ShardTransaction.props(store.newReadWriteTransaction(), shard,
250 TestModel.createTestContext());
252 final TestActorRef<ShardTransaction> subject = TestActorRef
253 .create(getSystem(), props,
254 "testNegativeDeleteDataWhenTransactionReady");
256 ShardTransactionMessages.ReadyTransaction readyTransaction =
257 ShardTransactionMessages.ReadyTransaction.newBuilder().build();
259 Future<Object> future =
260 akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
261 assertTrue(future.isCompleted());
262 Await.result(future, Duration.Zero());
264 ShardTransactionMessages.DeleteData deleteData =
265 ShardTransactionMessages.DeleteData.newBuilder()
266 .setInstanceIdentifierPathArguments(
267 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
270 future = akka.pattern.Patterns.ask(subject, deleteData, 3000);
271 assertTrue(future.isCompleted());
272 Await.result(future, Duration.Zero());