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.spi;
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;
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;
27 @RunWith(MockitoJUnitRunner.StrictStubs.class)
28 public class AbstractDOMForwardedTransactionTest {
30 public DOMStoreTransaction configTx;
32 public DOMStoreTransaction operationalTx;
35 public void closeSubtransactionsTest() throws Exception {
36 doThrow(UnsupportedOperationException.class).when(configTx).close();
38 final var forwardedTx = new DOMForwardedTransactionTestImpl("test",
39 type -> switch (type) {
40 case CONFIGURATION -> configTx;
41 case OPERATIONAL -> operationalTx;
44 forwardedTx.closeSubtransactions(); // no backingTx yet -> close do nothing ignored
46 forwardedTx.getSubtransaction(CONFIGURATION); // backingTx created, exception on close is expected
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));
55 public void datastoreMismatchOnGetSubtransaction() {
56 final var forwardedTx = new DOMForwardedTransactionTestImpl("test",
57 type -> switch (type) {
58 case CONFIGURATION -> configTx;
59 case OPERATIONAL -> operationalTx;
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());
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);