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.tree.leafref;
10 import org.junit.jupiter.api.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.DataTreeConfiguration;
18 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
19 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
22 private static final String FOO_NS = "foo";
23 private static final String BAR_NS = "bar";
24 private static final String REV = "2017-09-06";
27 void dataTreeCanditateValidationTest() throws Exception {
28 final var context = YangParserTestUtils.parseYang("""
33 import foo { prefix foo; revision-date 2017-09-06; }
55 final var rootLeafRefContext = LeafRefContext.create(context);
56 final var inMemoryDataTree = new InMemoryDataTreeFactory().create(
57 DataTreeConfiguration.DEFAULT_OPERATIONAL, context);
59 final var root = createRootContainer();
60 final var rootPath = YangInstanceIdentifier.of(foo("root"));
61 final var writeModification = inMemoryDataTree.takeSnapshot().newModification();
62 writeModification.write(rootPath, root);
63 writeModification.ready();
65 final var writeContributorsCandidate = inMemoryDataTree.prepare(writeModification);
67 LeafRefValidation.validate(writeContributorsCandidate, rootLeafRefContext);
68 inMemoryDataTree.commit(writeContributorsCandidate);
71 private static ContainerNode createRootContainer() {
72 return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(foo("root")))
73 .withChild(ImmutableNodes.leafNode(bar("target"), "target value"))
74 .withChild(ImmutableNodes.leafNode(bar("ref"), "target value")).build();
77 private static QName foo(final String localName) {
78 return QName.create(FOO_NS, REV, localName);
81 private static QName bar(final String localName) {
82 return QName.create(BAR_NS, REV, localName);