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