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.Assert;
19 import org.junit.Test;
20 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
21 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
22 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
23 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
24 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
25 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import scala.concurrent.Await;
28 import scala.concurrent.Future;
29 import scala.concurrent.duration.Duration;
31 import java.util.Collections;
33 import static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.assertTrue;
37 * Covers negative test cases
38 * @author Basheeruddin Ahmed <syedbahm@cisco.com>
40 public class ShardTransactionFailureTest extends AbstractActorTest {
41 private static ListeningExecutorService storeExecutor =
42 MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
44 private static final InMemoryDOMDataStore store =
45 new InMemoryDOMDataStore("OPER", storeExecutor,
46 MoreExecutors.sameThreadExecutor());
48 private static final SchemaContext testSchemaContext =
49 TestModel.createTestContext();
52 store.onGlobalContextUpdated(testSchemaContext);
57 public void testNegativePerformingWriteOperationOnReadTransaction()
63 .actorOf(Shard.props("config", Collections.EMPTY_MAP));
66 .props(store.newReadOnlyTransaction(), shard, TestModel
67 .createTestContext());
68 final TestActorRef subject = TestActorRef.apply(props, getSystem());
71 .receive(new DeleteData(TestModel.TEST_PATH).toSerializable(),
73 Assert.assertFalse(true);
76 } catch (Exception cs) {
77 assertEquals(cs.getClass().getSimpleName(),
78 Exception.class.getSimpleName());
79 assertTrue(cs.getMessage().startsWith(
80 "ShardTransaction:handleRecieve received an unknown message"));
84 @Test(expected = ReadFailedException.class)
85 public void testNegativeReadWithReadOnlyTransactionClosed()
88 final ActorRef shard =
89 getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
91 ShardTransaction.props(store.newReadOnlyTransaction(), shard,
92 TestModel.createTestContext());
94 final TestActorRef<ShardTransaction> subject = TestActorRef
95 .create(getSystem(), props,
96 "testNegativeReadWithReadOnlyTransactionClosed");
98 ShardTransactionMessages.ReadData readData =
99 ShardTransactionMessages.ReadData.newBuilder()
100 .setInstanceIdentifierPathArguments(
101 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
104 Future<Object> future =
105 akka.pattern.Patterns.ask(subject, readData, 3000);
106 assertTrue(future.isCompleted());
107 Await.result(future, Duration.Zero());
109 ((ShardReadTransaction) subject.underlyingActor())
110 .forUnitTestOnlyExplicitTransactionClose();
112 future = akka.pattern.Patterns.ask(subject, readData, 3000);
113 Await.result(future, Duration.Zero());
119 @Test(expected = ReadFailedException.class)
120 public void testNegativeReadWithReadWriteOnlyTransactionClosed()
123 final ActorRef shard =
124 getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
126 ShardTransaction.props(store.newReadWriteTransaction(), shard,
127 TestModel.createTestContext());
129 final TestActorRef<ShardTransaction> subject = TestActorRef
130 .create(getSystem(), props,
131 "testNegativeReadWithReadWriteOnlyTransactionClosed");
133 ShardTransactionMessages.ReadData readData =
134 ShardTransactionMessages.ReadData.newBuilder()
135 .setInstanceIdentifierPathArguments(
136 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
139 Future<Object> future =
140 akka.pattern.Patterns.ask(subject, readData, 3000);
141 assertTrue(future.isCompleted());
142 Await.result(future, Duration.Zero());
144 ((ShardReadWriteTransaction) subject.underlyingActor())
145 .forUnitTestOnlyExplicitTransactionClose();
147 future = akka.pattern.Patterns.ask(subject, readData, 3000);
148 Await.result(future, Duration.Zero());
154 @Test(expected = IllegalStateException.class)
155 public void testNegativeWriteWithTransactionReady() throws Exception {
158 final ActorRef shard =
159 getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
161 ShardTransaction.props(store.newWriteOnlyTransaction(), shard,
162 TestModel.createTestContext());
164 final TestActorRef<ShardTransaction> subject = TestActorRef
165 .create(getSystem(), props,
166 "testNegativeWriteWithTransactionReady");
168 ShardTransactionMessages.ReadyTransaction readyTransaction =
169 ShardTransactionMessages.ReadyTransaction.newBuilder().build();
171 Future<Object> future =
172 akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
173 assertTrue(future.isCompleted());
174 Await.result(future, Duration.Zero());
176 ShardTransactionMessages.WriteData writeData =
177 ShardTransactionMessages.WriteData.newBuilder()
178 .setInstanceIdentifierPathArguments(
179 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
180 .build()).setNormalizedNode(
181 NormalizedNodeMessages.Node.newBuilder().build()
185 future = akka.pattern.Patterns.ask(subject, writeData, 3000);
186 assertTrue(future.isCompleted());
187 Await.result(future, Duration.Zero());
193 @Test(expected = IllegalStateException.class)
194 public void testNegativeReadWriteWithTransactionReady() throws Exception {
197 final ActorRef shard =
198 getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
200 ShardTransaction.props(store.newReadWriteTransaction(), shard,
201 TestModel.createTestContext());
203 final TestActorRef<ShardTransaction> subject = TestActorRef
204 .create(getSystem(), props,
205 "testNegativeReadWriteWithTransactionReady");
207 ShardTransactionMessages.ReadyTransaction readyTransaction =
208 ShardTransactionMessages.ReadyTransaction.newBuilder().build();
210 Future<Object> future =
211 akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
212 assertTrue(future.isCompleted());
213 Await.result(future, Duration.Zero());
215 ShardTransactionMessages.WriteData writeData =
216 ShardTransactionMessages.WriteData.newBuilder()
217 .setInstanceIdentifierPathArguments(
218 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
219 .build()).setNormalizedNode(
220 NormalizedNodeMessages.Node.newBuilder().build()
224 future = akka.pattern.Patterns.ask(subject, writeData, 3000);
225 assertTrue(future.isCompleted());
226 Await.result(future, Duration.Zero());
231 @Test(expected = IllegalStateException.class)
232 public void testNegativeMergeTransactionReady() throws Exception {
235 final ActorRef shard =
236 getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
238 ShardTransaction.props(store.newReadWriteTransaction(), shard,
239 TestModel.createTestContext());
241 final TestActorRef<ShardTransaction> subject = TestActorRef
242 .create(getSystem(), props, "testNegativeMergeTransactionReady");
244 ShardTransactionMessages.ReadyTransaction readyTransaction =
245 ShardTransactionMessages.ReadyTransaction.newBuilder().build();
247 Future<Object> future =
248 akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
249 assertTrue(future.isCompleted());
250 Await.result(future, Duration.Zero());
252 ShardTransactionMessages.MergeData mergeData =
253 ShardTransactionMessages.MergeData.newBuilder()
254 .setInstanceIdentifierPathArguments(
255 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
256 .build()).setNormalizedNode(
257 NormalizedNodeMessages.Node.newBuilder().build()
261 future = akka.pattern.Patterns.ask(subject, mergeData, 3000);
262 assertTrue(future.isCompleted());
263 Await.result(future, Duration.Zero());
269 @Test(expected = IllegalStateException.class)
270 public void testNegativeDeleteDataWhenTransactionReady() throws Exception {
273 final ActorRef shard =
274 getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
276 ShardTransaction.props(store.newReadWriteTransaction(), shard,
277 TestModel.createTestContext());
279 final TestActorRef<ShardTransaction> subject = TestActorRef
280 .create(getSystem(), props,
281 "testNegativeDeleteDataWhenTransactionReady");
283 ShardTransactionMessages.ReadyTransaction readyTransaction =
284 ShardTransactionMessages.ReadyTransaction.newBuilder().build();
286 Future<Object> future =
287 akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
288 assertTrue(future.isCompleted());
289 Await.result(future, Duration.Zero());
291 ShardTransactionMessages.DeleteData deleteData =
292 ShardTransactionMessages.DeleteData.newBuilder()
293 .setInstanceIdentifierPathArguments(
294 NormalizedNodeMessages.InstanceIdentifier.newBuilder()
297 future = akka.pattern.Patterns.ask(subject, deleteData, 3000);
298 assertTrue(future.isCompleted());
299 Await.result(future, Duration.Zero());