Cleanup DataTree interfaces and InMemmoryDataTreeFactory
[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.DataTree;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
26 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
27 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29
30 public class Bug8291Test {
31     private static final String NS = "foo";
32     private static final QName ROOT = QName.create(NS, "root");
33     private static final QName OUTER_LIST = QName.create(NS, "outer-list");
34     private static final QName OUTER_LIST_ID = QName.create(NS, "id");
35     private static final QName INNER_LIST = QName.create(NS, "inner-list");
36     private SchemaContext schemaContext;
37
38     @Before
39     public void init() {
40         this.schemaContext = TestModel.createTestContext("/bug8291/foo.yang");
41         assertNotNull("Schema context must not be null.", this.schemaContext);
42     }
43
44     private static DataTree 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         return new InMemoryDataTreeFactory().create(config, schemaContext);
49     }
50
51     @Test
52     public void test() throws DataValidationFailedException {
53         final DataTree inMemoryDataTree = initDataTree(schemaContext);
54         writeOuterListMapEntry(inMemoryDataTree);
55         writeInnerList(inMemoryDataTree);
56     }
57
58     private static void writeInnerList(final DataTree inMemoryDataTree) throws DataValidationFailedException {
59         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
60         modificationTree.write(
61                 YangInstanceIdentifier.create(
62                         new NodeIdentifierWithPredicates(OUTER_LIST, ImmutableMap.of(OUTER_LIST_ID, 1))).node(
63                         INNER_LIST), Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(INNER_LIST)).build());
64
65         modificationTree.ready();
66         inMemoryDataTree.validate(modificationTree);
67         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
68         inMemoryDataTree.commit(prepare);
69     }
70
71     private static void writeOuterListMapEntry(final DataTree inMemoryDataTree)
72             throws DataValidationFailedException {
73         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
74
75         final MapEntryNode outerListMapEntry = Builders.mapEntryBuilder()
76                 .withNodeIdentifier(new NodeIdentifierWithPredicates(OUTER_LIST, ImmutableMap.of(OUTER_LIST_ID, 1)))
77                 .withChild(ImmutableNodes.leafNode(OUTER_LIST_ID, 1)).build();
78
79         modificationTree.write(YangInstanceIdentifier.create(new NodeIdentifierWithPredicates(OUTER_LIST, ImmutableMap
80                 .of(OUTER_LIST_ID, 1))), outerListMapEntry);
81         modificationTree.ready();
82
83         inMemoryDataTree.validate(modificationTree);
84         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
85         inMemoryDataTree.commit(prepare);
86     }
87 }