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