2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.leafref.context;
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.api.schema.tree.DataTree;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
19 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContext;
20 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefValidation;
21 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
22 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
23 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
27 public class Bug8713Test {
28 private static final String FOO_NS = "foo";
29 private static final String BAR_NS = "bar";
30 private static final String REV = "2017-09-06";
33 public void dataTreeCanditateValidationTest() throws Exception {
34 final SchemaContext context = YangParserTestUtils.parseYangResourceDirectory("/bug8713/");
35 final LeafRefContext rootLeafRefContext = LeafRefContext.create(context);
36 final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
37 DataTreeConfiguration.DEFAULT_OPERATIONAL, context);
39 final ContainerNode root = createRootContainer();
40 final YangInstanceIdentifier rootPath = YangInstanceIdentifier.of(foo("root"));
41 final DataTreeModification writeModification = inMemoryDataTree.takeSnapshot().newModification();
42 writeModification.write(rootPath, root);
43 writeModification.ready();
45 final DataTreeCandidate writeContributorsCandidate = inMemoryDataTree.prepare(writeModification);
47 LeafRefValidation.validate(writeContributorsCandidate, rootLeafRefContext);
48 inMemoryDataTree.commit(writeContributorsCandidate);
51 private static ContainerNode createRootContainer() {
52 return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(foo("root")))
53 .withChild(ImmutableNodes.leafNode(bar("target"), "target value"))
54 .withChild(ImmutableNodes.leafNode(bar("ref"), "target value")).build();
57 private static QName foo(final String localName) {
58 return QName.create(FOO_NS, REV, localName);
61 private static QName bar(final String localName) {
62 return QName.create(BAR_NS, REV, localName);