Add ImmutableNode.newXYXBuilder() methods
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / impl / Bug8291Test.java
1 /*
2  * Copyright (c) 2017 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.yangtools.yang.data.tree.impl;
9
10 import static org.junit.jupiter.api.Assertions.assertNotNull;
11
12 import org.junit.jupiter.api.Test;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
17 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
18 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
19 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
20 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
21 import org.opendaylight.yangtools.yang.data.tree.api.TreeType;
22 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25
26 class Bug8291Test {
27     private static final String NS = "bug8291";
28     private static final QName ROOT = QName.create(NS, "root");
29     private static final QName OUTER_LIST = QName.create(NS, "outer-list");
30     private static final QName OUTER_LIST_ID = QName.create(NS, "id");
31     private static final QName INNER_LIST = QName.create(NS, "inner-list");
32
33     private EffectiveModelContext schemaContext;
34
35     private static DataTree initDataTree(final EffectiveModelContext schemaContext) {
36         final var config = new DataTreeConfiguration.Builder(TreeType.CONFIGURATION).setRootPath(
37                 YangInstanceIdentifier.of(ROOT).node(OUTER_LIST)).build();
38         return new InMemoryDataTreeFactory().create(config, schemaContext);
39     }
40
41     @Test
42     void test() throws DataValidationFailedException {
43         schemaContext = YangParserTestUtils.parseYang("""
44             module bug8291 {
45               yang-version 1;
46               namespace bug8291;
47               prefix bug8291;
48
49               container root {
50                 list outer-list {
51                   key "id";
52                   leaf id {
53                     type uint16;
54                   }
55
56                   list inner-list {
57                     key name;
58                     leaf name {
59                       type string;
60                     }
61                     leaf value {
62                       type string;
63                     }
64                   }
65                 }
66               }
67             }""");
68         assertNotNull(schemaContext, "Schema context must not be null.");
69
70         final var inMemoryDataTree = initDataTree(schemaContext);
71         writeOuterListMapEntry(inMemoryDataTree);
72         writeInnerList(inMemoryDataTree);
73     }
74
75     private static void writeInnerList(final DataTree inMemoryDataTree) throws DataValidationFailedException {
76         final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
77         modificationTree.write(
78             YangInstanceIdentifier.of(NodeIdentifierWithPredicates.of(OUTER_LIST, OUTER_LIST_ID, 1)).node(INNER_LIST),
79             ImmutableNodes.newSystemMapBuilder().withNodeIdentifier(new NodeIdentifier(INNER_LIST)).build());
80
81         modificationTree.ready();
82         inMemoryDataTree.validate(modificationTree);
83         final var prepare = inMemoryDataTree.prepare(modificationTree);
84         inMemoryDataTree.commit(prepare);
85     }
86
87     private static void writeOuterListMapEntry(final DataTree inMemoryDataTree)
88             throws DataValidationFailedException {
89         final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
90
91         final var outerListMapEntry = ImmutableNodes.newMapEntryBuilder()
92             .withNodeIdentifier(NodeIdentifierWithPredicates.of(OUTER_LIST, OUTER_LIST_ID, 1))
93             .withChild(ImmutableNodes.leafNode(OUTER_LIST_ID, 1))
94             .build();
95
96         modificationTree.write(YangInstanceIdentifier.of(NodeIdentifierWithPredicates.of(OUTER_LIST, OUTER_LIST_ID, 1)),
97             outerListMapEntry);
98         modificationTree.ready();
99
100         inMemoryDataTree.validate(modificationTree);
101         final var prepare = inMemoryDataTree.prepare(modificationTree);
102         inMemoryDataTree.commit(prepare);
103     }
104 }