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