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