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<Object> TRANSACTION_READY_PROTOTYPE =
33 mock(TransactionReadyPrototype.class);
34 private SnapshotBackedReadWriteTransaction<Object> snapshotBackedReadWriteTransaction;
37 public void setUp() throws Exception {
38 doReturn(DATA_TREE_MODIFICATION).when(DATA_TREE_SNAPSHOT).newModification();
39 snapshotBackedReadWriteTransaction = new SnapshotBackedReadWriteTransaction<>(new Object(), false,
40 DATA_TREE_SNAPSHOT, TRANSACTION_READY_PROTOTYPE);
44 public void basicTest() throws Exception {
45 final NormalizedNode<?, ?> testNode = mock(NormalizedNode.class);
46 final Optional<NormalizedNode<?, ?>> optional = Optional.of(testNode);
47 doReturn("testNode").when(testNode).toString();
48 doReturn(optional).when(DATA_TREE_MODIFICATION).readNode(YangInstanceIdentifier.EMPTY);
49 assertTrue(snapshotBackedReadWriteTransaction.exists(YangInstanceIdentifier.EMPTY).get());
50 assertEquals(optional, snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.EMPTY).get());
53 @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:IllegalCatch"})
54 @Test(expected = ReadFailedException.class)
55 public void readTestWithNullException() throws Throwable {
56 doReturn(null).when(DATA_TREE_MODIFICATION).readNode(YangInstanceIdentifier.EMPTY);
58 snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.EMPTY).get();
59 fail("Expected ReadFailedException");
60 } catch (Exception e) {
65 @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:IllegalCatch"})
66 @Test(expected = ReadFailedException.class)
67 public void readNodeTestWithException() throws Throwable {
68 doThrow(new NullPointerException("no Node")).when(DATA_TREE_MODIFICATION).readNode(any());
70 snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.EMPTY).get();
71 fail("Expected ReadFailedException");
72 } catch (Exception e) {
77 @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:IllegalCatch"})
78 @Test(expected = ReadFailedException.class)
79 public void existsTestWithException() throws Throwable {
80 doThrow(new NullPointerException("no Node")).when(DATA_TREE_MODIFICATION).readNode(any());
82 snapshotBackedReadWriteTransaction.exists(YangInstanceIdentifier.EMPTY).get();
83 fail("Expected ReadFailedException");
84 } catch (Exception e) {