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 @Test(expected = ReadFailedException.class)
53 public void readTestWithNullException() throws Throwable {
54 doReturn(null).when(DATA_TREE_MODIFICATION).readNode(YangInstanceIdentifier.EMPTY);
56 snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.EMPTY).get();
57 fail("Expected ReadFailedException");
58 } catch (Exception e) {
63 @Test(expected = ReadFailedException.class)
64 public void readNodeTestWithException() throws Throwable {
65 doThrow(new NullPointerException("no Node")).when(DATA_TREE_MODIFICATION).readNode(any());
67 snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.EMPTY).get();
68 fail("Expected ReadFailedException");
69 } catch (Exception e) {
74 @Test(expected = ReadFailedException.class)
75 public void existsTestWithException() throws Throwable {
76 doThrow(new NullPointerException("no Node")).when(DATA_TREE_MODIFICATION).readNode(any());
78 snapshotBackedReadWriteTransaction.exists(YangInstanceIdentifier.EMPTY).get();
79 fail("Expected ReadFailedException");
80 } catch (Exception e) {