d742d325bed2d380c339cf429686fddab6600769
[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     private static final InstanceIdentifier<Top> TOP_PATH = InstanceIdentifier.create(Top.class);
27     private static final TopLevelListKey TOP_LIST_KEY = new TopLevelListKey("foo");
28     private static final InstanceIdentifier<TopLevelList> NODE_PATH = TOP_PATH.child(TopLevelList.class, TOP_LIST_KEY);
29     private static final TopLevelList NODE = new TopLevelListBuilder().withKey(TOP_LIST_KEY).build();
30
31     @Test
32     public void test() throws InterruptedException, ExecutionException {
33         final WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
34         writeTx.put(LogicalDatastoreType.OPERATIONAL, TOP_PATH, new TopBuilder().build());
35         writeTx.put(LogicalDatastoreType.OPERATIONAL, NODE_PATH, NODE);
36         writeTx.commit().get();
37     }
38
39     @Test
40     public void testPutCreateParentsSuccess() throws InterruptedException, ExecutionException {
41         final WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
42         writeTx.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, NODE_PATH, NODE);
43         writeTx.commit().get();
44
45         final ReadTransaction readTx = getDataBroker().newReadOnlyTransaction();
46         final Optional<Top> topNode = readTx.read(LogicalDatastoreType.OPERATIONAL, TOP_PATH).get();
47         assertTrue("Top node must exists after commit",topNode.isPresent());
48         final Optional<TopLevelList> listNode = readTx.read(LogicalDatastoreType.OPERATIONAL, NODE_PATH).get();
49         assertTrue("List node must exists after commit",listNode.isPresent());
50     }
51
52     @Test
53     public void testMergeCreateParentsSuccess() throws InterruptedException, ExecutionException {
54         final WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
55         writeTx.mergeParentStructureMerge(LogicalDatastoreType.OPERATIONAL, NODE_PATH, NODE);
56         writeTx.commit().get();
57
58         final ReadTransaction readTx = getDataBroker().newReadOnlyTransaction();
59         final Optional<Top> topNode = readTx.read(LogicalDatastoreType.OPERATIONAL, TOP_PATH).get();
60         assertTrue("Top node must exists after commit",topNode.isPresent());
61         final Optional<TopLevelList> listNode = readTx.read(LogicalDatastoreType.OPERATIONAL, NODE_PATH).get();
62         assertTrue("List node must exists after commit",listNode.isPresent());
63     }
64 }