8b70e00da923d92a806c59530c2111b5cfe87b7d
[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 testReadWhenANullIsReturned() throws Exception {
62         final Props props = Props.create(DoNothingActor.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
74         ListenableFuture<Optional<NormalizedNode<?, ?>>> read =
75             transactionProxy.read(TestModel.TEST_PATH);
76
77         Optional<NormalizedNode<?, ?>> normalizedNodeOptional = read.get();
78
79         Assert.assertFalse(normalizedNodeOptional.isPresent());
80
81         actorContext.setExecuteRemoteOperationResponse(new ReadDataReply(
82             null));
83
84         read = transactionProxy.read(TestModel.TEST_PATH);
85
86         normalizedNodeOptional = read.get();
87
88         Assert.assertFalse(normalizedNodeOptional.isPresent());
89     }
90
91     @Test
92     public void testWrite() throws Exception {
93         final Props props = Props.create(MessageCollectorActor.class);
94         final ActorRef actorRef = getSystem().actorOf(props);
95
96         final MockActorContext actorContext = new MockActorContext(this.getSystem());
97         actorContext.setExecuteShardOperationResponse(new CreateTransactionReply(actorRef.path()));
98         actorContext.setExecuteRemoteOperationResponse("message");
99
100         TransactionProxy transactionProxy =
101             new TransactionProxy(actorContext,
102                 TransactionProxy.TransactionType.READ_ONLY);
103
104         transactionProxy.write(TestModel.TEST_PATH,
105             ImmutableNodes.containerNode(TestModel.NAME_QNAME));
106
107         ActorContext testContext = new ActorContext(getSystem(), getSystem().actorOf(Props.create(DoNothingActor.class)));
108         Object messages = testContext
109             .executeLocalOperation(actorRef, "messages",
110                 ActorContext.ASK_DURATION);
111
112         Assert.assertNotNull(messages);
113
114         Assert.assertTrue(messages instanceof List);
115
116         List<Object> listMessages = (List<Object>) messages;
117
118         Assert.assertEquals(1, listMessages.size());
119
120         Assert.assertTrue(listMessages.get(0) instanceof WriteData);
121     }
122
123     @Test
124     public void testMerge() throws Exception {
125         final Props props = Props.create(MessageCollectorActor.class);
126         final ActorRef actorRef = getSystem().actorOf(props);
127
128         final MockActorContext actorContext = new MockActorContext(this.getSystem());
129         actorContext.setExecuteShardOperationResponse(new CreateTransactionReply(actorRef.path()));
130         actorContext.setExecuteRemoteOperationResponse("message");
131
132         TransactionProxy transactionProxy =
133             new TransactionProxy(actorContext,
134                 TransactionProxy.TransactionType.READ_ONLY);
135
136         transactionProxy.merge(TestModel.TEST_PATH,
137             ImmutableNodes.containerNode(TestModel.NAME_QNAME));
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 MergeData);
153     }
154
155     @Test
156     public void testDelete() throws Exception {
157         final Props props = Props.create(MessageCollectorActor.class);
158         final ActorRef actorRef = getSystem().actorOf(props);
159
160         final MockActorContext actorContext = new MockActorContext(this.getSystem());
161         actorContext.setExecuteShardOperationResponse(new CreateTransactionReply(actorRef.path()));
162         actorContext.setExecuteRemoteOperationResponse("message");
163
164         TransactionProxy transactionProxy =
165             new TransactionProxy(actorContext,
166                 TransactionProxy.TransactionType.READ_ONLY);
167
168         transactionProxy.delete(TestModel.TEST_PATH);
169
170         ActorContext testContext = new ActorContext(getSystem(), getSystem().actorOf(Props.create(DoNothingActor.class)));
171         Object messages = testContext
172             .executeLocalOperation(actorRef, "messages",
173                 ActorContext.ASK_DURATION);
174
175         Assert.assertNotNull(messages);
176
177         Assert.assertTrue(messages instanceof List);
178
179         List<Object> listMessages = (List<Object>) messages;
180
181         Assert.assertEquals(1, listMessages.size());
182
183         Assert.assertTrue(listMessages.get(0) instanceof DeleteData);
184     }
185
186     @Test
187     public void testReady() throws Exception {
188         final Props props = Props.create(DoNothingActor.class);
189         final ActorRef doNothingActorRef = getSystem().actorOf(props);
190
191         final MockActorContext actorContext = new MockActorContext(this.getSystem());
192         actorContext.setExecuteShardOperationResponse(new CreateTransactionReply(doNothingActorRef.path()));
193         actorContext.setExecuteRemoteOperationResponse(new ReadyTransactionReply(doNothingActorRef.path()));
194
195         TransactionProxy transactionProxy =
196             new TransactionProxy(actorContext,
197                 TransactionProxy.TransactionType.READ_ONLY);
198
199
200         DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
201
202         Assert.assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
203
204         ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
205
206         Assert.assertTrue("No cohort paths returned", proxy.getCohortPaths().size() > 0);
207
208     }
209
210     @Test
211     public void testGetIdentifier(){
212         final Props props = Props.create(DoNothingActor.class);
213         final ActorRef doNothingActorRef = getSystem().actorOf(props);
214
215         final MockActorContext actorContext = new MockActorContext(this.getSystem());
216         actorContext.setExecuteShardOperationResponse(
217             new CreateTransactionReply(doNothingActorRef.path()));
218
219         TransactionProxy transactionProxy =
220             new TransactionProxy(actorContext,
221                 TransactionProxy.TransactionType.READ_ONLY);
222
223         Assert.assertNotNull(transactionProxy.getIdentifier());
224     }
225
226     @Test
227     public void testClose(){
228         final Props props = Props.create(MessageCollectorActor.class);
229         final ActorRef actorRef = getSystem().actorOf(props);
230
231         final MockActorContext actorContext = new MockActorContext(this.getSystem());
232         actorContext.setExecuteShardOperationResponse(new CreateTransactionReply(actorRef.path()));
233         actorContext.setExecuteRemoteOperationResponse("message");
234
235         TransactionProxy transactionProxy =
236             new TransactionProxy(actorContext,
237                 TransactionProxy.TransactionType.READ_ONLY);
238
239         transactionProxy.close();
240
241         ActorContext testContext = new ActorContext(getSystem(), getSystem().actorOf(Props.create(DoNothingActor.class)));
242         Object messages = testContext
243             .executeLocalOperation(actorRef, "messages",
244                 ActorContext.ASK_DURATION);
245
246         Assert.assertNotNull(messages);
247
248         Assert.assertTrue(messages instanceof List);
249
250         List<Object> listMessages = (List<Object>) messages;
251
252         Assert.assertEquals(1, listMessages.size());
253
254         Assert.assertTrue(listMessages.get(0) instanceof CloseTransaction);
255     }
256 }

©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.