Add ImmutableNode.newXYXBuilder() methods
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / impl / Bug4295Test.java
1 /*
2  * Copyright (c) 2015 Pantheon Technologies s.r.o. 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 org.junit.jupiter.api.Test;
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.common.QNameModule;
13 import org.opendaylight.yangtools.yang.common.XMLNamespace;
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.api.schema.ContainerNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.builder.CollectionNodeBuilder;
21 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
22 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
23 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
24 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
25 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
26 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
27 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
28 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29
30 class Bug4295Test {
31
32     private DataTree inMemoryDataTree;
33     private QName root;
34     private QName subRoot;
35     private QName outerList;
36     private QName innerList;
37     private QName oid;
38     private QName iid;
39     private QName oleaf;
40     private QName ileaf;
41     private QNameModule foo;
42
43     @Test
44     void test() throws DataValidationFailedException {
45         foo = QNameModule.create(XMLNamespace.of("foo"));
46         root = QName.create(foo, "root");
47         subRoot = QName.create(foo, "sub-root");
48         outerList = QName.create(foo, "outer-list");
49         innerList = QName.create(foo, "inner-list");
50         oid = QName.create(foo, "o-id");
51         iid = QName.create(foo, "i-id");
52         oleaf = QName.create(foo, "o");
53         ileaf = QName.create(foo, "i");
54         inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
55                 YangParserTestUtils.parseYang("""
56                 module foo {
57                   namespace "foo";
58                   prefix foo;
59
60                   container root {
61                     container sub-root {
62                       list outer-list {
63                         key "o-id";
64                         leaf o-id {
65                           type string;
66                         }
67                         list inner-list {
68                           key "i-id";
69                           leaf i-id {
70                             type string;
71                           }
72                           leaf i {
73                             type string;
74                           }
75                         }
76                         leaf o {
77                           type string;
78                         }
79                       }
80                     }
81                   }
82                 }"""));
83
84         firstModification();
85         secondModification(1);
86         secondModification(2);
87         secondModification(3);
88     }
89
90     private void firstModification() throws DataValidationFailedException {
91         /*  MERGE */
92         YangInstanceIdentifier path = YangInstanceIdentifier.of(root);
93         DataTreeModification modification = inMemoryDataTree.takeSnapshot().newModification();
94         modification.merge(path, createRootContainerBuilder()
95             .withChild(createSubRootContainerBuilder()
96                 .withChild(ImmutableNodes.newSystemMapBuilder()
97                     .withNodeIdentifier(NodeIdentifier.create(outerList))
98                     .withChild(createOuterListEntry("1", "o-1"))
99                     .withChild(createOuterListEntry("2", "o-2"))
100                     .withChild(createOuterListEntry("3", "o-3"))
101                     .build())
102                 .build())
103             .build());
104
105         /*  WRITE INNER LIST WITH ENTRIES*/
106         path = YangInstanceIdentifier.of(root).node(subRoot).node(outerList).node(createOuterListEntryPath("2"))
107                 .node(innerList);
108         modification.write(path, createInnerListBuilder()
109             .withChild(createInnerListEntry("a", "i-a"))
110             .withChild(createInnerListEntry("b", "i-b"))
111             .build());
112
113         /*  COMMIT */
114         modification.ready();
115         inMemoryDataTree.validate(modification);
116         inMemoryDataTree.commit(inMemoryDataTree.prepare(modification));
117     }
118
119     private void secondModification(final int testScenarioNumber) throws DataValidationFailedException {
120         /*  MERGE */
121         ContainerNode rootContainerNode = createRootContainerBuilder()
122             .withChild(createSubRootContainerBuilder()
123                 .withChild(ImmutableNodes.newSystemMapBuilder()
124                     .withNodeIdentifier(NodeIdentifier.create(outerList))
125                     .withChild(createOuterListEntry("3", "o-3"))
126                     .withChild(createOuterListEntry("4", "o-4"))
127                     .withChild(createOuterListEntry("5", "o-5"))
128                     .build())
129                 .build())
130             .build();
131
132         YangInstanceIdentifier path = YangInstanceIdentifier.of(root);
133         DataTreeModification modification = inMemoryDataTree.takeSnapshot().newModification();
134         modification.merge(path, rootContainerNode);
135
136         switch (testScenarioNumber) {
137             case 1:
138                 /* WRITE EMPTY INNER LIST */
139                 writeEmptyInnerList(modification, "2");
140                 break;
141             case 2: {
142                 /* WRITE INNER LIST ENTRY */
143                 MapEntryNode innerListEntryA = createInnerListEntry("a", "i-a-2");
144                 path = YangInstanceIdentifier.of(root, subRoot, outerList).node(createOuterListEntryPath("2"))
145                     .node(innerList).node(createInnerListEntryPath("a"));
146                 modification.write(path, innerListEntryA);
147                 break;
148             }
149             case 3: {
150                 /* WRITE INNER LIST WITH ENTRIES */
151                 path = YangInstanceIdentifier.of(root, subRoot, outerList).node(createOuterListEntryPath("2"))
152                     .node(innerList);
153                 modification.write(path, createInnerListBuilder()
154                     .withChild(createInnerListEntry("a", "i-a-3"))
155                     .withChild(createInnerListEntry("c", "i-c"))
156                     .build());
157                 break;
158             }
159             default:
160                 break;
161         }
162
163         /*  COMMIT */
164         modification.ready();
165         inMemoryDataTree.validate(modification);
166         inMemoryDataTree.commit(inMemoryDataTree.prepare(modification));
167     }
168
169     private void writeEmptyInnerList(final DataTreeModification modification, final String outerListEntryKey) {
170         YangInstanceIdentifier path = YangInstanceIdentifier.of(root, subRoot, outerList)
171                 .node(createOuterListEntryPath(outerListEntryKey)).node(innerList);
172         modification.write(path, createInnerListBuilder().build());
173     }
174
175     private DataContainerNodeBuilder<NodeIdentifier, ContainerNode> createRootContainerBuilder() {
176         return ImmutableNodes.newContainerBuilder().withNodeIdentifier(new NodeIdentifier(root));
177     }
178
179     private DataContainerNodeBuilder<NodeIdentifier, ContainerNode> createSubRootContainerBuilder() {
180         return  ImmutableNodes.newContainerBuilder().withNodeIdentifier(new NodeIdentifier(subRoot));
181     }
182
183     private CollectionNodeBuilder<MapEntryNode, SystemMapNode> createInnerListBuilder() {
184         return ImmutableNodes.newSystemMapBuilder().withNodeIdentifier(NodeIdentifier.create(innerList));
185     }
186
187     private NodeIdentifierWithPredicates createInnerListEntryPath(final String keyValue) {
188         return NodeIdentifierWithPredicates.of(innerList, iid, keyValue);
189     }
190
191     private NodeIdentifierWithPredicates createOuterListEntryPath(final String keyValue) {
192         return NodeIdentifierWithPredicates.of(outerList, oid, keyValue);
193     }
194
195     private MapEntryNode createOuterListEntry(final String keyValue, final String leafValue) {
196         return ImmutableNodes.newMapEntryBuilder()
197             .withNodeIdentifier(NodeIdentifierWithPredicates.of(outerList, oid, keyValue))
198             .withChild(ImmutableNodes.leafNode(oid, keyValue))
199             .withChild(ImmutableNodes.leafNode(oleaf, leafValue))
200             .build();
201     }
202
203     private MapEntryNode createInnerListEntry(final String keyValue, final String leafValue) {
204         return ImmutableNodes.newMapEntryBuilder()
205             .withNodeIdentifier(NodeIdentifierWithPredicates.of(innerList, iid, keyValue))
206             .withChild(ImmutableNodes.leafNode(iid, keyValue))
207             .withChild(ImmutableNodes.leafNode(ileaf, leafValue))
208             .build();
209     }
210 }