Merge "Small fix to xsql dependencies"
[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
17 import com.google.common.util.concurrent.ListeningExecutorService;
18 import com.google.common.util.concurrent.MoreExecutors;
19
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
23 import org.opendaylight.controller.cluster.datastore.node.utils.serialization.NormalizedNodeSerializer;
24 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
25 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
26 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
27 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
28 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32
33 import scala.concurrent.Await;
34 import scala.concurrent.Future;
35 import scala.concurrent.duration.Duration;
36
37 import java.util.Collections;
38 import java.util.concurrent.TimeUnit;
39
40 /**
41  * Covers negative test cases
42  *
43  * @author Basheeruddin Ahmed <syedbahm@cisco.com>
44  */
45 public class ShardTransactionFailureTest extends AbstractActorTest {
46     private static ListeningExecutorService storeExecutor =
47         MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
48
49     private static final InMemoryDOMDataStore store =
50         new InMemoryDOMDataStore("OPER", storeExecutor,
51             MoreExecutors.sameThreadExecutor());
52
53     private static final SchemaContext testSchemaContext =
54         TestModel.createTestContext();
55
56     private static final ShardIdentifier SHARD_IDENTIFIER =
57         ShardIdentifier.builder().memberName("member-1")
58             .shardName("inventory").type("operational").build();
59
60     private final ShardContext shardContext = new ShardContext();
61
62     @BeforeClass
63     public static void staticSetup() {
64         store.onGlobalContextUpdated(testSchemaContext);
65     }
66
67     @Test(expected = ReadFailedException.class)
68     public void testNegativeReadWithReadOnlyTransactionClosed()
69         throws Throwable {
70
71         final ActorRef shard =
72             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP, new ShardContext()));
73         final Props props = ShardTransaction.props(store.newReadOnlyTransaction(), shard,
74                 testSchemaContext, shardContext);
75
76         final TestActorRef<ShardTransaction> subject = TestActorRef
77             .create(getSystem(), props,
78                 "testNegativeReadWithReadOnlyTransactionClosed");
79
80         ShardTransactionMessages.ReadData readData =
81             ShardTransactionMessages.ReadData.newBuilder()
82                 .setInstanceIdentifierPathArguments(
83                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
84                         .build()
85                 ).build();
86         Future<Object> future =
87             akka.pattern.Patterns.ask(subject, readData, 3000);
88         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
89
90         subject.underlyingActor().getDOMStoreTransaction().close();
91
92         future = akka.pattern.Patterns.ask(subject, readData, 3000);
93         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
94     }
95
96
97     @Test(expected = ReadFailedException.class)
98     public void testNegativeReadWithReadWriteTransactionClosed()
99         throws Throwable {
100
101         final ActorRef shard =
102             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP, new ShardContext()));
103         final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
104                 testSchemaContext, shardContext);
105
106         final TestActorRef<ShardTransaction> subject = TestActorRef
107             .create(getSystem(), props,
108                 "testNegativeReadWithReadWriteTransactionClosed");
109
110         ShardTransactionMessages.ReadData readData =
111             ShardTransactionMessages.ReadData.newBuilder()
112                 .setInstanceIdentifierPathArguments(
113                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
114                         .build()
115                 ).build();
116
117         Future<Object> future =
118             akka.pattern.Patterns.ask(subject, readData, 3000);
119         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
120
121         subject.underlyingActor().getDOMStoreTransaction().close();
122
123         future = akka.pattern.Patterns.ask(subject, readData, 3000);
124         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
125     }
126
127     @Test(expected = ReadFailedException.class)
128     public void testNegativeExistsWithReadWriteTransactionClosed()
129         throws Throwable {
130
131         final ActorRef shard =
132             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP, new ShardContext()));
133         final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
134                 testSchemaContext, shardContext);
135
136         final TestActorRef<ShardTransaction> subject = TestActorRef
137             .create(getSystem(), props,
138                 "testNegativeExistsWithReadWriteTransactionClosed");
139
140         ShardTransactionMessages.DataExists dataExists =
141             ShardTransactionMessages.DataExists.newBuilder()
142                 .setInstanceIdentifierPathArguments(
143                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
144                         .build()
145                 ).build();
146
147         Future<Object> future =
148             akka.pattern.Patterns.ask(subject, dataExists, 3000);
149         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
150
151         subject.underlyingActor().getDOMStoreTransaction().close();
152
153         future = akka.pattern.Patterns.ask(subject, dataExists, 3000);
154         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
155     }
156
157     @Test(expected = IllegalStateException.class)
158     public void testNegativeWriteWithTransactionReady() throws Exception {
159
160
161         final ActorRef shard =
162             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP, new ShardContext()));
163         final Props props = ShardTransaction.props(store.newWriteOnlyTransaction(), shard,
164                 testSchemaContext, shardContext);
165
166         final TestActorRef<ShardTransaction> subject = TestActorRef
167             .create(getSystem(), props,
168                 "testNegativeWriteWithTransactionReady");
169
170         ShardTransactionMessages.ReadyTransaction readyTransaction =
171             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
172
173         Future<Object> future =
174             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
175         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
176
177         ShardTransactionMessages.WriteData writeData =
178             ShardTransactionMessages.WriteData.newBuilder()
179                 .setInstanceIdentifierPathArguments(
180                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
181                         .build()).setNormalizedNode(
182                 buildNormalizedNode()
183
184             ).build();
185
186         future = akka.pattern.Patterns.ask(subject, writeData, 3000);
187         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
188     }
189
190     @Test(expected = IllegalStateException.class)
191     public void testNegativeReadWriteWithTransactionReady() throws Exception {
192
193
194         final ActorRef shard =
195             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP, new ShardContext()));
196         final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
197                 testSchemaContext, shardContext);
198
199         final TestActorRef<ShardTransaction> subject = TestActorRef
200             .create(getSystem(), props,
201                 "testNegativeReadWriteWithTransactionReady");
202
203         ShardTransactionMessages.ReadyTransaction readyTransaction =
204             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
205
206         Future<Object> future =
207             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
208         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
209
210         ShardTransactionMessages.WriteData writeData =
211             ShardTransactionMessages.WriteData.newBuilder()
212                 .setInstanceIdentifierPathArguments(
213                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
214                         .build()
215                 )
216                 .setNormalizedNode(buildNormalizedNode())
217                 .build();
218
219         future = akka.pattern.Patterns.ask(subject, writeData, 3000);
220         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
221     }
222
223     private NormalizedNodeMessages.Node buildNormalizedNode() {
224         return NormalizedNodeSerializer
225             .serialize(Builders.containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).build());
226     }
227
228     @Test(expected = IllegalStateException.class)
229     public void testNegativeMergeTransactionReady() throws Exception {
230
231
232         final ActorRef shard =
233             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP, new ShardContext()));
234         final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
235                 testSchemaContext, shardContext);
236
237         final TestActorRef<ShardTransaction> subject = TestActorRef
238             .create(getSystem(), props, "testNegativeMergeTransactionReady");
239
240         ShardTransactionMessages.ReadyTransaction readyTransaction =
241             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
242
243         Future<Object> future =
244             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
245         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
246
247         ShardTransactionMessages.MergeData mergeData =
248             ShardTransactionMessages.MergeData.newBuilder()
249                 .setInstanceIdentifierPathArguments(
250                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
251                         .build()).setNormalizedNode(
252                 buildNormalizedNode()
253
254             ).build();
255
256         future = akka.pattern.Patterns.ask(subject, mergeData, 3000);
257         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
258     }
259
260
261     @Test(expected = IllegalStateException.class)
262     public void testNegativeDeleteDataWhenTransactionReady() throws Exception {
263
264
265         final ActorRef shard =
266             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP, new ShardContext()));
267         final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
268                 testSchemaContext, shardContext);
269
270         final TestActorRef<ShardTransaction> subject = TestActorRef
271             .create(getSystem(), props,
272                 "testNegativeDeleteDataWhenTransactionReady");
273
274         ShardTransactionMessages.ReadyTransaction readyTransaction =
275             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
276
277         Future<Object> future =
278             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
279         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
280
281         ShardTransactionMessages.DeleteData deleteData =
282             ShardTransactionMessages.DeleteData.newBuilder()
283                 .setInstanceIdentifierPathArguments(
284                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
285                         .build()).build();
286
287         future = akka.pattern.Patterns.ask(subject, deleteData, 3000);
288         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
289     }
290 }