2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.dom.store.inmemory;
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.doThrow;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.MockitoAnnotations.initMocks;
21 import java.lang.reflect.Field;
22 import java.util.concurrent.ExecutionException;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Mock;
26 import org.opendaylight.mdsal.common.api.OptimisticLockFailedException;
27 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
28 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedTransactions;
29 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
37 public class InMemoryDOMStoreThreePhaseCommitCohortTest {
39 private static InMemoryDOMStoreThreePhaseCommitCohort inMemoryDOMStoreThreePhaseCommitCohort = null;
42 private static InMemoryDOMDataStore IN_MEMORY_DOM_DATA_STORE;
45 private static DataTreeCandidate DATA_TREE_CANDIDATE;
48 private static TransactionReadyPrototype<String> TRANSACTION_READY_PROTOTYPE;
51 private static DataTreeSnapshot DATA_TREE_SNAPSHOT;
54 private static DataTreeModification DATA_TREE_MODIFICATION;
57 public void setUp() throws Exception {
59 doReturn(DATA_TREE_MODIFICATION).when(DATA_TREE_SNAPSHOT).newModification();
60 doReturn("testModification").when(DATA_TREE_MODIFICATION).toString();
61 inMemoryDOMStoreThreePhaseCommitCohort =
62 new InMemoryDOMStoreThreePhaseCommitCohort(IN_MEMORY_DOM_DATA_STORE,
63 SnapshotBackedTransactions.newWriteTransaction(
64 "test", false, DATA_TREE_SNAPSHOT, TRANSACTION_READY_PROTOTYPE),
65 DATA_TREE_MODIFICATION,
70 public void canCommitTest() throws Exception {
71 doNothing().when(IN_MEMORY_DOM_DATA_STORE).validate(any());
72 inMemoryDOMStoreThreePhaseCommitCohort.canCommit();
73 verify(IN_MEMORY_DOM_DATA_STORE).validate(any());
77 public void canCommitWithOperationError() throws Exception {
78 RuntimeException operationError = new RuntimeException();
79 inMemoryDOMStoreThreePhaseCommitCohort =
80 new InMemoryDOMStoreThreePhaseCommitCohort(IN_MEMORY_DOM_DATA_STORE,
81 SnapshotBackedTransactions.newWriteTransaction(
82 "test", false, DATA_TREE_SNAPSHOT, TRANSACTION_READY_PROTOTYPE),
83 DATA_TREE_MODIFICATION,
85 doNothing().when(IN_MEMORY_DOM_DATA_STORE).validate(any());
87 inMemoryDOMStoreThreePhaseCommitCohort.canCommit().get();
88 fail("Expected exception");
89 } catch (ExecutionException e) {
90 assertTrue(e.getCause() == operationError);
94 @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
95 @Test(expected = OptimisticLockFailedException.class)
96 public void canCommitTestWithOptimisticLockFailedException() throws Throwable {
97 doThrow(new ConflictingModificationAppliedException(YangInstanceIdentifier.EMPTY, "testException"))
98 .when(IN_MEMORY_DOM_DATA_STORE).validate(any());
100 inMemoryDOMStoreThreePhaseCommitCohort.canCommit().get();
101 fail("Expected exception");
102 } catch (ExecutionException e) {
103 assertTrue(e.getCause() instanceof OptimisticLockFailedException);
108 @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
109 @Test(expected = TransactionCommitFailedException.class)
110 public void canCommitTestWithTransactionCommitFailedException() throws Throwable {
111 doThrow(new DataValidationFailedException(YangInstanceIdentifier.EMPTY, "testException"))
112 .when(IN_MEMORY_DOM_DATA_STORE).validate(any());
114 inMemoryDOMStoreThreePhaseCommitCohort.canCommit().get();
115 fail("Expected exception");
116 } catch (ExecutionException e) {
117 assertTrue(e.getCause() instanceof TransactionCommitFailedException);
122 @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
123 @Test(expected = UnsupportedOperationException.class)
124 public void canCommitTestWithUnknownException() throws Throwable {
125 doThrow(new UnsupportedOperationException("testException"))
126 .when(IN_MEMORY_DOM_DATA_STORE).validate(any());
128 inMemoryDOMStoreThreePhaseCommitCohort.canCommit().get();
129 fail("Expected exception");
130 } catch (ExecutionException e) {
131 assertTrue(e.getCause() instanceof UnsupportedOperationException);
137 public void preCommitTest() throws Exception {
138 doReturn(DATA_TREE_CANDIDATE).when(IN_MEMORY_DOM_DATA_STORE).prepare(any());
139 inMemoryDOMStoreThreePhaseCommitCohort.preCommit().get();
140 verify(IN_MEMORY_DOM_DATA_STORE).prepare(any());
143 @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
144 @Test(expected = UnsupportedOperationException.class)
145 public void preCommitTestWithUnknownException() throws Throwable {
146 doThrow(new UnsupportedOperationException("testException"))
147 .when(IN_MEMORY_DOM_DATA_STORE).prepare(any());
149 inMemoryDOMStoreThreePhaseCommitCohort.preCommit().get();
150 fail("Expected exception");
151 } catch (ExecutionException e) {
152 assertTrue(e.getCause() instanceof UnsupportedOperationException);
158 public void abortTest() throws Exception {
159 doReturn(DATA_TREE_CANDIDATE).when(IN_MEMORY_DOM_DATA_STORE).prepare(any());
160 doReturn("testDataTreeCandidate").when(DATA_TREE_CANDIDATE).toString();
161 final Field candidateField = InMemoryDOMStoreThreePhaseCommitCohort.class.getDeclaredField("candidate");
162 candidateField.setAccessible(true);
164 inMemoryDOMStoreThreePhaseCommitCohort.preCommit();
165 DataTreeCandidate candidate =
166 (DataTreeCandidate) candidateField.get(inMemoryDOMStoreThreePhaseCommitCohort);
168 assertNotNull(candidate);
169 inMemoryDOMStoreThreePhaseCommitCohort.abort();
170 candidate = (DataTreeCandidate) candidateField.get(inMemoryDOMStoreThreePhaseCommitCohort);
171 assertNull(candidate);
175 public void commitTest() throws Exception {
176 doNothing().when(IN_MEMORY_DOM_DATA_STORE).commit(any());
177 doReturn(DATA_TREE_CANDIDATE).when(IN_MEMORY_DOM_DATA_STORE).prepare(any());
178 inMemoryDOMStoreThreePhaseCommitCohort.preCommit();
179 inMemoryDOMStoreThreePhaseCommitCohort.commit();
180 verify(IN_MEMORY_DOM_DATA_STORE).commit(any());