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.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertSame;
16 import static org.junit.Assert.assertThrows;
17 import static org.junit.Assert.assertTrue;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.doReturn;
20 import static org.mockito.Mockito.doThrow;
21 import static org.mockito.Mockito.mock;
23 import com.google.common.util.concurrent.Futures;
24 import java.util.Optional;
25 import java.util.concurrent.ExecutionException;
26 import org.junit.Test;
27 import org.opendaylight.mdsal.common.api.ReadFailedException;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshot;
32 public class SnapshotBackedReadTransactionTest {
34 private static final DataTreeSnapshot DATA_TREE_SNAPSHOT = mock(DataTreeSnapshot.class);
36 private SnapshotBackedReadTransaction<Object> snapshotBackedReadTransaction =
37 new SnapshotBackedReadTransaction<>(new Object(), false, DATA_TREE_SNAPSHOT, null);
40 public void basicTest() throws Exception {
41 final var testNode = mock(ContainerNode.class);
42 final var optional = Optional.of(testNode);
43 doReturn("testNode").when(testNode).toString();
44 doReturn(Optional.of(testNode)).when(DATA_TREE_SNAPSHOT).readNode(YangInstanceIdentifier.of());
45 assertTrue(snapshotBackedReadTransaction.exists(YangInstanceIdentifier.of()).get());
47 assertEquals(optional, snapshotBackedReadTransaction.read(YangInstanceIdentifier.of()).get());
48 final var stableSnapshotField = SnapshotBackedReadTransaction.class.getDeclaredField("stableSnapshot");
49 stableSnapshotField.setAccessible(true);
51 DataTreeSnapshot stableSnapshot = (DataTreeSnapshot) stableSnapshotField.get(snapshotBackedReadTransaction);
52 assertNotNull(stableSnapshot);
53 snapshotBackedReadTransaction.close();
54 stableSnapshot = (DataTreeSnapshot) stableSnapshotField.get(snapshotBackedReadTransaction);
55 assertNull(stableSnapshot);
59 public void readTestWithException() {
60 snapshotBackedReadTransaction.close();
61 final var future = snapshotBackedReadTransaction.read(YangInstanceIdentifier.of());
62 final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
63 assertThat(cause, instanceOf(ReadFailedException.class));
64 assertEquals("Transaction is closed", cause.getMessage());
68 public void readNodeTestWithException() {
69 final var thrown = new NullPointerException("no Node");
70 doThrow(thrown).when(DATA_TREE_SNAPSHOT).readNode(any());
71 snapshotBackedReadTransaction = new SnapshotBackedReadTransaction<>(new Object(), false, DATA_TREE_SNAPSHOT,
74 final var future = snapshotBackedReadTransaction.read(YangInstanceIdentifier.of());
75 final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
76 assertThat(cause, instanceOf(ReadFailedException.class));
77 assertEquals("Read failed", cause.getMessage());
78 assertSame(thrown, cause.getCause());
82 public void existsTestWithException() {
83 final var thrown = new NullPointerException("no Node");
84 doThrow(thrown).when(DATA_TREE_SNAPSHOT).readNode(any());
86 final var future = snapshotBackedReadTransaction.exists(YangInstanceIdentifier.of());
87 final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
88 assertThat(cause, instanceOf(ReadFailedException.class));
89 assertEquals("Read failed", cause.getMessage());
90 assertSame(thrown, cause.getCause());