Merge "Add unit tests for sal-netconf-connector"
[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.base.Optional;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import com.google.common.util.concurrent.Futures;
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
25 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30
31 public class ReadWriteTxTest {
32     @Mock
33     private DOMDataReadTransaction delegateReadTx;
34     @Mock
35     private DOMDataWriteTransaction delegateWriteTx;
36     private ReadWriteTx tx;
37
38     @Before
39     public void setUp() throws Exception {
40         MockitoAnnotations.initMocks(this);
41         tx = new ReadWriteTx(delegateReadTx, delegateWriteTx);
42     }
43
44     @Test
45     public void submit() throws Exception {
46         final YangInstanceIdentifier id1 = TxTestUtils.getContainerId();
47         final ContainerNode containerNode = TxTestUtils.getContainerNode();
48         tx.put(LogicalDatastoreType.CONFIGURATION, id1, containerNode);
49         verify(delegateWriteTx).put(LogicalDatastoreType.CONFIGURATION, id1, containerNode);
50         final YangInstanceIdentifier id2 = TxTestUtils.getLeafId();
51         final LeafNode<String> leafNode = TxTestUtils.getLeafNode();
52         tx.merge(LogicalDatastoreType.CONFIGURATION, id2, leafNode);
53         verify(delegateWriteTx).merge(LogicalDatastoreType.CONFIGURATION, id2, leafNode);
54         tx.delete(LogicalDatastoreType.CONFIGURATION, id2);
55         verify(delegateWriteTx).delete(LogicalDatastoreType.CONFIGURATION, id2);
56         tx.submit();
57         verify(delegateWriteTx).submit();
58     }
59
60     @Test
61     public void commit() throws Exception {
62         tx.commit();
63         verify(delegateWriteTx).commit();
64     }
65
66     @Test
67     public void cancel() throws Exception {
68         tx.cancel();
69         verify(delegateWriteTx).cancel();
70     }
71
72     @Test
73     public void read() throws Exception {
74         tx.read(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getContainerId());
75         verify(delegateReadTx).read(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getContainerId());
76     }
77
78     @Test
79     public void exists() throws Exception {
80         final YangInstanceIdentifier id = TxTestUtils.getContainerId();
81         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultFuture =
82                 Futures.immediateCheckedFuture(Optional.of(TxTestUtils.getContainerNode()));
83         when(delegateReadTx.read(LogicalDatastoreType.CONFIGURATION, id)).thenReturn(resultFuture);
84         final CheckedFuture<Boolean, ReadFailedException> exists = tx.exists(LogicalDatastoreType.CONFIGURATION, id);
85         Assert.assertTrue(exists.get());
86     }
87
88     @Test
89     public void getIdentifier() throws Exception {
90         final ReadWriteTx tx2 = new ReadWriteTx(null, null);
91         Assert.assertNotEquals(tx.getIdentifier(), tx2.getIdentifier());
92     }
93
94 }