Add AbstractProxyTransaction derived classes tests
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / databroker / actors / dds / LocalReadOnlyProxyTransactionTest.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.mockito.Mockito.when;
11 import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.assertOperationThrowsException;
12
13 import akka.testkit.TestProbe;
14 import com.google.common.base.VerifyException;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.opendaylight.controller.cluster.access.commands.AbortLocalTransactionRequest;
18 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionRequest;
19 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionRequestBuilder;
20 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
22
23 public class LocalReadOnlyProxyTransactionTest extends LocalProxyTransactionTest<LocalReadOnlyProxyTransaction> {
24
25     private DataTreeSnapshot snapshot;
26
27     @Override
28     protected LocalReadOnlyProxyTransaction createTransaction(final ProxyHistory parent,
29                                                               final TransactionIdentifier id,
30                                                               final DataTreeSnapshot snapshot) {
31         when(snapshot.readNode(PATH_1)).thenReturn(com.google.common.base.Optional.of(DATA_1));
32         when(snapshot.readNode(PATH_3)).thenReturn(com.google.common.base.Optional.absent());
33         this.snapshot = snapshot;
34         return new LocalReadOnlyProxyTransaction(parent, id, this.snapshot);
35     }
36
37     @Test
38     public void testIsSnapshotOnly() {
39         Assert.assertTrue(transaction.isSnapshotOnly());
40     }
41
42     @Test
43     public void testReadOnlyView() {
44         Assert.assertEquals(snapshot, transaction.readOnlyView());
45     }
46
47     @Override
48     @Test(expected = UnsupportedOperationException.class)
49     public void testDirectCommit() throws Exception {
50         transaction.directCommit();
51     }
52
53     @Override
54     @Test(expected = UnsupportedOperationException.class)
55     public void testCanCommit() throws Exception {
56         transaction.canCommit(new VotingFuture<>(new Object(), 1));
57     }
58
59     @Override
60     @Test(expected = UnsupportedOperationException.class)
61     public void testPreCommit() throws Exception {
62         transaction.preCommit(new VotingFuture<>(new Object(), 1));
63     }
64
65     @Override
66     @Test(expected = UnsupportedOperationException.class)
67     public void testDoCommit() throws Exception {
68         transaction.doCommit(new VotingFuture<>(new Object(), 1));
69     }
70
71     @Override
72     @Test(expected = UnsupportedOperationException.class)
73     public void testDelete() {
74         transaction.delete(PATH_1);
75     }
76
77     @Override
78     @Test(expected = UnsupportedOperationException.class)
79     public void testMerge() {
80         transaction.merge(PATH_1, DATA_1);
81     }
82
83     @Override
84     @Test(expected = UnsupportedOperationException.class)
85     public void testWrite() {
86         transaction.write(PATH_1, DATA_1);
87     }
88
89     @Test(expected = UnsupportedOperationException.class)
90     public void testDoDelete() {
91         transaction.doDelete(PATH_1);
92     }
93
94     @Test(expected = UnsupportedOperationException.class)
95     public void testDoMerge() {
96         transaction.doMerge(PATH_1, DATA_1);
97     }
98
99     @Test(expected = UnsupportedOperationException.class)
100     public void testDoWrite() {
101         transaction.doWrite(PATH_1, DATA_1);
102     }
103
104     @Test(expected = UnsupportedOperationException.class)
105     public void testCommitRequest() {
106         transaction.commitRequest(true);
107     }
108
109     @Test
110     public void testApplyModifyTransactionRequest() throws Exception {
111         final TestProbe probe = createProbe();
112         final ModifyTransactionRequestBuilder builder =
113                 new ModifyTransactionRequestBuilder(TRANSACTION_ID, probe.ref());
114         builder.setSequence(0);
115         builder.setAbort();
116         final ModifyTransactionRequest request = builder.build();
117         transaction.applyModifyTransactionRequest(request, createCallbackMock());
118         getTester().expectTransactionRequest(AbortLocalTransactionRequest.class);
119     }
120
121     @Test
122     public void testApplyModifyTransactionRequestNotAbort() throws Exception {
123         final TestProbe probe = createProbe();
124         final ModifyTransactionRequestBuilder builder =
125                 new ModifyTransactionRequestBuilder(TRANSACTION_ID, probe.ref());
126         builder.setSequence(0);
127         builder.setReady();
128         final ModifyTransactionRequest request = builder.build();
129         assertOperationThrowsException(() -> transaction.applyModifyTransactionRequest(request, createCallbackMock()),
130                 VerifyException.class);
131     }
132
133 }