Migrate netconf to MD-SAL APIs
[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 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
9
10 import static org.junit.Assert.assertNotEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.when;
14
15 import com.google.common.util.concurrent.FluentFuture;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mock;
19 import org.mockito.MockitoAnnotations;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
23 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
27
28 public class ReadWriteTxTest {
29     @Mock
30     private DOMDataTreeReadTransaction delegateReadTx;
31     @Mock
32     private DOMDataTreeWriteTransaction delegateWriteTx;
33     private ReadWriteTx tx;
34
35     @Before
36     public void setUp() throws Exception {
37         MockitoAnnotations.initMocks(this);
38         tx = new ReadWriteTx(delegateReadTx, delegateWriteTx);
39     }
40
41     @Test
42     public void submit() throws Exception {
43         final YangInstanceIdentifier id1 = TxTestUtils.getContainerId();
44         final ContainerNode containerNode = TxTestUtils.getContainerNode();
45         tx.put(LogicalDatastoreType.CONFIGURATION, id1, containerNode);
46         verify(delegateWriteTx).put(LogicalDatastoreType.CONFIGURATION, id1, containerNode);
47         final YangInstanceIdentifier id2 = TxTestUtils.getLeafId();
48         final LeafNode<String> leafNode = TxTestUtils.getLeafNode();
49         tx.merge(LogicalDatastoreType.CONFIGURATION, id2, leafNode);
50         verify(delegateWriteTx).merge(LogicalDatastoreType.CONFIGURATION, id2, leafNode);
51         tx.delete(LogicalDatastoreType.CONFIGURATION, id2);
52         verify(delegateWriteTx).delete(LogicalDatastoreType.CONFIGURATION, id2);
53         tx.commit();
54         verify(delegateWriteTx).commit();
55     }
56
57     @Test
58     public void cancel() throws Exception {
59         tx.cancel();
60         verify(delegateWriteTx).cancel();
61     }
62
63     @Test
64     public void read() throws Exception {
65         tx.read(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getContainerId());
66         verify(delegateReadTx).read(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getContainerId());
67     }
68
69     @Test
70     public void exists() throws Exception {
71         final YangInstanceIdentifier id = TxTestUtils.getContainerId();
72         when(delegateReadTx.exists(LogicalDatastoreType.CONFIGURATION, id)).thenReturn(
73             FluentFutures.immediateTrueFluentFuture());
74         final FluentFuture<Boolean> exists = tx.exists(LogicalDatastoreType.CONFIGURATION, id);
75         assertTrue(exists.get());
76     }
77
78     @Test
79     public void getIdentifier() throws Exception {
80         final ReadWriteTx tx2 = new ReadWriteTx(null, null);
81         assertNotEquals(tx.getIdentifier(), tx2.getIdentifier());
82     }
83 }