6d057a4dbeeffdfc7892a798f62fb539b7ffc4fc
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / TransactionProxyTest.java
1 package org.opendaylight.controller.cluster.datastore;
2
3 import akka.actor.ActorRef;
4 import akka.actor.Props;
5 import com.google.common.base.Optional;
6 import com.google.common.util.concurrent.ListenableFuture;
7 import junit.framework.Assert;
8 import org.junit.Test;
9 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
10 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
11 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
12 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
13 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
14 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
15 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
16 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
17 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
18 import org.opendaylight.controller.cluster.datastore.utils.MessageCollectorActor;
19 import org.opendaylight.controller.cluster.datastore.utils.MockActorContext;
20 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
24
25 import java.util.List;
26
27 public class TransactionProxyTest extends AbstractActorTest {
28
29     @Test
30     public void testRead() throws Exception {
31         final Props props = Props.create(DoNothingActor.class);
32         final ActorRef actorRef = getSystem().actorOf(props);
33
34         final MockActorContext actorContext = new MockActorContext(this.getSystem());
35         actorContext.setExecuteShardOperationResponse(new CreateTransactionReply(actorRef.path()));
36         actorContext.setExecuteRemoteOperationResponse("message");
37
38         TransactionProxy transactionProxy =
39             new TransactionProxy(actorContext,
40                 TransactionProxy.TransactionType.READ_ONLY);
41
42
43         ListenableFuture<Optional<NormalizedNode<?, ?>>> read =
44             transactionProxy.read(TestModel.TEST_PATH);
45
46         Optional<NormalizedNode<?, ?>> normalizedNodeOptional = read.get();
47
48         Assert.assertFalse(normalizedNodeOptional.isPresent());
49
50         actorContext.setExecuteRemoteOperationResponse(new ReadDataReply(
51             ImmutableNodes.containerNode(TestModel.TEST_QNAME)));
52
53         read = transactionProxy.read(TestModel.TEST_PATH);
54
55         normalizedNodeOptional = read.get();
56
57         Assert.assertTrue(normalizedNodeOptional.isPresent());
58     }
59
60     @Test
61     public void testWrite() throws Exception {
62         final Props props = Props.create(MessageCollectorActor.class);
63         final ActorRef actorRef = getSystem().actorOf(props);
64
65         final MockActorContext actorContext = new MockActorContext(this.getSystem());
66         actorContext.setExecuteShardOperationResponse(new CreateTransactionReply(actorRef.path()));
67         actorContext.setExecuteRemoteOperationResponse("message");
68
69         TransactionProxy transactionProxy =
70             new TransactionProxy(actorContext,
71                 TransactionProxy.TransactionType.READ_ONLY);
72
73         transactionProxy.write(TestModel.TEST_PATH,
74             ImmutableNodes.containerNode(TestModel.NAME_QNAME));
75
76         ActorContext testContext = new ActorContext(getSystem(), getSystem().actorOf(Props.create(DoNothingActor.class)));
77         Object messages = testContext
78             .executeLocalOperation(actorRef, "messages",
79                 ActorContext.ASK_DURATION);
80
81         Assert.assertNotNull(messages);
82
83         Assert.assertTrue(messages instanceof List);
84
85         List<Object> listMessages = (List<Object>) messages;
86
87         Assert.assertEquals(1, listMessages.size());
88
89         Assert.assertTrue(listMessages.get(0) instanceof WriteData);
90     }
91
92     @Test
93     public void testMerge() throws Exception {
94         final Props props = Props.create(MessageCollectorActor.class);
95         final ActorRef actorRef = getSystem().actorOf(props);
96
97         final MockActorContext actorContext = new MockActorContext(this.getSystem());
98         actorContext.setExecuteShardOperationResponse(new CreateTransactionReply(actorRef.path()));
99         actorContext.setExecuteRemoteOperationResponse("message");
100
101         TransactionProxy transactionProxy =
102             new TransactionProxy(actorContext,
103                 TransactionProxy.TransactionType.READ_ONLY);
104
105         transactionProxy.merge(TestModel.TEST_PATH,
106             ImmutableNodes.containerNode(TestModel.NAME_QNAME));
107
108         ActorContext testContext = new ActorContext(getSystem(), getSystem().actorOf(Props.create(DoNothingActor.class)));
109         Object messages = testContext
110             .executeLocalOperation(actorRef, "messages",
111                 ActorContext.ASK_DURATION);
112
113         Assert.assertNotNull(messages);
114
115         Assert.assertTrue(messages instanceof List);
116
117         List<Object> listMessages = (List<Object>) messages;
118
119         Assert.assertEquals(1, listMessages.size());
120
121         Assert.assertTrue(listMessages.get(0) instanceof MergeData);
122     }
123
124     @Test
125     public void testDelete() throws Exception {
126         final Props props = Props.create(MessageCollectorActor.class);
127         final ActorRef actorRef = getSystem().actorOf(props);
128
129         final MockActorContext actorContext = new MockActorContext(this.getSystem());
130         actorContext.setExecuteShardOperationResponse(new CreateTransactionReply(actorRef.path()));
131         actorContext.setExecuteRemoteOperationResponse("message");
132
133         TransactionProxy transactionProxy =
134             new TransactionProxy(actorContext,
135                 TransactionProxy.TransactionType.READ_ONLY);
136
137         transactionProxy.delete(TestModel.TEST_PATH);
138
139         ActorContext testContext = new ActorContext(getSystem(), getSystem().actorOf(Props.create(DoNothingActor.class)));
140         Object messages = testContext
141             .executeLocalOperation(actorRef, "messages",
142                 ActorContext.ASK_DURATION);
143
144         Assert.assertNotNull(messages);
145
146         Assert.assertTrue(messages instanceof List);
147
148         List<Object> listMessages = (List<Object>) messages;
149
150         Assert.assertEquals(1, listMessages.size());
151
152         Assert.assertTrue(listMessages.get(0) instanceof DeleteData);
153     }
154
155     @Test
156     public void testReady() throws Exception {
157         final Props props = Props.create(DoNothingActor.class);
158         final ActorRef doNothingActorRef = getSystem().actorOf(props);
159
160         final MockActorContext actorContext = new MockActorContext(this.getSystem());
161         actorContext.setExecuteShardOperationResponse(new CreateTransactionReply(doNothingActorRef.path()));
162         actorContext.setExecuteRemoteOperationResponse(new ReadyTransactionReply(doNothingActorRef.path()));
163
164         TransactionProxy transactionProxy =
165             new TransactionProxy(actorContext,
166                 TransactionProxy.TransactionType.READ_ONLY);
167
168
169         DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
170
171         Assert.assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
172
173         ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
174
175         Assert.assertTrue("No cohort paths returned", proxy.getCohortPaths().size() > 0);
176
177     }
178
179     @Test
180     public void testGetIdentifier(){
181         final Props props = Props.create(DoNothingActor.class);
182         final ActorRef doNothingActorRef = getSystem().actorOf(props);
183
184         final MockActorContext actorContext = new MockActorContext(this.getSystem());
185         actorContext.setExecuteShardOperationResponse(
186             new CreateTransactionReply(doNothingActorRef.path()));
187
188         TransactionProxy transactionProxy =
189             new TransactionProxy(actorContext,
190                 TransactionProxy.TransactionType.READ_ONLY);
191
192         Assert.assertNotNull(transactionProxy.getIdentifier());
193     }
194
195     @Test
196     public void testClose(){
197         final Props props = Props.create(MessageCollectorActor.class);
198         final ActorRef actorRef = getSystem().actorOf(props);
199
200         final MockActorContext actorContext = new MockActorContext(this.getSystem());
201         actorContext.setExecuteShardOperationResponse(new CreateTransactionReply(actorRef.path()));
202         actorContext.setExecuteRemoteOperationResponse("message");
203
204         TransactionProxy transactionProxy =
205             new TransactionProxy(actorContext,
206                 TransactionProxy.TransactionType.READ_ONLY);
207
208         transactionProxy.close();
209
210         ActorContext testContext = new ActorContext(getSystem(), getSystem().actorOf(Props.create(DoNothingActor.class)));
211         Object messages = testContext
212             .executeLocalOperation(actorRef, "messages",
213                 ActorContext.ASK_DURATION);
214
215         Assert.assertNotNull(messages);
216
217         Assert.assertTrue(messages instanceof List);
218
219         List<Object> listMessages = (List<Object>) messages;
220
221         Assert.assertEquals(1, listMessages.size());
222
223         Assert.assertTrue(listMessages.get(0) instanceof CloseTransaction);
224     }
225 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.