2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.adapter;
10 import static org.junit.Assert.assertTrue;
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.binding.dom.adapter.test.AbstractDataBrokerTest;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.TopBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 public class WriteTransactionTest extends AbstractDataBrokerTest {
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();
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();
41 public void testPutCreateParentsSuccess() throws InterruptedException, ExecutionException {
42 final WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
43 writeTx.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, NODE_PATH, NODE);
44 writeTx.commit().get();
46 final ReadTransaction readTx = getDataBroker().newReadOnlyTransaction();
47 final Optional<Top> topNode = readTx.read(LogicalDatastoreType.OPERATIONAL, TOP_PATH).get();
48 assertTrue("Top node must exists after commit",topNode.isPresent());
49 final Optional<TopLevelList> listNode = readTx.read(LogicalDatastoreType.OPERATIONAL, NODE_PATH).get();
50 assertTrue("List node must exists after commit",listNode.isPresent());
54 public void testPutCreateParentsSuperfluous() throws InterruptedException, ExecutionException {
55 final WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
56 writeTx.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, TOP_PATH, new TopBuilder().build());
57 writeTx.commit().get();
61 public void testMergeCreateParentsSuccess() throws InterruptedException, ExecutionException {
62 final WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
63 writeTx.mergeParentStructureMerge(LogicalDatastoreType.OPERATIONAL, NODE_PATH, NODE);
64 writeTx.commit().get();
66 final ReadTransaction readTx = getDataBroker().newReadOnlyTransaction();
67 final Optional<Top> topNode = readTx.read(LogicalDatastoreType.OPERATIONAL, TOP_PATH).get();
68 assertTrue("Top node must exists after commit",topNode.isPresent());
69 final Optional<TopLevelList> listNode = readTx.read(LogicalDatastoreType.OPERATIONAL, NODE_PATH).get();
70 assertTrue("List node must exists after commit",listNode.isPresent());
74 public void testMergeCreateParentsSuperfluous() throws InterruptedException, ExecutionException {
75 final WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
76 writeTx.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, TOP_PATH, new TopBuilder().build());
77 writeTx.commit().get();