795af83945ca2b8b7f86f56a9cf32dc647355831
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / sal / tx / ReadWriteTxTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
10
11 import static org.mockito.Mockito.verify;
12 import static org.mockito.Mockito.when;
13
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.Futures;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
28
29 public class ReadWriteTxTest {
30     @Mock
31     private DOMDataReadTransaction delegateReadTx;
32     @Mock
33     private DOMDataWriteTransaction delegateWriteTx;
34     private ReadWriteTx tx;
35
36     @Before
37     public void setUp() throws Exception {
38         MockitoAnnotations.initMocks(this);
39         tx = new ReadWriteTx(delegateReadTx, delegateWriteTx);
40     }
41
42     @Test
43     public void submit() throws Exception {
44         final YangInstanceIdentifier id1 = TxTestUtils.getContainerId();
45         final ContainerNode containerNode = TxTestUtils.getContainerNode();
46         tx.put(LogicalDatastoreType.CONFIGURATION, id1, containerNode);
47         verify(delegateWriteTx).put(LogicalDatastoreType.CONFIGURATION, id1, containerNode);
48         final YangInstanceIdentifier id2 = TxTestUtils.getLeafId();
49         final LeafNode<String> leafNode = TxTestUtils.getLeafNode();
50         tx.merge(LogicalDatastoreType.CONFIGURATION, id2, leafNode);
51         verify(delegateWriteTx).merge(LogicalDatastoreType.CONFIGURATION, id2, leafNode);
52         tx.delete(LogicalDatastoreType.CONFIGURATION, id2);
53         verify(delegateWriteTx).delete(LogicalDatastoreType.CONFIGURATION, id2);
54         tx.submit();
55         verify(delegateWriteTx).submit();
56     }
57
58     @Test
59     public void commit() throws Exception {
60         tx.commit();
61         verify(delegateWriteTx).commit();
62     }
63
64     @Test
65     public void cancel() throws Exception {
66         tx.cancel();
67         verify(delegateWriteTx).cancel();
68     }
69
70     @Test
71     public void read() throws Exception {
72         tx.read(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getContainerId());
73         verify(delegateReadTx).read(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getContainerId());
74     }
75
76     @Test
77     public void exists() throws Exception {
78         final YangInstanceIdentifier id = TxTestUtils.getContainerId();
79         final CheckedFuture<Boolean, ReadFailedException> resultFuture =
80                 Futures.immediateCheckedFuture(true);
81         when(delegateReadTx.exists(LogicalDatastoreType.CONFIGURATION, id)).thenReturn(resultFuture);
82         final CheckedFuture<Boolean, ReadFailedException> exists = tx.exists(LogicalDatastoreType.CONFIGURATION, id);
83         Assert.assertTrue(exists.get());
84     }
85
86     @Test
87     public void getIdentifier() throws Exception {
88         final ReadWriteTx tx2 = new ReadWriteTx(null, null);
89         Assert.assertNotEquals(tx.getIdentifier(), tx2.getIdentifier());
90     }
91
92 }