b5c36cb065f59cb7cd913ac58fa0a54967fc40a1
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / test / WriteTransactionTest.java
1 /*
2  * Copyright (c) 2014 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.mdsal.binding.dom.adapter.test;
9
10 import static org.junit.Assert.assertTrue;
11
12 import java.util.Optional;
13 import java.util.concurrent.ExecutionException;
14 import org.junit.Test;
15 import org.opendaylight.mdsal.binding.api.ReadTransaction;
16 import org.opendaylight.mdsal.binding.api.WriteTransaction;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.TopBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24
25 public class WriteTransactionTest extends AbstractDataBrokerTest {
26
27     private static final InstanceIdentifier<Top> TOP_PATH = InstanceIdentifier.create(Top.class);
28     private static final TopLevelListKey TOP_LIST_KEY = new TopLevelListKey("foo");
29     private static final InstanceIdentifier<TopLevelList> NODE_PATH = TOP_PATH.child(TopLevelList.class, TOP_LIST_KEY);
30     private static final TopLevelList NODE = new TopLevelListBuilder().withKey(TOP_LIST_KEY).build();
31
32     @Test
33     public void test() throws InterruptedException, ExecutionException {
34         final WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
35         writeTx.put(LogicalDatastoreType.OPERATIONAL, TOP_PATH, new TopBuilder().build());
36         writeTx.put(LogicalDatastoreType.OPERATIONAL, NODE_PATH, NODE);
37         writeTx.commit().get();
38     }
39
40     @Test
41     public void testPutCreateParentsSuccess() throws InterruptedException, ExecutionException {
42
43         final WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
44         writeTx.put(LogicalDatastoreType.OPERATIONAL, NODE_PATH, NODE,true);
45         writeTx.commit().get();
46
47         final ReadTransaction readTx = getDataBroker().newReadOnlyTransaction();
48         final Optional<Top> topNode = readTx.read(LogicalDatastoreType.OPERATIONAL, TOP_PATH).get();
49         assertTrue("Top node must exists after commit",topNode.isPresent());
50         final Optional<TopLevelList> listNode = readTx.read(LogicalDatastoreType.OPERATIONAL, NODE_PATH).get();
51         assertTrue("List node must exists after commit",listNode.isPresent());
52     }
53
54     @Test
55     public void testMergeCreateParentsSuccess() throws InterruptedException, ExecutionException {
56
57         final WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
58         writeTx.merge(LogicalDatastoreType.OPERATIONAL, NODE_PATH, NODE,true);
59         writeTx.commit().get();
60
61         final ReadTransaction readTx = getDataBroker().newReadOnlyTransaction();
62         final Optional<Top> topNode = readTx.read(LogicalDatastoreType.OPERATIONAL, TOP_PATH).get();
63         assertTrue("Top node must exists after commit",topNode.isPresent());
64         final Optional<TopLevelList> listNode = readTx.read(LogicalDatastoreType.OPERATIONAL, NODE_PATH).get();
65         assertTrue("List node must exists after commit",listNode.isPresent());
66     }
67
68 }