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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.mockito.Matchers.any;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.doThrow;
18 import static org.mockito.Mockito.mock;
20 import com.google.common.base.Optional;
21 import java.lang.reflect.Field;
22 import org.junit.Test;
23 import org.opendaylight.mdsal.common.api.ReadFailedException;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
28 public class SnapshotBackedReadTransactionTest {
30 private static final DataTreeSnapshot DATA_TREE_SNAPSHOT = mock(DataTreeSnapshot.class);
31 private static SnapshotBackedReadTransaction snapshotBackedReadTransaction =
32 new SnapshotBackedReadTransaction<>(new Object(), false, DATA_TREE_SNAPSHOT);
35 public void basicTest() throws Exception {
36 final NormalizedNode<?, ?> testNode = mock(NormalizedNode.class);
37 final Optional<NormalizedNode> optional = Optional.of(testNode);
38 doReturn("testNode").when(testNode).toString();
39 doReturn(optional).when(DATA_TREE_SNAPSHOT).readNode(YangInstanceIdentifier.EMPTY);
40 assertTrue((Boolean) snapshotBackedReadTransaction.exists(YangInstanceIdentifier.EMPTY).get());
42 assertEquals(optional, snapshotBackedReadTransaction.read(YangInstanceIdentifier.EMPTY).get());
44 final Field stableSnapshotField = SnapshotBackedReadTransaction.class.getDeclaredField("stableSnapshot");
45 stableSnapshotField.setAccessible(true);
47 DataTreeSnapshot stableSnapshot = (DataTreeSnapshot) stableSnapshotField.get(snapshotBackedReadTransaction);
48 assertNotNull(stableSnapshot);
49 snapshotBackedReadTransaction.close();
50 stableSnapshot = (DataTreeSnapshot) stableSnapshotField.get(snapshotBackedReadTransaction);
51 assertNull(stableSnapshot);
54 @Test(expected = ReadFailedException.class)
55 public void readTestWithException() throws Throwable {
56 snapshotBackedReadTransaction.close();
58 snapshotBackedReadTransaction.read(YangInstanceIdentifier.EMPTY).get();
59 fail("Expected ReadFailedException");
60 } catch (Exception e) {
65 @Test(expected = ReadFailedException.class)
66 public void readNodeTestWithException() throws Throwable {
67 doThrow(new NullPointerException("no Node")).when(DATA_TREE_SNAPSHOT).readNode(any());
68 snapshotBackedReadTransaction = new SnapshotBackedReadTransaction<>(new Object(), false, DATA_TREE_SNAPSHOT);
70 snapshotBackedReadTransaction.read(YangInstanceIdentifier.EMPTY).get();
71 fail("Expected ReadFailedException");
72 } catch (Exception e) {
77 @Test(expected = ReadFailedException.class)
78 public void existsTestWithException() throws Throwable {
79 doThrow(new NullPointerException("no Node")).when(DATA_TREE_SNAPSHOT).readNode(any());
82 snapshotBackedReadTransaction.exists(YangInstanceIdentifier.EMPTY).get();
83 fail("Expected ReadFailedException");
84 } catch (Exception e) {