e26df9e917df22da433790cf81fa4f8bf3d41c76
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / Bug8291Test.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. 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 static org.junit.Assert.assertNotNull;
11
12 import com.google.common.collect.ImmutableMap;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
19 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
24 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
25 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
28
29 public class Bug8291Test {
30     private static final String NS = "foo";
31     private static final String REV = "1970-01-01";
32     private static final QName ROOT = QName.create(NS, REV, "root");
33     private static final QName OUTER_LIST = QName.create(NS, REV, "outer-list");
34     private static final QName OUTER_LIST_ID = QName.create(NS, REV, "id");
35     private static final QName INNER_LIST = QName.create(NS, REV, "inner-list");
36     private SchemaContext schemaContext;
37
38     @Before
39     public void init() throws ReactorException {
40         this.schemaContext = TestModel.createTestContext("/bug8291/foo.yang");
41         assertNotNull("Schema context must not be null.", this.schemaContext);
42     }
43
44     private static InMemoryDataTree initDataTree(final SchemaContext schemaContext)
45             throws DataValidationFailedException {
46         final DataTreeConfiguration config = new DataTreeConfiguration.Builder(TreeType.CONFIGURATION).setRootPath(
47                 YangInstanceIdentifier.of(ROOT).node(OUTER_LIST)).build();
48         final InMemoryDataTree inMemoryDataTree = (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(
49                 config, schemaContext);
50         return inMemoryDataTree;
51     }
52
53     @Test
54     public void test() throws DataValidationFailedException {
55         final InMemoryDataTree inMemoryDataTree = initDataTree(schemaContext);
56         writeOuterListMapEntry(inMemoryDataTree);
57         writeInnerList(inMemoryDataTree);
58     }
59
60     private void writeInnerList(final InMemoryDataTree inMemoryDataTree) throws DataValidationFailedException {
61         final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
62         modificationTree.write(
63                 YangInstanceIdentifier.create(
64                         new NodeIdentifierWithPredicates(OUTER_LIST, ImmutableMap.of(OUTER_LIST_ID, 1))).node(
65                         INNER_LIST), Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(INNER_LIST)).build());
66
67         modificationTree.ready();
68         inMemoryDataTree.validate(modificationTree);
69         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
70         inMemoryDataTree.commit(prepare);
71     }
72
73     private static void writeOuterListMapEntry(final InMemoryDataTree inMemoryDataTree)
74             throws DataValidationFailedException {
75         final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
76
77         final MapEntryNode outerListMapEntry = Builders.mapEntryBuilder()
78                 .withNodeIdentifier(new NodeIdentifierWithPredicates(OUTER_LIST, ImmutableMap.of(OUTER_LIST_ID, 1)))
79                 .withChild(ImmutableNodes.leafNode(OUTER_LIST_ID, 1)).build();
80
81         modificationTree.write(YangInstanceIdentifier.create(new NodeIdentifierWithPredicates(OUTER_LIST, ImmutableMap
82                 .of(OUTER_LIST_ID, 1))), outerListMapEntry);
83         modificationTree.ready();
84
85         inMemoryDataTree.validate(modificationTree);
86         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
87         inMemoryDataTree.commit(prepare);
88     }
89 }