checkStyleViolationSeverity=error implemented for mdsal-dom-inmemory-datastore
[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.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;
21
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;
35
36 public class InMemoryDOMStoreThreePhaseCommitCohortTest {
37
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);
41
42     @Before
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();
50
51         inMemoryDOMStoreThreePhaseCommitCohort =
52                 new InMemoryDOMStoreThreePhaseCommitCohort(IN_MEMORY_DOM_DATA_STORE,
53                         SnapshotBackedTransactions
54                                 .newWriteTransaction("test", false, dataTreeSnapshot, transactionReadyPrototype),
55                         dataTreeModification);
56     }
57
58     @Test
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());
63     }
64
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());
70         try {
71             inMemoryDOMStoreThreePhaseCommitCohort.canCommit().get();
72             fail("Expected exception");
73         } catch (Exception e) {
74             assertTrue(e.getCause() instanceof OptimisticLockFailedException);
75             throw e.getCause();
76         }
77     }
78
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());
84         try {
85             inMemoryDOMStoreThreePhaseCommitCohort.canCommit().get();
86             fail("Expected exception");
87         } catch (Exception e) {
88             assertTrue(e.getCause() instanceof TransactionCommitFailedException);
89             throw e.getCause();
90         }
91     }
92
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());
98         try {
99             inMemoryDOMStoreThreePhaseCommitCohort.canCommit().get();
100             fail("Expected exception");
101         } catch (Exception e) {
102             assertTrue(e.getCause() instanceof UnsupportedOperationException);
103             throw e.getCause();
104         }
105     }
106
107     @Test
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());
112     }
113
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());
119         try {
120             inMemoryDOMStoreThreePhaseCommitCohort.preCommit().get();
121             fail("Expected exception");
122         } catch (Exception e) {
123             assertTrue(e.getCause() instanceof UnsupportedOperationException);
124             throw e.getCause();
125         }
126     }
127
128     @Test
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);
134
135         inMemoryDOMStoreThreePhaseCommitCohort.preCommit();
136         DataTreeCandidate candidate =
137                 (DataTreeCandidate) candidateField.get(inMemoryDOMStoreThreePhaseCommitCohort);
138
139         assertNotNull(candidate);
140         inMemoryDOMStoreThreePhaseCommitCohort.abort();
141         candidate = (DataTreeCandidate) candidateField.get(inMemoryDOMStoreThreePhaseCommitCohort);
142         assertNull(candidate);
143     }
144
145     @Test
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());
152     }
153 }