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