Report ExactDataObjectStep from DataObjectModification
[mdsal.git] / dom / mdsal-dom-spi / src / test / java / org / opendaylight / mdsal / dom / spi / AbstractDOMForwardedTransactionTest.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.spi;
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.assertThrows;
14 import static org.mockito.Mockito.doThrow;
15 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
16 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.OPERATIONAL;
17
18 import java.util.function.Function;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.junit.MockitoJUnitRunner;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.mdsal.common.api.TransactionDatastoreMismatchException;
25 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransaction;
26
27 @RunWith(MockitoJUnitRunner.StrictStubs.class)
28 public class AbstractDOMForwardedTransactionTest {
29     @Mock
30     public DOMStoreTransaction configTx;
31     @Mock
32     public DOMStoreTransaction operationalTx;
33
34     @Test
35     public void closeSubtransactionsTest() throws Exception {
36         doThrow(UnsupportedOperationException.class).when(configTx).close();
37
38         final var forwardedTx = new DOMForwardedTransactionTestImpl("test",
39             type -> switch (type) {
40                 case CONFIGURATION -> configTx;
41                 case OPERATIONAL -> operationalTx;
42             });
43
44         forwardedTx.closeSubtransactions(); // no backingTx yet -> close do nothing ignored
45
46         forwardedTx.getSubtransaction(CONFIGURATION); // backingTx created, exception on close is expected
47
48         final var ex = assertThrows(IllegalStateException.class,
49             forwardedTx::closeSubtransactions);
50         assertEquals("Uncaught exception occurred during closing transaction", ex.getMessage());
51         assertThat(ex.getCause(), instanceOf(UnsupportedOperationException.class));
52     }
53
54     @Test
55     public void datastoreMismatchOnGetSubtransaction() {
56         final var forwardedTx = new DOMForwardedTransactionTestImpl("test",
57             type -> switch (type) {
58                 case CONFIGURATION -> configTx;
59                 case OPERATIONAL -> operationalTx;
60             });
61
62         assertEquals(configTx, forwardedTx.getSubtransaction(CONFIGURATION));
63         final var exception = assertThrows(TransactionDatastoreMismatchException.class,
64                 () -> forwardedTx.getSubtransaction(OPERATIONAL));
65         assertEquals(CONFIGURATION, exception.expected());
66         assertEquals(OPERATIONAL, exception.encountered());
67     }
68
69     private static final class DOMForwardedTransactionTestImpl
70             extends AbstractDOMForwardedTransaction<DOMStoreTransaction> {
71         DOMForwardedTransactionTestImpl(final Object identifier,
72                 final Function<LogicalDatastoreType, DOMStoreTransaction> backingTxFactory) {
73             super(identifier, backingTxFactory);
74         }
75     }
76 }