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.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertSame;
16 import static org.junit.Assert.assertThrows;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.Mockito.doNothing;
19 import static org.mockito.Mockito.doReturn;
20 import static org.mockito.Mockito.doThrow;
21 import static org.mockito.Mockito.verify;
23 import java.util.concurrent.ExecutionException;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.opendaylight.mdsal.common.api.OptimisticLockFailedException;
29 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
30 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
31 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedTransactions;
32 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
33 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
34 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
38 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
39 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
40 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
42 @RunWith(MockitoJUnitRunner.StrictStubs.class)
43 public class InMemoryDOMStoreThreePhaseCommitCohortTest {
45 private InMemoryDOMDataStore dataStore;
47 private DataTreeCandidate candidate;
49 private TransactionReadyPrototype<String> prototype;
51 private DataTreeSnapshot snapshot;
53 private DataTreeModification modification;
56 public void canCommitTest() throws Exception {
57 doNothing().when(dataStore).validate(any());
58 prepareSimpleCohort().canCommit();
59 verify(dataStore).validate(any());
63 public void canCommitWithOperationError() throws Exception {
64 doReturn(modification).when(snapshot).newModification();
65 final var operationError = new RuntimeException();
66 final var cohort = new InMemoryDOMStoreThreePhaseCommitCohort(dataStore,
67 SnapshotBackedTransactions.newWriteTransaction("test", false, snapshot, prototype), modification,
70 assertSame(operationError, assertFailsCanCommit(cohort));
74 public void canCommitTestWithOptimisticLockFailedException() throws Exception {
75 final var cause = new ConflictingModificationAppliedException(YangInstanceIdentifier.empty(), "testException");
76 doThrow(cause).when(dataStore).validate(any());
78 final var ex = assertFailsCanCommit(prepareSimpleCohort());
79 assertThat(ex, instanceOf(OptimisticLockFailedException.class));
80 assertSame(cause, ex.getCause());
81 final var errors = ((OptimisticLockFailedException) ex).getErrorList();
82 assertEquals(1, errors.size());
83 final var error = errors.get(0);
84 assertEquals(ErrorSeverity.ERROR, error.getSeverity());
85 assertEquals(ErrorType.APPLICATION, error.getErrorType());
86 assertEquals("resource-denied", error.getTag());
90 public void canCommitTestWithTransactionCommitFailedException() throws Exception {
91 final var cause = new DataValidationFailedException(YangInstanceIdentifier.empty(), "testException");
92 doThrow(cause).when(dataStore).validate(any());
94 final var ex = assertFailsCanCommit(prepareSimpleCohort());
95 assertThat(ex, instanceOf(TransactionCommitFailedException.class));
96 assertSame(cause, ex.getCause());
97 final var errors = ((TransactionCommitFailedException) ex).getErrorList();
98 assertEquals(1, errors.size());
99 final var error = errors.get(0);
100 assertEquals(ErrorSeverity.ERROR, error.getSeverity());
101 assertEquals(ErrorType.APPLICATION, error.getErrorType());
102 assertEquals("operation-failed", error.getTag());
106 public void canCommitTestWithUnknownException() throws Exception {
107 final var cause = new UnsupportedOperationException("testException");
108 doThrow(cause).when(dataStore).validate(any());
110 assertSame(cause, assertFailsCanCommit(prepareSimpleCohort()));
114 public void preCommitTest() throws Exception {
115 doReturn(candidate).when(dataStore).prepare(any());
116 prepareSimpleCohort().preCommit().get();
117 verify(dataStore).prepare(any());
121 public void preCommitTestWithUnknownException() throws Exception {
122 final var cause = new UnsupportedOperationException("testException");
123 doThrow(cause).when(dataStore).prepare(any());
125 final var future = prepareSimpleCohort().preCommit();
126 final var ex = assertThrows(ExecutionException.class, future::get).getCause();
127 assertSame(cause, ex);
131 public void abortTest() throws Exception {
132 doReturn(candidate).when(dataStore).prepare(any());
134 final var cohort = prepareSimpleCohort();
136 assertNotNull(cohort.candidate);
139 assertNull(cohort.candidate);
143 public void commitTest() throws Exception {
144 doNothing().when(dataStore).commit(any());
145 doReturn(candidate).when(dataStore).prepare(any());
147 final var cohort = prepareSimpleCohort();
150 verify(dataStore).commit(any());
153 private InMemoryDOMStoreThreePhaseCommitCohort prepareSimpleCohort() {
154 doReturn(modification).when(snapshot).newModification();
155 return new InMemoryDOMStoreThreePhaseCommitCohort(dataStore,
156 SnapshotBackedTransactions.newWriteTransaction("test", false, snapshot, prototype),
160 private static Throwable assertFailsCanCommit(final DOMStoreThreePhaseCommitCohort cohort) {
161 final var future = cohort.canCommit();
162 return assertThrows(ExecutionException.class, future::get).getCause();