1 package org.opendaylight.controller.cluster.datastore;
3 import akka.actor.ActorRef;
4 import akka.actor.Props;
5 import akka.actor.Terminated;
6 import akka.testkit.JavaTestKit;
7 import akka.testkit.TestActorRef;
8 import com.google.common.util.concurrent.ListeningExecutorService;
9 import com.google.common.util.concurrent.MoreExecutors;
10 import org.junit.Assert;
11 import org.junit.Test;
12 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
13 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionReply;
14 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
15 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
16 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
17 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
18 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
19 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
20 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
21 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
22 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
23 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
24 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
25 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
26 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
27 import org.opendaylight.controller.cluster.datastore.modification.Modification;
28 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
29 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
30 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import java.util.Collections;
37 import static org.junit.Assert.assertEquals;
38 import static org.junit.Assert.assertTrue;
40 public class ShardTransactionTest extends AbstractActorTest {
41 private static ListeningExecutorService storeExecutor =
42 MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
44 private static final InMemoryDOMDataStore store =
45 new InMemoryDOMDataStore("OPER", storeExecutor, MoreExecutors.sameThreadExecutor());
47 private static final SchemaContext testSchemaContext = TestModel.createTestContext();
50 store.onGlobalContextUpdated(testSchemaContext);
54 public void testOnReceiveReadData() throws Exception {
55 new JavaTestKit(getSystem()) {{
56 final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
58 ShardTransaction.props(store.newReadOnlyTransaction(), shard, testSchemaContext);
59 final ActorRef subject = getSystem().actorOf(props, "testReadData");
61 new Within(duration("1 seconds")) {
63 protected void run() {
66 new ReadData(YangInstanceIdentifier.builder().build()).toSerializable(),
69 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
70 // do not put code outside this method, will run afterwards
72 protected String match(Object in) {
73 if (in.getClass().equals(ReadDataReply.SERIALIZABLE_CLASS)) {
74 if (ReadDataReply.fromSerializable(testSchemaContext,YangInstanceIdentifier.builder().build(), in)
75 .getNormalizedNode()!= null) {
83 }.get(); // this extracts the received message
85 assertEquals("match", out);
96 public void testOnReceiveReadDataWhenDataNotFound() throws Exception {
97 new JavaTestKit(getSystem()) {{
98 final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
100 ShardTransaction.props( store.newReadOnlyTransaction(), shard, testSchemaContext);
101 final ActorRef subject = getSystem().actorOf(props, "testReadDataWhenDataNotFound");
103 new Within(duration("1 seconds")) {
105 protected void run() {
108 new ReadData(TestModel.TEST_PATH).toSerializable(),
111 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
112 // do not put code outside this method, will run afterwards
114 protected String match(Object in) {
115 if (in.getClass().equals(ReadDataReply.SERIALIZABLE_CLASS)) {
116 if (ReadDataReply.fromSerializable(testSchemaContext,TestModel.TEST_PATH, in)
126 }.get(); // this extracts the received message
128 assertEquals("match", out);
138 private void assertModification(final ActorRef subject,
139 final Class<? extends Modification> modificationType) {
140 new JavaTestKit(getSystem()) {{
141 new Within(duration("1 seconds")) {
143 protected void run() {
145 .tell(new ShardTransaction.GetCompositedModification(),
148 final CompositeModification compositeModification =
149 new ExpectMsg<CompositeModification>(duration("1 seconds"), "match hint") {
150 // do not put code outside this method, will run afterwards
152 protected CompositeModification match(Object in) {
153 if (in instanceof ShardTransaction.GetCompositeModificationReply) {
154 return ((ShardTransaction.GetCompositeModificationReply) in)
160 }.get(); // this extracts the received message
163 compositeModification.getModifications().size() == 1);
164 assertEquals(modificationType,
165 compositeModification.getModifications().get(0)
174 public void testOnReceiveWriteData() throws Exception {
175 new JavaTestKit(getSystem()) {{
176 final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
178 ShardTransaction.props(store.newWriteOnlyTransaction(), shard, TestModel.createTestContext());
179 final ActorRef subject =
180 getSystem().actorOf(props, "testWriteData");
182 new Within(duration("1 seconds")) {
184 protected void run() {
186 subject.tell(new WriteData(TestModel.TEST_PATH,
187 ImmutableNodes.containerNode(TestModel.TEST_QNAME), TestModel.createTestContext()).toSerializable(),
190 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
191 // do not put code outside this method, will run afterwards
193 protected String match(Object in) {
194 if (in.getClass().equals(WriteDataReply.SERIALIZABLE_CLASS)) {
200 }.get(); // this extracts the received message
202 assertEquals("match", out);
204 assertModification(subject, WriteModification.class);
214 public void testOnReceiveMergeData() throws Exception {
215 new JavaTestKit(getSystem()) {{
216 final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
218 ShardTransaction.props(store.newReadWriteTransaction(), shard, testSchemaContext);
219 final ActorRef subject =
220 getSystem().actorOf(props, "testMergeData");
222 new Within(duration("1 seconds")) {
224 protected void run() {
226 subject.tell(new MergeData(TestModel.TEST_PATH,
227 ImmutableNodes.containerNode(TestModel.TEST_QNAME), testSchemaContext).toSerializable(),
230 final String out = new ExpectMsg<String>(duration("500 milliseconds"), "match hint") {
231 // do not put code outside this method, will run afterwards
233 protected String match(Object in) {
234 if (in.getClass().equals(MergeDataReply.SERIALIZABLE_CLASS)) {
240 }.get(); // this extracts the received message
242 assertEquals("match", out);
244 assertModification(subject, MergeModification.class);
255 public void testOnReceiveDeleteData() throws Exception {
256 new JavaTestKit(getSystem()) {{
257 final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
259 ShardTransaction.props( store.newWriteOnlyTransaction(), shard, TestModel.createTestContext());
260 final ActorRef subject =
261 getSystem().actorOf(props, "testDeleteData");
263 new Within(duration("1 seconds")) {
265 protected void run() {
267 subject.tell(new DeleteData(TestModel.TEST_PATH).toSerializable(), getRef());
269 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
270 // do not put code outside this method, will run afterwards
272 protected String match(Object in) {
273 if (in.getClass().equals(DeleteDataReply.SERIALIZABLE_CLASS)) {
279 }.get(); // this extracts the received message
281 assertEquals("match", out);
283 assertModification(subject, DeleteModification.class);
294 public void testOnReceiveReadyTransaction() throws Exception {
295 new JavaTestKit(getSystem()) {{
296 final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
298 ShardTransaction.props( store.newReadWriteTransaction(), shard, TestModel.createTestContext());
299 final ActorRef subject =
300 getSystem().actorOf(props, "testReadyTransaction");
302 new Within(duration("1 seconds")) {
304 protected void run() {
306 subject.tell(new ReadyTransaction().toSerializable(), getRef());
308 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
309 // do not put code outside this method, will run afterwards
311 protected String match(Object in) {
312 if (in.getClass().equals(ReadyTransactionReply.SERIALIZABLE_CLASS)) {
318 }.get(); // this extracts the received message
320 assertEquals("match", out);
332 public void testOnReceiveCloseTransaction() throws Exception {
333 new JavaTestKit(getSystem()) {{
334 final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
336 ShardTransaction.props(store.newReadWriteTransaction(), shard, TestModel.createTestContext());
337 final ActorRef subject =
338 getSystem().actorOf(props, "testCloseTransaction");
342 new Within(duration("2 seconds")) {
344 protected void run() {
346 subject.tell(new CloseTransaction().toSerializable(), getRef());
348 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
349 // do not put code outside this method, will run afterwards
351 protected String match(Object in) {
352 if (in.getClass().equals(CloseTransactionReply.SERIALIZABLE_CLASS)) {
358 }.get(); // this extracts the received message
360 assertEquals("match", out);
362 final String termination = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
363 // do not put code outside this method, will run afterwards
365 protected String match(Object in) {
366 if (in instanceof Terminated) {
372 }.get(); // this extracts the received message
386 public void testNegativePerformingWriteOperationOnReadTransaction() throws Exception {
389 final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
391 ShardTransaction.props(store.newReadOnlyTransaction(), shard, TestModel.createTestContext());
392 final TestActorRef subject = TestActorRef.apply(props,getSystem());
394 subject.receive(new DeleteData(TestModel.TEST_PATH).toSerializable(), ActorRef.noSender());
395 Assert.assertFalse(true);
398 } catch (Exception cs) {
399 assertEquals(cs.getClass().getSimpleName(), Exception.class.getSimpleName());
400 assertTrue(cs.getMessage().startsWith("ShardTransaction:handleRecieve received an unknown message"));