Bug 5947: Increasing code coverage - 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     @Test(expected = OptimisticLockFailedException.class)
66     public void canCommitTestWithOptimisticLockFailedException() throws Throwable {
67         doThrow(new ConflictingModificationAppliedException(YangInstanceIdentifier.EMPTY, "testException"))
68                 .when(IN_MEMORY_DOM_DATA_STORE).validate(any());
69         try {
70             inMemoryDOMStoreThreePhaseCommitCohort.canCommit().get();
71             fail("Expected exception");
72         } catch (Exception e) {
73             assertTrue(e.getCause() instanceof OptimisticLockFailedException);
74             throw e.getCause();
75         }
76     }
77
78     @Test(expected = TransactionCommitFailedException.class)
79     public void canCommitTestWithTransactionCommitFailedException() throws Throwable {
80         doThrow(new DataValidationFailedException(YangInstanceIdentifier.EMPTY, "testException"))
81                 .when(IN_MEMORY_DOM_DATA_STORE).validate(any());
82         try {
83             inMemoryDOMStoreThreePhaseCommitCohort.canCommit().get();
84             fail("Expected exception");
85         } catch (Exception e) {
86             assertTrue(e.getCause() instanceof TransactionCommitFailedException);
87             throw e.getCause();
88         }
89     }
90
91     @Test(expected = UnsupportedOperationException.class)
92     public void canCommitTestWithUnknownException() throws Throwable {
93         doThrow(new UnsupportedOperationException("testException"))
94                 .when(IN_MEMORY_DOM_DATA_STORE).validate(any());
95         try {
96             inMemoryDOMStoreThreePhaseCommitCohort.canCommit().get();
97             fail("Expected exception");
98         } catch (Exception e) {
99             assertTrue(e.getCause() instanceof UnsupportedOperationException);
100             throw e.getCause();
101         }
102     }
103
104     @Test
105     public void preCommitTest() throws Exception {
106         doReturn(DATA_TREE_CANDIDATE).when(IN_MEMORY_DOM_DATA_STORE).prepare(any());
107         inMemoryDOMStoreThreePhaseCommitCohort.preCommit().get();
108         verify(IN_MEMORY_DOM_DATA_STORE).prepare(any());
109     }
110
111     @Test(expected = UnsupportedOperationException.class)
112     public void preCommitTestWithUnknownException() throws Throwable {
113         doThrow(new UnsupportedOperationException("testException"))
114                 .when(IN_MEMORY_DOM_DATA_STORE).prepare(any());
115         try {
116             inMemoryDOMStoreThreePhaseCommitCohort.preCommit().get();
117             fail("Expected exception");
118         } catch (Exception e) {
119             assertTrue(e.getCause() instanceof UnsupportedOperationException);
120             throw e.getCause();
121         }
122     }
123
124     @Test
125     public void abortTest() throws Exception {
126         doReturn(DATA_TREE_CANDIDATE).when(IN_MEMORY_DOM_DATA_STORE).prepare(any());
127         doReturn("testDataTreeCandidate").when(DATA_TREE_CANDIDATE).toString();
128         final Field candidateField = InMemoryDOMStoreThreePhaseCommitCohort.class.getDeclaredField("candidate");
129         candidateField.setAccessible(true);
130
131         inMemoryDOMStoreThreePhaseCommitCohort.preCommit();
132         DataTreeCandidate candidate =
133                 (DataTreeCandidate) candidateField.get(inMemoryDOMStoreThreePhaseCommitCohort);
134
135         assertNotNull(candidate);
136         inMemoryDOMStoreThreePhaseCommitCohort.abort();
137         candidate = (DataTreeCandidate) candidateField.get(inMemoryDOMStoreThreePhaseCommitCohort);
138         assertNull(candidate);
139     }
140
141     @Test
142     public void commitTest() throws Exception {
143         doNothing().when(IN_MEMORY_DOM_DATA_STORE).commit(any());
144         doReturn(DATA_TREE_CANDIDATE).when(IN_MEMORY_DOM_DATA_STORE).prepare(any());
145         inMemoryDOMStoreThreePhaseCommitCohort.preCommit();
146         inMemoryDOMStoreThreePhaseCommitCohort.commit();
147         verify(IN_MEMORY_DOM_DATA_STORE).commit(any());
148     }
149 }