Migrate users of deprecated yang.common methods
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / 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;
9
10 import static org.junit.Assert.assertEquals;
11
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;
25
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")
35         .build();
36
37     @Test
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();
43     }
44
45     @Test
46     public void testPutCreateParentsSuccess() throws Exception {
47         final var writeTx = getDataBroker().newWriteOnlyTransaction();
48         writeTx.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, NODE_PATH, NODE);
49         writeTx.commit().get();
50
51         assertTop(new TopBuilder().setTopLevelList(BindingMap.of(NODE)).build());
52     }
53
54     @Test
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();
59
60         assertTop(new TopBuilder()
61             .setTopLevelList(BindingMap.of(new TopLevelListBuilder()
62                 .withKey(TOP_LIST_KEY)
63                 .addAugmentation(NODE_AUGMENT)
64                 .build()))
65             .build());
66     }
67
68     @Test
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();
73     }
74
75     @Test
76     public void testMergeCreateParentsSuccess() throws Exception {
77         final var writeTx = getDataBroker().newWriteOnlyTransaction();
78         writeTx.mergeParentStructureMerge(LogicalDatastoreType.OPERATIONAL, NODE_PATH, NODE);
79         writeTx.commit().get();
80
81         assertTop(new TopBuilder().setTopLevelList(BindingMap.of(NODE)).build());
82     }
83
84     @Test
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();
89
90         assertTop(new TopBuilder()
91             .setTopLevelList(BindingMap.of(new TopLevelListBuilder()
92                 .withKey(TOP_LIST_KEY)
93                 .addAugmentation(NODE_AUGMENT)
94                 .build()))
95             .build());
96     }
97
98     @Test
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();
103     }
104
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());
108         }
109     }
110 }