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.lang.reflect.Field;
25 import java.util.Optional;
26 import java.util.concurrent.ExecutionException;
27 import org.junit.Test;
28 import org.opendaylight.mdsal.common.api.ReadFailedException;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshot;
33 public class SnapshotBackedReadTransactionTest {
35 private static final DataTreeSnapshot DATA_TREE_SNAPSHOT = mock(DataTreeSnapshot.class);
37 private SnapshotBackedReadTransaction<Object> snapshotBackedReadTransaction =
38 new SnapshotBackedReadTransaction<>(new Object(), false, DATA_TREE_SNAPSHOT, null);
41 public void basicTest() throws Exception {
42 final NormalizedNode testNode = mock(NormalizedNode.class);
43 final Optional<NormalizedNode> optional = Optional.of(testNode);
44 doReturn("testNode").when(testNode).toString();
45 doReturn(Optional.of(testNode)).when(DATA_TREE_SNAPSHOT).readNode(YangInstanceIdentifier.of());
46 assertTrue(snapshotBackedReadTransaction.exists(YangInstanceIdentifier.of()).get());
48 assertEquals(optional, snapshotBackedReadTransaction.read(YangInstanceIdentifier.of()).get());
49 final Field stableSnapshotField = SnapshotBackedReadTransaction.class.getDeclaredField("stableSnapshot");
50 stableSnapshotField.setAccessible(true);
52 DataTreeSnapshot stableSnapshot = (DataTreeSnapshot) stableSnapshotField.get(snapshotBackedReadTransaction);
53 assertNotNull(stableSnapshot);
54 snapshotBackedReadTransaction.close();
55 stableSnapshot = (DataTreeSnapshot) stableSnapshotField.get(snapshotBackedReadTransaction);
56 assertNull(stableSnapshot);
60 public void readTestWithException() {
61 snapshotBackedReadTransaction.close();
62 final var future = snapshotBackedReadTransaction.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_SNAPSHOT).readNode(any());
72 snapshotBackedReadTransaction = new SnapshotBackedReadTransaction<>(new Object(), false, DATA_TREE_SNAPSHOT,
75 final var future = snapshotBackedReadTransaction.read(YangInstanceIdentifier.of());
76 final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
77 assertThat(cause, instanceOf(ReadFailedException.class));
78 assertEquals("Read failed", cause.getMessage());
79 assertSame(thrown, cause.getCause());
83 public void existsTestWithException() {
84 final var thrown = new NullPointerException("no Node");
85 doThrow(thrown).when(DATA_TREE_SNAPSHOT).readNode(any());
87 final var future = snapshotBackedReadTransaction.exists(YangInstanceIdentifier.of());
88 final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
89 assertThat(cause, instanceOf(ReadFailedException.class));
90 assertEquals("Read failed", cause.getMessage());
91 assertSame(thrown, cause.getCause());