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.assertTrue;
12 import static org.junit.Assert.fail;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.doThrow;
16 import static org.mockito.Mockito.mock;
18 import com.google.common.base.Optional;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.mdsal.common.api.ReadFailedException;
22 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
28 public class SnapshotBackedReadWriteTransactionTest {
30 private static final DataTreeSnapshot DATA_TREE_SNAPSHOT = mock(DataTreeSnapshot.class);
31 private static final DataTreeModification DATA_TREE_MODIFICATION = mock(DataTreeModification.class);
32 private static final TransactionReadyPrototype TRANSACTION_READY_PROTOTYPE = mock(TransactionReadyPrototype.class);
33 private SnapshotBackedReadWriteTransaction snapshotBackedReadWriteTransaction;
36 public void setUp() throws Exception {
37 doReturn(DATA_TREE_MODIFICATION).when(DATA_TREE_SNAPSHOT).newModification();
38 snapshotBackedReadWriteTransaction = new SnapshotBackedReadWriteTransaction(new Object(), false,
39 DATA_TREE_SNAPSHOT, TRANSACTION_READY_PROTOTYPE);
43 public void basicTest() throws Exception {
44 final NormalizedNode<?, ?> testNode = mock(NormalizedNode.class);
45 final Optional<NormalizedNode> optional = Optional.of(testNode);
46 doReturn("testNode").when(testNode).toString();
47 doReturn(optional).when(DATA_TREE_MODIFICATION).readNode(YangInstanceIdentifier.EMPTY);
48 assertTrue((Boolean) snapshotBackedReadWriteTransaction.exists(YangInstanceIdentifier.EMPTY).get());
49 assertEquals(optional, snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.EMPTY).get());
52 @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:IllegalCatch"})
53 @Test(expected = ReadFailedException.class)
54 public void readTestWithNullException() throws Throwable {
55 doReturn(null).when(DATA_TREE_MODIFICATION).readNode(YangInstanceIdentifier.EMPTY);
57 snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.EMPTY).get();
58 fail("Expected ReadFailedException");
59 } catch (Exception e) {
64 @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:IllegalCatch"})
65 @Test(expected = ReadFailedException.class)
66 public void readNodeTestWithException() throws Throwable {
67 doThrow(new NullPointerException("no Node")).when(DATA_TREE_MODIFICATION).readNode(any());
69 snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.EMPTY).get();
70 fail("Expected ReadFailedException");
71 } catch (Exception e) {
76 @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:IllegalCatch"})
77 @Test(expected = ReadFailedException.class)
78 public void existsTestWithException() throws Throwable {
79 doThrow(new NullPointerException("no Node")).when(DATA_TREE_MODIFICATION).readNode(any());
81 snapshotBackedReadWriteTransaction.exists(YangInstanceIdentifier.EMPTY).get();
82 fail("Expected ReadFailedException");
83 } catch (Exception e) {