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 org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.mdsal.common.api.OptimisticLockFailedException;
26 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
27 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedTransactions;
28 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
36 public class InMemoryDOMStoreThreePhaseCommitCohortTest {
38 private static InMemoryDOMStoreThreePhaseCommitCohort inMemoryDOMStoreThreePhaseCommitCohort = null;
39 private static final InMemoryDOMDataStore IN_MEMORY_DOM_DATA_STORE = mock(InMemoryDOMDataStore.class);
40 private static final DataTreeCandidate DATA_TREE_CANDIDATE = mock(DataTreeCandidate.class);
43 public void setUp() throws Exception {
44 reset(IN_MEMORY_DOM_DATA_STORE);
45 DataTreeSnapshot dataTreeSnapshot = mock(DataTreeSnapshot.class);
46 TransactionReadyPrototype transactionReadyPrototype = mock(TransactionReadyPrototype.class);
47 DataTreeModification dataTreeModification = mock(DataTreeModification.class);
48 doReturn(dataTreeModification).when(dataTreeSnapshot).newModification();
49 doReturn("testModification").when(dataTreeModification).toString();
51 inMemoryDOMStoreThreePhaseCommitCohort =
52 new InMemoryDOMStoreThreePhaseCommitCohort(IN_MEMORY_DOM_DATA_STORE,
53 SnapshotBackedTransactions
54 .newWriteTransaction("test", false, dataTreeSnapshot, transactionReadyPrototype),
55 dataTreeModification);
59 public void canCommitTest() throws Exception {
60 doNothing().when(IN_MEMORY_DOM_DATA_STORE).validate(any());
61 inMemoryDOMStoreThreePhaseCommitCohort.canCommit();
62 verify(IN_MEMORY_DOM_DATA_STORE).validate(any());
65 @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:IllegalCatch"})
66 @Test(expected = OptimisticLockFailedException.class)
67 public void canCommitTestWithOptimisticLockFailedException() throws Throwable {
68 doThrow(new ConflictingModificationAppliedException(YangInstanceIdentifier.EMPTY, "testException"))
69 .when(IN_MEMORY_DOM_DATA_STORE).validate(any());
71 inMemoryDOMStoreThreePhaseCommitCohort.canCommit().get();
72 fail("Expected exception");
73 } catch (Exception e) {
74 assertTrue(e.getCause() instanceof OptimisticLockFailedException);
79 @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:IllegalCatch"})
80 @Test(expected = TransactionCommitFailedException.class)
81 public void canCommitTestWithTransactionCommitFailedException() throws Throwable {
82 doThrow(new DataValidationFailedException(YangInstanceIdentifier.EMPTY, "testException"))
83 .when(IN_MEMORY_DOM_DATA_STORE).validate(any());
85 inMemoryDOMStoreThreePhaseCommitCohort.canCommit().get();
86 fail("Expected exception");
87 } catch (Exception e) {
88 assertTrue(e.getCause() instanceof TransactionCommitFailedException);
93 @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:IllegalCatch"})
94 @Test(expected = UnsupportedOperationException.class)
95 public void canCommitTestWithUnknownException() throws Throwable {
96 doThrow(new UnsupportedOperationException("testException"))
97 .when(IN_MEMORY_DOM_DATA_STORE).validate(any());
99 inMemoryDOMStoreThreePhaseCommitCohort.canCommit().get();
100 fail("Expected exception");
101 } catch (Exception e) {
102 assertTrue(e.getCause() instanceof UnsupportedOperationException);
108 public void preCommitTest() throws Exception {
109 doReturn(DATA_TREE_CANDIDATE).when(IN_MEMORY_DOM_DATA_STORE).prepare(any());
110 inMemoryDOMStoreThreePhaseCommitCohort.preCommit().get();
111 verify(IN_MEMORY_DOM_DATA_STORE).prepare(any());
114 @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:IllegalCatch"})
115 @Test(expected = UnsupportedOperationException.class)
116 public void preCommitTestWithUnknownException() throws Throwable {
117 doThrow(new UnsupportedOperationException("testException"))
118 .when(IN_MEMORY_DOM_DATA_STORE).prepare(any());
120 inMemoryDOMStoreThreePhaseCommitCohort.preCommit().get();
121 fail("Expected exception");
122 } catch (Exception e) {
123 assertTrue(e.getCause() instanceof UnsupportedOperationException);
129 public void abortTest() throws Exception {
130 doReturn(DATA_TREE_CANDIDATE).when(IN_MEMORY_DOM_DATA_STORE).prepare(any());
131 doReturn("testDataTreeCandidate").when(DATA_TREE_CANDIDATE).toString();
132 final Field candidateField = InMemoryDOMStoreThreePhaseCommitCohort.class.getDeclaredField("candidate");
133 candidateField.setAccessible(true);
135 inMemoryDOMStoreThreePhaseCommitCohort.preCommit();
136 DataTreeCandidate candidate =
137 (DataTreeCandidate) candidateField.get(inMemoryDOMStoreThreePhaseCommitCohort);
139 assertNotNull(candidate);
140 inMemoryDOMStoreThreePhaseCommitCohort.abort();
141 candidate = (DataTreeCandidate) candidateField.get(inMemoryDOMStoreThreePhaseCommitCohort);
142 assertNull(candidate);
146 public void commitTest() throws Exception {
147 doNothing().when(IN_MEMORY_DOM_DATA_STORE).commit(any());
148 doReturn(DATA_TREE_CANDIDATE).when(IN_MEMORY_DOM_DATA_STORE).prepare(any());
149 inMemoryDOMStoreThreePhaseCommitCohort.preCommit();
150 inMemoryDOMStoreThreePhaseCommitCohort.commit();
151 verify(IN_MEMORY_DOM_DATA_STORE).commit(any());