Split out yang-data-tree-{api,spi}
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / leafref / Bug8713Test.java
1 /*
2  * Copyright (c) 2016 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.tree.leafref;
9
10 import org.junit.Test;
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
15 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
16 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
17 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
18 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
19 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
20 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
21 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
24
25 public class Bug8713Test {
26     private static final String FOO_NS = "foo";
27     private static final String BAR_NS = "bar";
28     private static final String REV = "2017-09-06";
29
30     @Test
31     public void dataTreeCanditateValidationTest() throws Exception {
32         final EffectiveModelContext context = YangParserTestUtils.parseYangResourceDirectory("/bug8713/");
33         final LeafRefContext rootLeafRefContext = LeafRefContext.create(context);
34         final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
35             DataTreeConfiguration.DEFAULT_OPERATIONAL, context);
36
37         final ContainerNode root = createRootContainer();
38         final YangInstanceIdentifier rootPath = YangInstanceIdentifier.of(foo("root"));
39         final DataTreeModification writeModification = inMemoryDataTree.takeSnapshot().newModification();
40         writeModification.write(rootPath, root);
41         writeModification.ready();
42
43         final DataTreeCandidate writeContributorsCandidate = inMemoryDataTree.prepare(writeModification);
44
45         LeafRefValidation.validate(writeContributorsCandidate, rootLeafRefContext);
46         inMemoryDataTree.commit(writeContributorsCandidate);
47     }
48
49     private static ContainerNode createRootContainer() {
50         return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(foo("root")))
51                 .withChild(ImmutableNodes.leafNode(bar("target"), "target value"))
52                 .withChild(ImmutableNodes.leafNode(bar("ref"), "target value")).build();
53     }
54
55     private static QName foo(final String localName) {
56         return QName.create(FOO_NS, REV, localName);
57     }
58
59     private static QName bar(final String localName) {
60         return QName.create(BAR_NS, REV, localName);
61     }
62 }