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