Bug 8342: Add info logging to ConfigManagerActivator
[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 TransactionAbortRequest received = testForwardToRemote(request, TransactionAbortRequest.class);
182         Assert.assertEquals(request.getTarget(), received.getTarget());
183     }
184
185     @Test
186     public void testForwardToRemoteModifyRead() throws Exception {
187         final TestProbe probe = createProbe();
188         final ReadTransactionRequest request =
189                 new ReadTransactionRequest(TRANSACTION_ID, 0L, probe.ref(), PATH_1, false);
190         final ReadTransactionRequest received = testForwardToRemote(request, ReadTransactionRequest.class);
191         Assert.assertEquals(request.getTarget(), received.getTarget());
192         Assert.assertEquals(request.getPath(), received.getPath());
193     }
194
195     @Test
196     public void testForwardToRemoteModifyExists() throws Exception {
197         final TestProbe probe = createProbe();
198         final ExistsTransactionRequest request =
199                 new ExistsTransactionRequest(TRANSACTION_ID, 0L, probe.ref(), PATH_1, false);
200         final ExistsTransactionRequest received = testForwardToRemote(request, ExistsTransactionRequest.class);
201         Assert.assertEquals(request.getTarget(), received.getTarget());
202         Assert.assertEquals(request.getPath(), received.getPath());
203     }
204
205     @Test
206     public void testForwardToRemoteModifyPreCommit() throws Exception {
207         final TestProbe probe = createProbe();
208         final TransactionPreCommitRequest request =
209                 new TransactionPreCommitRequest(TRANSACTION_ID, 0L, probe.ref());
210         final TransactionPreCommitRequest received = testForwardToRemote(request, TransactionPreCommitRequest.class);
211         Assert.assertEquals(request.getTarget(), received.getTarget());
212     }
213
214     @Test
215     public void testForwardToRemoteModifyDoCommit() throws Exception {
216         final TestProbe probe = createProbe();
217         final TransactionDoCommitRequest request =
218                 new TransactionDoCommitRequest(TRANSACTION_ID, 0L, probe.ref());
219         final TransactionDoCommitRequest received = testForwardToRemote(request, TransactionDoCommitRequest.class);
220         Assert.assertEquals(request.getTarget(), received.getTarget());
221     }
222
223
224     private <T extends TransactionModification> void testModification(final Runnable modification,
225                                                                       final Class<T> cls,
226                                                                       final YangInstanceIdentifier expectedPath) {
227         modification.run();
228         final ModifyTransactionRequest request = transaction.commitRequest(false);
229         final List<TransactionModification> modifications = request.getModifications();
230         Assert.assertEquals(1, modifications.size());
231         Assert.assertThat(modifications, hasItem(both(isA(cls)).and(hasPath(expectedPath))));
232     }
233
234 }

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