Refactor TransactionContext.executeModification()
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / LocalTransactionContextTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.datastore;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.doThrow;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.verify;
15
16 import akka.actor.ActorSelection;
17 import com.google.common.util.concurrent.SettableFuture;
18 import java.util.Optional;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.mockito.MockitoAnnotations;
23 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
24 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
25 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
26 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
27 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
28 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import scala.concurrent.Future;
32
33 public class LocalTransactionContextTest {
34
35     @Mock
36     private OperationLimiter limiter;
37
38     @Mock
39     private DOMStoreReadWriteTransaction readWriteTransaction;
40
41     @Mock
42     private LocalTransactionReadySupport mockReadySupport;
43
44     private LocalTransactionContext localTransactionContext;
45
46     @Before
47     public void setUp() {
48         MockitoAnnotations.initMocks(this);
49         localTransactionContext = new LocalTransactionContext(readWriteTransaction, limiter.getIdentifier(),
50                 mockReadySupport) {
51             @Override
52             protected DOMStoreWriteTransaction getWriteDelegate() {
53                 return readWriteTransaction;
54             }
55
56             @Override
57             protected DOMStoreReadTransaction getReadDelegate() {
58                 return readWriteTransaction;
59             }
60         };
61     }
62
63     @Test
64     public void testWrite() {
65         YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.empty();
66         NormalizedNode<?, ?> normalizedNode = mock(NormalizedNode.class);
67         localTransactionContext.executeWrite(yangInstanceIdentifier, normalizedNode, null);
68         verify(readWriteTransaction).write(yangInstanceIdentifier, normalizedNode);
69     }
70
71     @Test
72     public void testMerge() {
73         YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.empty();
74         NormalizedNode<?, ?> normalizedNode = mock(NormalizedNode.class);
75         localTransactionContext.executeMerge(yangInstanceIdentifier, normalizedNode, null);
76         verify(readWriteTransaction).merge(yangInstanceIdentifier, normalizedNode);
77     }
78
79     @Test
80     public void testDelete() {
81         YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.empty();
82         localTransactionContext.executeDelete(yangInstanceIdentifier, null);
83         verify(readWriteTransaction).delete(yangInstanceIdentifier);
84     }
85
86     @Test
87     public void testRead() {
88         YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.empty();
89         NormalizedNode<?, ?> normalizedNode = mock(NormalizedNode.class);
90         doReturn(FluentFutures.immediateFluentFuture(Optional.of(normalizedNode))).when(readWriteTransaction)
91             .read(yangInstanceIdentifier);
92         localTransactionContext.executeRead(new ReadData(yangInstanceIdentifier, DataStoreVersions.CURRENT_VERSION),
93                 SettableFuture.create(), null);
94         verify(readWriteTransaction).read(yangInstanceIdentifier);
95     }
96
97     @Test
98     public void testExists() {
99         YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.empty();
100         doReturn(FluentFutures.immediateTrueFluentFuture()).when(readWriteTransaction).exists(yangInstanceIdentifier);
101         localTransactionContext.executeRead(new DataExists(yangInstanceIdentifier, DataStoreVersions.CURRENT_VERSION),
102                 SettableFuture.create(), null);
103         verify(readWriteTransaction).exists(yangInstanceIdentifier);
104     }
105
106     @Test
107     public void testReady() {
108         final LocalThreePhaseCommitCohort mockCohort = mock(LocalThreePhaseCommitCohort.class);
109         doReturn(akka.dispatch.Futures.successful(null)).when(mockCohort).initiateCoordinatedCommit(Optional.empty());
110         doReturn(mockCohort).when(mockReadySupport).onTransactionReady(readWriteTransaction, null);
111
112         Future<ActorSelection> future = localTransactionContext.readyTransaction(null, Optional.empty());
113         assertTrue(future.isCompleted());
114
115         verify(mockReadySupport).onTransactionReady(readWriteTransaction, null);
116     }
117
118     @Test
119     public void testReadyWithWriteError() {
120         YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.empty();
121         NormalizedNode<?, ?> normalizedNode = mock(NormalizedNode.class);
122         RuntimeException error = new RuntimeException("mock");
123         doThrow(error).when(readWriteTransaction).write(yangInstanceIdentifier, normalizedNode);
124
125         localTransactionContext.executeWrite(yangInstanceIdentifier, normalizedNode, null);
126         localTransactionContext.executeWrite(yangInstanceIdentifier, normalizedNode, null);
127
128         verify(readWriteTransaction).write(yangInstanceIdentifier, normalizedNode);
129
130         doReadyWithExpectedError(error);
131     }
132
133     @Test
134     public void testReadyWithMergeError() {
135         YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.empty();
136         NormalizedNode<?, ?> normalizedNode = mock(NormalizedNode.class);
137         RuntimeException error = new RuntimeException("mock");
138         doThrow(error).when(readWriteTransaction).merge(yangInstanceIdentifier, normalizedNode);
139
140         localTransactionContext.executeMerge(yangInstanceIdentifier, normalizedNode, null);
141         localTransactionContext.executeMerge(yangInstanceIdentifier, normalizedNode, null);
142
143         verify(readWriteTransaction).merge(yangInstanceIdentifier, normalizedNode);
144
145         doReadyWithExpectedError(error);
146     }
147
148     @Test
149     public void testReadyWithDeleteError() {
150         YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.empty();
151         RuntimeException error = new RuntimeException("mock");
152         doThrow(error).when(readWriteTransaction).delete(yangInstanceIdentifier);
153
154         localTransactionContext.executeDelete(yangInstanceIdentifier, null);
155         localTransactionContext.executeDelete(yangInstanceIdentifier, null);
156
157         verify(readWriteTransaction).delete(yangInstanceIdentifier);
158
159         doReadyWithExpectedError(error);
160     }
161
162     private void doReadyWithExpectedError(final RuntimeException expError) {
163         LocalThreePhaseCommitCohort mockCohort = mock(LocalThreePhaseCommitCohort.class);
164         doReturn(akka.dispatch.Futures.successful(null)).when(mockCohort).initiateCoordinatedCommit(Optional.empty());
165         doReturn(mockCohort).when(mockReadySupport).onTransactionReady(readWriteTransaction, expError);
166
167         localTransactionContext.readyTransaction(null, Optional.empty());
168     }
169 }

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