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.store;
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.assertSame;
14 import static org.junit.Assert.assertThrows;
15 import static org.junit.Assert.assertTrue;
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.doThrow;
19 import static org.mockito.Mockito.mock;
21 import com.google.common.util.concurrent.Futures;
22 import java.util.Optional;
23 import java.util.concurrent.ExecutionException;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.mdsal.common.api.ReadFailedException;
27 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
31 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshot;
33 public class SnapshotBackedReadWriteTransactionTest {
35 private static final DataTreeSnapshot DATA_TREE_SNAPSHOT = mock(DataTreeSnapshot.class);
36 private static final DataTreeModification DATA_TREE_MODIFICATION = mock(DataTreeModification.class);
37 private static final TransactionReadyPrototype<Object> TRANSACTION_READY_PROTOTYPE =
38 mock(TransactionReadyPrototype.class);
39 private SnapshotBackedReadWriteTransaction<Object> snapshotBackedReadWriteTransaction;
43 doReturn(DATA_TREE_MODIFICATION).when(DATA_TREE_SNAPSHOT).newModification();
44 snapshotBackedReadWriteTransaction = new SnapshotBackedReadWriteTransaction<>(new Object(), false,
45 DATA_TREE_SNAPSHOT, TRANSACTION_READY_PROTOTYPE);
49 public void basicTest() throws Exception {
50 final NormalizedNode testNode = mock(NormalizedNode.class);
51 final Optional<NormalizedNode> optional = Optional.of(testNode);
52 doReturn("testNode").when(testNode).toString();
53 doReturn(Optional.of(testNode)).when(DATA_TREE_MODIFICATION).readNode(YangInstanceIdentifier.of());
54 assertTrue(snapshotBackedReadWriteTransaction.exists(YangInstanceIdentifier.of()).get());
55 assertEquals(optional, snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.of()).get());
59 public void readTestWithNullException() {
60 doReturn(null).when(DATA_TREE_MODIFICATION).readNode(YangInstanceIdentifier.of());
62 final var future = snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.of());
63 final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
64 assertThat(cause, instanceOf(ReadFailedException.class));
65 assertEquals("Transaction is closed", cause.getMessage());
69 public void readNodeTestWithException() {
70 final var thrown = new NullPointerException("no Node");
71 doThrow(thrown).when(DATA_TREE_MODIFICATION).readNode(any());
73 final var future = snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.of());
74 final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
75 assertThat(cause, instanceOf(ReadFailedException.class));
76 assertEquals("Read failed", cause.getMessage());
77 assertSame(thrown, cause.getCause());
81 public void existsTestWithException() {
82 final var thrown = new NullPointerException("no Node");
83 doThrow(thrown).when(DATA_TREE_MODIFICATION).readNode(any());
85 final var future = snapshotBackedReadWriteTransaction.exists(YangInstanceIdentifier.of());
86 final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
87 assertThat(cause, instanceOf(ReadFailedException.class));
88 assertEquals("Read failed", cause.getMessage());
89 assertSame(thrown, cause.getCause());