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.assertEquals;
12 import java.util.Optional;
13 import org.junit.Test;
14 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractDataBrokerTest;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyUsesAugment;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyUsesAugmentBuilder;
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 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
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 InstanceIdentifier<TreeLeafOnlyUsesAugment> NODE_AUGMENT_PATH =
31 NODE_PATH.augmentation(TreeLeafOnlyUsesAugment.class);
32 private static final TopLevelList NODE = new TopLevelListBuilder().withKey(TOP_LIST_KEY).build();
33 private static final TreeLeafOnlyUsesAugment NODE_AUGMENT = new TreeLeafOnlyUsesAugmentBuilder()
34 .setLeafFromGrouping("foo")
38 public void test() throws Exception {
39 final var writeTx = getDataBroker().newWriteOnlyTransaction();
40 writeTx.put(LogicalDatastoreType.OPERATIONAL, TOP_PATH, new TopBuilder().build());
41 writeTx.put(LogicalDatastoreType.OPERATIONAL, NODE_PATH, NODE);
42 writeTx.commit().get();
46 public void testPutCreateParentsSuccess() throws Exception {
47 final var writeTx = getDataBroker().newWriteOnlyTransaction();
48 writeTx.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, NODE_PATH, NODE);
49 writeTx.commit().get();
51 assertTop(new TopBuilder().setTopLevelList(BindingMap.of(NODE)).build());
55 public void testPutCreateAugmentationParentsSuccess() throws Exception {
56 final var writeTx = getDataBroker().newWriteOnlyTransaction();
57 writeTx.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, NODE_AUGMENT_PATH, NODE_AUGMENT);
58 writeTx.commit().get();
60 assertTop(new TopBuilder()
61 .setTopLevelList(BindingMap.of(new TopLevelListBuilder()
62 .withKey(TOP_LIST_KEY)
63 .addAugmentation(NODE_AUGMENT)
69 public void testPutCreateParentsSuperfluous() throws Exception {
70 final var writeTx = getDataBroker().newWriteOnlyTransaction();
71 writeTx.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, TOP_PATH, new TopBuilder().build());
72 writeTx.commit().get();
76 public void testMergeCreateParentsSuccess() throws Exception {
77 final var writeTx = getDataBroker().newWriteOnlyTransaction();
78 writeTx.mergeParentStructureMerge(LogicalDatastoreType.OPERATIONAL, NODE_PATH, NODE);
79 writeTx.commit().get();
81 assertTop(new TopBuilder().setTopLevelList(BindingMap.of(NODE)).build());
85 public void testMergeCreateAugmentationParentsSuccess() throws Exception {
86 final var writeTx = getDataBroker().newWriteOnlyTransaction();
87 writeTx.mergeParentStructureMerge(LogicalDatastoreType.OPERATIONAL, NODE_AUGMENT_PATH, NODE_AUGMENT);
88 writeTx.commit().get();
90 assertTop(new TopBuilder()
91 .setTopLevelList(BindingMap.of(new TopLevelListBuilder()
92 .withKey(TOP_LIST_KEY)
93 .addAugmentation(NODE_AUGMENT)
99 public void testMergeCreateParentsSuperfluous() throws Exception {
100 final var writeTx = getDataBroker().newWriteOnlyTransaction();
101 writeTx.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, TOP_PATH, new TopBuilder().build());
102 writeTx.commit().get();
105 private void assertTop(final Top expected) throws Exception {
106 try (var readTx = getDataBroker().newReadOnlyTransaction()) {
107 assertEquals(Optional.of(expected), readTx.read(LogicalDatastoreType.OPERATIONAL, TOP_PATH).get());