145f8aed6741feeb97f36ea6eb986427317dbcf4
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / test / java / org / opendaylight / mdsal / dom / store / inmemory / InMemoryDOMStoreThreePhaseCommitCohortTest.java
1 /*
2  * Copyright (c) 2016 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.mdsal.dom.store.inmemory;
9
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;
22
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;
41
42 @RunWith(MockitoJUnitRunner.StrictStubs.class)
43 public class InMemoryDOMStoreThreePhaseCommitCohortTest {
44     @Mock
45     private InMemoryDOMDataStore dataStore;
46     @Mock
47     private DataTreeCandidate candidate;
48     @Mock
49     private TransactionReadyPrototype<String> prototype;
50     @Mock
51     private DataTreeSnapshot snapshot;
52     @Mock
53     private DataTreeModification modification;
54
55     @Test
56     public void canCommitTest() throws Exception {
57         doNothing().when(dataStore).validate(any());
58         prepareSimpleCohort().canCommit();
59         verify(dataStore).validate(any());
60     }
61
62     @Test
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,
68             operationError);
69
70         assertSame(operationError, assertFailsCanCommit(cohort));
71     }
72
73     @Test
74     public void canCommitTestWithOptimisticLockFailedException() throws Exception {
75         final var cause = new ConflictingModificationAppliedException(YangInstanceIdentifier.empty(), "testException");
76         doThrow(cause).when(dataStore).validate(any());
77
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());
87     }
88
89     @Test
90     public void canCommitTestWithTransactionCommitFailedException() throws Exception {
91         final var cause = new DataValidationFailedException(YangInstanceIdentifier.empty(), "testException");
92         doThrow(cause).when(dataStore).validate(any());
93
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());
103     }
104
105     @Test
106     public void canCommitTestWithUnknownException() throws Exception {
107         final var cause = new UnsupportedOperationException("testException");
108         doThrow(cause).when(dataStore).validate(any());
109
110         assertSame(cause, assertFailsCanCommit(prepareSimpleCohort()));
111     }
112
113     @Test
114     public void preCommitTest() throws Exception {
115         doReturn(candidate).when(dataStore).prepare(any());
116         prepareSimpleCohort().preCommit().get();
117         verify(dataStore).prepare(any());
118     }
119
120     @Test
121     public void preCommitTestWithUnknownException() throws Exception {
122         final var cause = new UnsupportedOperationException("testException");
123         doThrow(cause).when(dataStore).prepare(any());
124
125         final var future = prepareSimpleCohort().preCommit();
126         final var ex = assertThrows(ExecutionException.class, future::get).getCause();
127         assertSame(cause, ex);
128     }
129
130     @Test
131     public void abortTest() throws Exception {
132         doReturn(candidate).when(dataStore).prepare(any());
133
134         final var cohort = prepareSimpleCohort();
135         cohort.preCommit();
136         assertNotNull(cohort.candidate);
137
138         cohort.abort();
139         assertNull(cohort.candidate);
140     }
141
142     @Test
143     public void commitTest() throws Exception {
144         doNothing().when(dataStore).commit(any());
145         doReturn(candidate).when(dataStore).prepare(any());
146
147         final var cohort = prepareSimpleCohort();
148         cohort.preCommit();
149         cohort.commit();
150         verify(dataStore).commit(any());
151     }
152
153     private InMemoryDOMStoreThreePhaseCommitCohort prepareSimpleCohort() {
154         doReturn(modification).when(snapshot).newModification();
155         return new InMemoryDOMStoreThreePhaseCommitCohort(dataStore,
156             SnapshotBackedTransactions.newWriteTransaction("test", false, snapshot, prototype),
157             modification, null);
158     }
159
160     private static Throwable assertFailsCanCommit(final DOMStoreThreePhaseCommitCohort cohort) {
161         final var future = cohort.canCommit();
162         return assertThrows(ExecutionException.class, future::get).getCause();
163     }
164 }