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.Matchers.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.mock;
19 import static org.mockito.Mockito.reset;
20 import static org.mockito.Mockito.verify;
22 import java.lang.reflect.Field;
23 import java.util.concurrent.ExecutionException;
24 import org.junit.Before;
25 import org.junit.Test;
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;
40 private static final InMemoryDOMDataStore IN_MEMORY_DOM_DATA_STORE = mock(InMemoryDOMDataStore.class);
41 private static final DataTreeCandidate DATA_TREE_CANDIDATE = mock(DataTreeCandidate.class);
44 public void setUp() throws Exception {
45 reset(IN_MEMORY_DOM_DATA_STORE);
46 DataTreeSnapshot dataTreeSnapshot = mock(DataTreeSnapshot.class);
47 TransactionReadyPrototype<String> transactionReadyPrototype = mock(TransactionReadyPrototype.class);
48 DataTreeModification dataTreeModification = mock(DataTreeModification.class);
49 doReturn(dataTreeModification).when(dataTreeSnapshot).newModification();
50 doReturn("testModification").when(dataTreeModification).toString();
52 inMemoryDOMStoreThreePhaseCommitCohort =
53 new InMemoryDOMStoreThreePhaseCommitCohort(IN_MEMORY_DOM_DATA_STORE,
54 SnapshotBackedTransactions
55 .newWriteTransaction("test", false, dataTreeSnapshot, transactionReadyPrototype),
56 dataTreeModification);
60 public void canCommitTest() throws Exception {
61 doNothing().when(IN_MEMORY_DOM_DATA_STORE).validate(any());
62 inMemoryDOMStoreThreePhaseCommitCohort.canCommit();
63 verify(IN_MEMORY_DOM_DATA_STORE).validate(any());
66 @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
67 @Test(expected = OptimisticLockFailedException.class)
68 public void canCommitTestWithOptimisticLockFailedException() throws Throwable {
69 doThrow(new ConflictingModificationAppliedException(YangInstanceIdentifier.EMPTY, "testException"))
70 .when(IN_MEMORY_DOM_DATA_STORE).validate(any());
72 inMemoryDOMStoreThreePhaseCommitCohort.canCommit().get();
73 fail("Expected exception");
74 } catch (ExecutionException e) {
75 assertTrue(e.getCause() instanceof OptimisticLockFailedException);
80 @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
81 @Test(expected = TransactionCommitFailedException.class)
82 public void canCommitTestWithTransactionCommitFailedException() throws Throwable {
83 doThrow(new DataValidationFailedException(YangInstanceIdentifier.EMPTY, "testException"))
84 .when(IN_MEMORY_DOM_DATA_STORE).validate(any());
86 inMemoryDOMStoreThreePhaseCommitCohort.canCommit().get();
87 fail("Expected exception");
88 } catch (ExecutionException e) {
89 assertTrue(e.getCause() instanceof TransactionCommitFailedException);
94 @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
95 @Test(expected = UnsupportedOperationException.class)
96 public void canCommitTestWithUnknownException() throws Throwable {
97 doThrow(new UnsupportedOperationException("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 UnsupportedOperationException);
109 public void preCommitTest() throws Exception {
110 doReturn(DATA_TREE_CANDIDATE).when(IN_MEMORY_DOM_DATA_STORE).prepare(any());
111 inMemoryDOMStoreThreePhaseCommitCohort.preCommit().get();
112 verify(IN_MEMORY_DOM_DATA_STORE).prepare(any());
115 @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
116 @Test(expected = UnsupportedOperationException.class)
117 public void preCommitTestWithUnknownException() throws Throwable {
118 doThrow(new UnsupportedOperationException("testException"))
119 .when(IN_MEMORY_DOM_DATA_STORE).prepare(any());
121 inMemoryDOMStoreThreePhaseCommitCohort.preCommit().get();
122 fail("Expected exception");
123 } catch (ExecutionException e) {
124 assertTrue(e.getCause() instanceof UnsupportedOperationException);
130 public void abortTest() throws Exception {
131 doReturn(DATA_TREE_CANDIDATE).when(IN_MEMORY_DOM_DATA_STORE).prepare(any());
132 doReturn("testDataTreeCandidate").when(DATA_TREE_CANDIDATE).toString();
133 final Field candidateField = InMemoryDOMStoreThreePhaseCommitCohort.class.getDeclaredField("candidate");
134 candidateField.setAccessible(true);
136 inMemoryDOMStoreThreePhaseCommitCohort.preCommit();
137 DataTreeCandidate candidate =
138 (DataTreeCandidate) candidateField.get(inMemoryDOMStoreThreePhaseCommitCohort);
140 assertNotNull(candidate);
141 inMemoryDOMStoreThreePhaseCommitCohort.abort();
142 candidate = (DataTreeCandidate) candidateField.get(inMemoryDOMStoreThreePhaseCommitCohort);
143 assertNull(candidate);
147 public void commitTest() throws Exception {
148 doNothing().when(IN_MEMORY_DOM_DATA_STORE).commit(any());
149 doReturn(DATA_TREE_CANDIDATE).when(IN_MEMORY_DOM_DATA_STORE).prepare(any());
150 inMemoryDOMStoreThreePhaseCommitCohort.preCommit();
151 inMemoryDOMStoreThreePhaseCommitCohort.commit();
152 verify(IN_MEMORY_DOM_DATA_STORE).commit(any());