f646bed486a6ee3eb7b144df8f0d07d73c4a7bdf
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / databroker / actors / dds / RemoteProxyTransactionTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.databroker.actors.dds;
9
10 import static org.hamcrest.CoreMatchers.both;
11 import static org.hamcrest.CoreMatchers.hasItem;
12 import static org.hamcrest.CoreMatchers.isA;
13 import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.assertFutureEquals;
14
15 import akka.testkit.TestProbe;
16 import com.google.common.base.Optional;
17 import com.google.common.util.concurrent.CheckedFuture;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import java.util.List;
20 import org.junit.Assert;
21 import org.junit.Test;
22 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionRequest;
23 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionSuccess;
24 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionRequest;
25 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionRequestBuilder;
26 import org.opendaylight.controller.cluster.access.commands.PersistenceProtocol;
27 import org.opendaylight.controller.cluster.access.commands.ReadTransactionRequest;
28 import org.opendaylight.controller.cluster.access.commands.ReadTransactionSuccess;
29 import org.opendaylight.controller.cluster.access.commands.TransactionAbortRequest;
30 import org.opendaylight.controller.cluster.access.commands.TransactionCanCommitSuccess;
31 import org.opendaylight.controller.cluster.access.commands.TransactionCommitSuccess;
32 import org.opendaylight.controller.cluster.access.commands.TransactionDelete;
33 import org.opendaylight.controller.cluster.access.commands.TransactionDoCommitRequest;
34 import org.opendaylight.controller.cluster.access.commands.TransactionMerge;
35 import org.opendaylight.controller.cluster.access.commands.TransactionModification;
36 import org.opendaylight.controller.cluster.access.commands.TransactionPreCommitRequest;
37 import org.opendaylight.controller.cluster.access.commands.TransactionPreCommitSuccess;
38 import org.opendaylight.controller.cluster.access.commands.TransactionWrite;
39 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
40 import org.opendaylight.mdsal.common.api.ReadFailedException;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
44
45 public class RemoteProxyTransactionTest extends AbstractProxyTransactionTest<RemoteProxyTransaction> {
46
47     @Override
48     protected RemoteProxyTransaction createTransaction(final ProxyHistory parent, final TransactionIdentifier id,
49                                                        final DataTreeSnapshot snapshot) {
50         return new RemoteProxyTransaction(parent, TRANSACTION_ID, false, false);
51     }
52
53     @Override
54     @Test
55     public void testExists() throws Exception {
56         final TransactionTester<RemoteProxyTransaction> tester = getTester();
57         final CheckedFuture<Boolean, ReadFailedException> exists = transaction.exists(PATH_1);
58         final ExistsTransactionRequest req = tester.expectTransactionRequest(ExistsTransactionRequest.class);
59         final boolean existsResult = true;
60         tester.replySuccess(new ExistsTransactionSuccess(TRANSACTION_ID, req.getSequence(), existsResult));
61         assertFutureEquals(existsResult, exists);
62     }
63
64     @Override
65     @Test
66     public void testRead() throws Exception {
67         final TransactionTester<RemoteProxyTransaction> tester = getTester();
68         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read = transaction.read(PATH_2);
69         final ReadTransactionRequest req = tester.expectTransactionRequest(ReadTransactionRequest.class);
70         final Optional<NormalizedNode<?, ?>> result = Optional.of(DATA_1);
71         tester.replySuccess(new ReadTransactionSuccess(TRANSACTION_ID, req.getSequence(), result));
72         assertFutureEquals(result, read);
73     }
74
75     @Override
76     @Test
77     public void testWrite() throws Exception {
78         final YangInstanceIdentifier path = PATH_1;
79         testModification(() -> transaction.write(path, DATA_1), TransactionWrite.class, path);
80     }
81
82     @Override
83     @Test
84     public void testMerge() throws Exception {
85         final YangInstanceIdentifier path = PATH_2;
86         testModification(() -> transaction.merge(path, DATA_2), TransactionMerge.class, path);
87     }
88
89     @Override
90     @Test
91     public void testDelete() throws Exception {
92         final YangInstanceIdentifier path = PATH_3;
93         testModification(() -> transaction.delete(path), TransactionDelete.class, path);
94     }
95
96     @Override
97     @Test
98     public void testDirectCommit() throws Exception {
99         transaction.seal();
100         final ListenableFuture<Boolean> result = transaction.directCommit();
101         final TransactionTester<RemoteProxyTransaction> tester = getTester();
102         final ModifyTransactionRequest req = tester.expectTransactionRequest(ModifyTransactionRequest.class);
103         Assert.assertTrue(req.getPersistenceProtocol().isPresent());
104         Assert.assertEquals(PersistenceProtocol.SIMPLE, req.getPersistenceProtocol().get());
105         tester.replySuccess(new TransactionCommitSuccess(TRANSACTION_ID, req.getSequence()));
106         assertFutureEquals(true, result);
107     }
108
109     @Override
110     @Test
111     public void testCanCommit() throws Exception {
112         testRequestResponse(transaction::canCommit, ModifyTransactionRequest.class,
113                 TransactionCanCommitSuccess::new);
114     }
115
116     @Override
117     @Test
118     public void testPreCommit() throws Exception {
119         testRequestResponse(transaction::preCommit, TransactionPreCommitRequest.class,
120                 TransactionPreCommitSuccess::new);
121     }
122
123     @Override
124     @Test
125     public void testDoCommit() throws Exception {
126         testRequestResponse(transaction::doCommit, TransactionDoCommitRequest.class, TransactionCommitSuccess::new);
127     }
128
129     @Override
130     @Test
131     public void testForwardToRemoteAbort() throws Exception {
132         final TestProbe probe = createProbe();
133         final TransactionAbortRequest request = new TransactionAbortRequest(TRANSACTION_ID, 0L, probe.ref());
134         testForwardToRemote(request, TransactionAbortRequest.class);
135
136     }
137
138     @Override
139     public void testForwardToRemoteCommit() throws Exception {
140         final TestProbe probe = createProbe();
141         final TransactionAbortRequest request = new TransactionAbortRequest(TRANSACTION_ID, 0L, probe.ref());
142         testForwardToRemote(request, TransactionAbortRequest.class);
143     }
144
145     @Test
146     public void testForwardToRemoteModifyCommitSimple() throws Exception {
147         final TestProbe probe = createProbe();
148         final ModifyTransactionRequestBuilder builder =
149                 new ModifyTransactionRequestBuilder(TRANSACTION_ID, probe.ref());
150         builder.setSequence(0L);
151         builder.setCommit(false);
152         final ModifyTransactionRequest request = builder.build();
153         final ModifyTransactionRequest received = testForwardToRemote(request, ModifyTransactionRequest.class);
154         Assert.assertEquals(request.getPersistenceProtocol(), received.getPersistenceProtocol());
155         Assert.assertEquals(request.getModifications(), received.getModifications());
156         Assert.assertEquals(request.getTarget(), received.getTarget());
157     }
158
159     @Test
160     public void testForwardToRemoteModifyCommit3Phase() throws Exception {
161         final TestProbe probe = createProbe();
162         final ModifyTransactionRequestBuilder builder =
163                 new ModifyTransactionRequestBuilder(TRANSACTION_ID, probe.ref());
164         builder.setSequence(0L);
165         builder.setCommit(true);
166         final ModifyTransactionRequest request = builder.build();
167         final ModifyTransactionRequest received = testForwardToRemote(request, ModifyTransactionRequest.class);
168         Assert.assertEquals(request.getPersistenceProtocol(), received.getPersistenceProtocol());
169         Assert.assertEquals(request.getModifications(), received.getModifications());
170         Assert.assertEquals(request.getTarget(), received.getTarget());
171     }
172
173     @Test
174     public void testForwardToRemoteModifyAbort() throws Exception {
175         final TestProbe probe = createProbe();
176         final ModifyTransactionRequestBuilder builder =
177                 new ModifyTransactionRequestBuilder(TRANSACTION_ID, probe.ref());
178         builder.setSequence(0L);
179         builder.setAbort();
180         final ModifyTransactionRequest request = builder.build();
181         final ModifyTransactionRequest received = testForwardToRemote(request, ModifyTransactionRequest.class);
182         Assert.assertEquals(request.getTarget(), received.getTarget());
183         Assert.assertTrue(received.getPersistenceProtocol().isPresent());
184         Assert.assertEquals(PersistenceProtocol.ABORT, received.getPersistenceProtocol().get());
185     }
186
187     @Test
188     public void testForwardToRemoteModifyRead() throws Exception {
189         final TestProbe probe = createProbe();
190         final ReadTransactionRequest request =
191                 new ReadTransactionRequest(TRANSACTION_ID, 0L, probe.ref(), PATH_1, false);
192         final ReadTransactionRequest received = testForwardToRemote(request, ReadTransactionRequest.class);
193         Assert.assertEquals(request.getTarget(), received.getTarget());
194         Assert.assertEquals(request.getPath(), received.getPath());
195     }
196
197     @Test
198     public void testForwardToRemoteModifyExists() throws Exception {
199         final TestProbe probe = createProbe();
200         final ExistsTransactionRequest request =
201                 new ExistsTransactionRequest(TRANSACTION_ID, 0L, probe.ref(), PATH_1, false);
202         final ExistsTransactionRequest received = testForwardToRemote(request, ExistsTransactionRequest.class);
203         Assert.assertEquals(request.getTarget(), received.getTarget());
204         Assert.assertEquals(request.getPath(), received.getPath());
205     }
206
207     @Test
208     public void testForwardToRemoteModifyPreCommit() throws Exception {
209         final TestProbe probe = createProbe();
210         final TransactionPreCommitRequest request =
211                 new TransactionPreCommitRequest(TRANSACTION_ID, 0L, probe.ref());
212         final TransactionPreCommitRequest received = testForwardToRemote(request, TransactionPreCommitRequest.class);
213         Assert.assertEquals(request.getTarget(), received.getTarget());
214     }
215
216     @Test
217     public void testForwardToRemoteModifyDoCommit() throws Exception {
218         final TestProbe probe = createProbe();
219         final TransactionDoCommitRequest request =
220                 new TransactionDoCommitRequest(TRANSACTION_ID, 0L, probe.ref());
221         final TransactionDoCommitRequest received = testForwardToRemote(request, TransactionDoCommitRequest.class);
222         Assert.assertEquals(request.getTarget(), received.getTarget());
223     }
224
225
226     private <T extends TransactionModification> void testModification(final Runnable modification,
227                                                                       final Class<T> cls,
228                                                                       final YangInstanceIdentifier expectedPath) {
229         modification.run();
230         final ModifyTransactionRequest request = transaction.commitRequest(false);
231         final List<TransactionModification> modifications = request.getModifications();
232         Assert.assertEquals(1, modifications.size());
233         Assert.assertThat(modifications, hasItem(both(isA(cls)).and(hasPath(expectedPath))));
234     }
235
236 }

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