Merge "Fixed remove rpc api in rpc registry"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardTransactionFailureTest.java
1 /*
2  *
3  *  Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
4  *
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
8  *
9  */
10
11 package org.opendaylight.controller.cluster.datastore;
12
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;
30
31 import java.util.Collections;
32
33 import static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.assertTrue;
35
36 /**
37  * Covers negative test cases
38  * @author Basheeruddin Ahmed <syedbahm@cisco.com>
39  */
40 public class ShardTransactionFailureTest extends AbstractActorTest {
41     private static ListeningExecutorService storeExecutor =
42         MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
43
44     private static final InMemoryDOMDataStore store =
45         new InMemoryDOMDataStore("OPER", storeExecutor,
46             MoreExecutors.sameThreadExecutor());
47
48     private static final SchemaContext testSchemaContext =
49         TestModel.createTestContext();
50
51     static {
52         store.onGlobalContextUpdated(testSchemaContext);
53     }
54
55
56     @Test
57     public void testNegativePerformingWriteOperationOnReadTransaction()
58         throws Exception {
59         try {
60
61             final ActorRef
62                 shard = getSystem()
63                 .actorOf(Shard.props("config", Collections.EMPTY_MAP));
64             final Props props =
65                 ShardTransaction
66                     .props(store.newReadOnlyTransaction(), shard, TestModel
67                         .createTestContext());
68             final TestActorRef subject = TestActorRef.apply(props, getSystem());
69
70             subject
71                 .receive(new DeleteData(TestModel.TEST_PATH).toSerializable(),
72                     ActorRef.noSender());
73             Assert.assertFalse(true);
74
75
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"));
81         }
82     }
83
84     @Test(expected = ReadFailedException.class)
85     public void testNegativeReadWithReadOnlyTransactionClosed()
86         throws Throwable {
87
88         final ActorRef shard =
89             getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
90         final Props props =
91             ShardTransaction.props(store.newReadOnlyTransaction(), shard,
92                 TestModel.createTestContext());
93
94         final TestActorRef<ShardTransaction> subject = TestActorRef
95             .create(getSystem(), props,
96                 "testNegativeReadWithReadOnlyTransactionClosed");
97
98         ShardTransactionMessages.ReadData readData =
99             ShardTransactionMessages.ReadData.newBuilder()
100                 .setInstanceIdentifierPathArguments(
101                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
102                         .build()
103                 ).build();
104         Future<Object> future =
105             akka.pattern.Patterns.ask(subject, readData, 3000);
106         assertTrue(future.isCompleted());
107         Await.result(future, Duration.Zero());
108
109         ((ShardReadTransaction) subject.underlyingActor())
110             .forUnitTestOnlyExplicitTransactionClose();
111
112         future = akka.pattern.Patterns.ask(subject, readData, 3000);
113         Await.result(future, Duration.Zero());
114
115
116     }
117
118
119     @Test(expected = ReadFailedException.class)
120     public void testNegativeReadWithReadWriteOnlyTransactionClosed()
121         throws Throwable {
122
123         final ActorRef shard =
124             getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
125         final Props props =
126             ShardTransaction.props(store.newReadWriteTransaction(), shard,
127                 TestModel.createTestContext());
128
129         final TestActorRef<ShardTransaction> subject = TestActorRef
130             .create(getSystem(), props,
131                 "testNegativeReadWithReadWriteOnlyTransactionClosed");
132
133         ShardTransactionMessages.ReadData readData =
134             ShardTransactionMessages.ReadData.newBuilder()
135                 .setInstanceIdentifierPathArguments(
136                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
137                         .build()
138                 ).build();
139         Future<Object> future =
140             akka.pattern.Patterns.ask(subject, readData, 3000);
141         assertTrue(future.isCompleted());
142         Await.result(future, Duration.Zero());
143
144         ((ShardReadWriteTransaction) subject.underlyingActor())
145             .forUnitTestOnlyExplicitTransactionClose();
146
147         future = akka.pattern.Patterns.ask(subject, readData, 3000);
148         Await.result(future, Duration.Zero());
149
150
151     }
152
153
154     @Test(expected = IllegalStateException.class)
155     public void testNegativeWriteWithTransactionReady() throws Exception {
156
157
158         final ActorRef shard =
159             getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
160         final Props props =
161             ShardTransaction.props(store.newWriteOnlyTransaction(), shard,
162                 TestModel.createTestContext());
163
164         final TestActorRef<ShardTransaction> subject = TestActorRef
165             .create(getSystem(), props,
166                 "testNegativeWriteWithTransactionReady");
167
168         ShardTransactionMessages.ReadyTransaction readyTransaction =
169             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
170
171         Future<Object> future =
172             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
173         assertTrue(future.isCompleted());
174         Await.result(future, Duration.Zero());
175
176         ShardTransactionMessages.WriteData writeData =
177             ShardTransactionMessages.WriteData.newBuilder()
178                 .setInstanceIdentifierPathArguments(
179                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
180                         .build()).setNormalizedNode(
181                 NormalizedNodeMessages.Node.newBuilder().build()
182
183             ).build();
184
185         future = akka.pattern.Patterns.ask(subject, writeData, 3000);
186         assertTrue(future.isCompleted());
187         Await.result(future, Duration.Zero());
188
189
190     }
191
192
193     @Test(expected = IllegalStateException.class)
194     public void testNegativeReadWriteWithTransactionReady() throws Exception {
195
196
197         final ActorRef shard =
198             getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
199         final Props props =
200             ShardTransaction.props(store.newReadWriteTransaction(), shard,
201                 TestModel.createTestContext());
202
203         final TestActorRef<ShardTransaction> subject = TestActorRef
204             .create(getSystem(), props,
205                 "testNegativeReadWriteWithTransactionReady");
206
207         ShardTransactionMessages.ReadyTransaction readyTransaction =
208             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
209
210         Future<Object> future =
211             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
212         assertTrue(future.isCompleted());
213         Await.result(future, Duration.Zero());
214
215         ShardTransactionMessages.WriteData writeData =
216             ShardTransactionMessages.WriteData.newBuilder()
217                 .setInstanceIdentifierPathArguments(
218                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
219                         .build()).setNormalizedNode(
220                 NormalizedNodeMessages.Node.newBuilder().build()
221
222             ).build();
223
224         future = akka.pattern.Patterns.ask(subject, writeData, 3000);
225         assertTrue(future.isCompleted());
226         Await.result(future, Duration.Zero());
227
228
229     }
230
231     @Test(expected = IllegalStateException.class)
232     public void testNegativeMergeTransactionReady() throws Exception {
233
234
235         final ActorRef shard =
236             getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
237         final Props props =
238             ShardTransaction.props(store.newReadWriteTransaction(), shard,
239                 TestModel.createTestContext());
240
241         final TestActorRef<ShardTransaction> subject = TestActorRef
242             .create(getSystem(), props, "testNegativeMergeTransactionReady");
243
244         ShardTransactionMessages.ReadyTransaction readyTransaction =
245             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
246
247         Future<Object> future =
248             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
249         assertTrue(future.isCompleted());
250         Await.result(future, Duration.Zero());
251
252         ShardTransactionMessages.MergeData mergeData =
253             ShardTransactionMessages.MergeData.newBuilder()
254                 .setInstanceIdentifierPathArguments(
255                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
256                         .build()).setNormalizedNode(
257                 NormalizedNodeMessages.Node.newBuilder().build()
258
259             ).build();
260
261         future = akka.pattern.Patterns.ask(subject, mergeData, 3000);
262         assertTrue(future.isCompleted());
263         Await.result(future, Duration.Zero());
264
265
266     }
267
268
269     @Test(expected = IllegalStateException.class)
270     public void testNegativeDeleteDataWhenTransactionReady() throws Exception {
271
272
273         final ActorRef shard =
274             getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
275         final Props props =
276             ShardTransaction.props(store.newReadWriteTransaction(), shard,
277                 TestModel.createTestContext());
278
279         final TestActorRef<ShardTransaction> subject = TestActorRef
280             .create(getSystem(), props,
281                 "testNegativeDeleteDataWhenTransactionReady");
282
283         ShardTransactionMessages.ReadyTransaction readyTransaction =
284             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
285
286         Future<Object> future =
287             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
288         assertTrue(future.isCompleted());
289         Await.result(future, Duration.Zero());
290
291         ShardTransactionMessages.DeleteData deleteData =
292             ShardTransactionMessages.DeleteData.newBuilder()
293                 .setInstanceIdentifierPathArguments(
294                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
295                         .build()).build();
296
297         future = akka.pattern.Patterns.ask(subject, deleteData, 3000);
298         assertTrue(future.isCompleted());
299         Await.result(future, Duration.Zero());
300
301
302     }
303 }