5834833e688f33b26698b6159da62c6504ae1109
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / databroker / actors / dds / ClientTransactionTest.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.verify;
11 import static org.mockito.Mockito.when;
12 import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.TRANSACTION_ID;
13 import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.assertFutureEquals;
14 import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.assertOperationThrowsException;
15 import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.getWithTimeout;
16
17 import com.google.common.base.Optional;
18 import com.google.common.util.concurrent.CheckedFuture;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.opendaylight.controller.cluster.access.commands.CommitLocalTransactionRequest;
25 import org.opendaylight.controller.cluster.access.commands.TransactionCommitSuccess;
26 import org.opendaylight.mdsal.common.api.ReadFailedException;
27 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
28 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.CursorAwareDataTreeModification;
33 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
34
35 public class ClientTransactionTest extends AbstractClientHandleTest<ClientTransaction> {
36
37     private static final YangInstanceIdentifier PATH = YangInstanceIdentifier.builder()
38             .node(QName.create("ns-1", "node-1"))
39             .build();
40     private static final NormalizedNode<?, ?> DATA = Builders.containerBuilder()
41             .withNodeIdentifier(YangInstanceIdentifier.NodeIdentifier.create(PATH.getLastPathArgument().getNodeType()))
42             .build();
43
44     @Mock
45     private CursorAwareDataTreeModification modification;
46
47     @Override
48     @Before
49     public void setUp() throws Exception {
50         super.setUp();
51         when(getDataTreeSnapshot().newModification()).thenReturn(modification);
52         when(modification.readNode(PATH)).thenReturn(Optional.of(DATA));
53     }
54
55     @Override
56     protected ClientTransaction createHandle(final AbstractClientHistory parent) {
57         return parent.createTransaction();
58     }
59
60     @Override
61     protected void doHandleOperation(final ClientTransaction transaction) {
62         transaction.read(PATH);
63     }
64
65     @Test
66     public void testOpenCloseCursor() throws Exception {
67         final DOMDataTreeWriteCursor cursor = getHandle().openCursor();
68         getHandle().closeCursor(cursor);
69         getHandle().openCursor().delete(PATH.getLastPathArgument());
70         verify(modification).delete(PATH);
71     }
72
73     @Test
74     public void testOpenSecondCursor() throws Exception {
75         getHandle().openCursor();
76         assertOperationThrowsException(getHandle()::openCursor, IllegalStateException.class);
77     }
78
79     @Test
80     public void testExists() throws Exception {
81         final CheckedFuture<Boolean, ReadFailedException> exists = getHandle().exists(PATH);
82         verify(modification).readNode(PATH);
83         Assert.assertTrue(getWithTimeout(exists));
84     }
85
86     @Test
87     public void testRead() throws Exception {
88         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultFuture = getHandle().read(PATH);
89         verify(modification).readNode(PATH);
90         final Optional<NormalizedNode<?, ?>> result = getWithTimeout(resultFuture);
91         Assert.assertTrue(result.isPresent());
92         Assert.assertEquals(DATA, result.get());
93     }
94
95     @Test
96     public void testDelete() throws Exception {
97         getHandle().delete(PATH);
98         verify(modification).delete(PATH);
99     }
100
101     @Test
102     public void testMerge() throws Exception {
103         getHandle().merge(PATH, DATA);
104         verify(modification).merge(PATH, DATA);
105     }
106
107     @Test
108     public void testWrite() throws Exception {
109         getHandle().write(PATH, DATA);
110         verify(modification).write(PATH, DATA);
111     }
112
113     @Test
114     public void testReadyEmpty() throws Exception {
115         final DOMStoreThreePhaseCommitCohort cohort = getHandle().ready();
116         assertFutureEquals(true, cohort.canCommit());
117         assertFutureEquals(null, cohort.preCommit());
118         assertFutureEquals(null, cohort.commit());
119     }
120
121     @Test
122     public void testReady() throws Exception {
123         getHandle().write(PATH, DATA);
124         final DOMStoreThreePhaseCommitCohort cohort = getHandle().ready();
125         final TransactionCommitSuccess response = new TransactionCommitSuccess(TRANSACTION_ID, 0L);
126         final ListenableFuture<Boolean> actual = cohort.canCommit();
127         final CommitLocalTransactionRequest request =
128                 backendRespondToRequest(CommitLocalTransactionRequest.class, response);
129         Assert.assertEquals(modification, request.getModification());
130         assertFutureEquals(true, actual);
131         assertFutureEquals(null, cohort.preCommit());
132         assertFutureEquals(null, cohort.commit());
133     }
134
135     @Test
136     public void testReadyNoFurtherOperationsAllowed() throws Exception {
137         getHandle().ready();
138         checkClosed();
139     }
140
141 }

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